python - inplace removal of quotes from list of list -
i changed list
orig_list=['"jason","hello1,hello2,hello3","somegroup2","bundle1","loc1"', '"ruby","hello","somegroup","bundle2","loc2"', '"sam","hello3,hello2","somegroup3,somegroup4","bundle2","loc3"'] new_list=[x.split(",") x in orig_list] new_list=[['"jason"', '"hello1', 'hello2', 'hello3"', '"somegroup2"', '"bundle1"', '"loc1"'], ['"ruby"', '"hello"', '"somegroup"', '"bundle2"', '"loc2"'], ['"sam"', '"hello3', 'hello2"', '"somegroup3', 'somegroup4"', '"bundle2"', '"loc3"']]
what intent
[['jason', 'hello1,hello2,hello3', 'somegroup2', 'bundle1', 'loc1'], ['ruby', 'hello', 'somegroup', 'bundle2', 'loc2'], ['sam', 'hello3,hello2', 'somegroup3,somegroup4', 'bundle2', 'loc3']]
is possible inplace , not creating new one?
update : can have elements in double quotes, in double quotes, no double quotes , same in single quotes.
if need in-place removal of quotes, need add in [:]
list comprehension assignment:
orig_list = ['"jason","hello1,hello2,hello3","somegroup2","bundle1","loc1"', '"ruby","hello","somegroup","bundle2","loc2"', '"sam","hello3,hello2","somegroup3,somegroup4","bundle2","loc3"'] id1 = id(orig_list) orig_list[:] = [w w in orig_list] orig_list[:] = [g.replace('"', "'") g in orig_list] orig_list[:] = [h.split("',") h in orig_list] orig_list[:] = [[j.replace("'", '') j in k] k in orig_list] id2 = id(orig_list) print id1 == id2 # true print orig_list # [['jason', 'hello1,hello2,hello3', 'somegroup2', 'bundle1', 'loc1'], ['ruby', 'hello', 'somegroup', 'bundle2', 'loc2'], ['sam', 'hello3,hello2', 'somegroup3,somegroup4', 'bundle2', 'loc3']]
note orig_list[:] = ...
. ensures don't create copy of list (hence, making not in-place).
Comments
Post a Comment