从列表列表中删除引号

时间:2015-07-01 22:43:34

标签: python

我更改了此列表

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(",") for 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"']]

我的目的是获得

[['jason', 'hello1,hello2,hello3', 'somegroup2', 'bundle1', 'loc1'], ['ruby', 'hello', 'somegroup', 'bundle2', 'loc2'], ['sam', 'hello3,hello2', 'somegroup3,somegroup4', 'bundle2', 'loc3']]

是否可以在原地进行而不是创建新的?

更新:我可以在双引号中加入一些元素,所有元素都用双引号,没有双引号和单引号相同。

3 个答案:

答案 0 :(得分:2)

而不是分割,上的","分割:

new_list=[[l.replace('"','') for l in x.split('","') for x in orig_list]
new_list
Out[99]: [['jason', 'hello1,hello2,hello3', 'somegroup2', 'bundle1', 'loc1'], ['ruby', 'hello', 'somegroup', 'bundle2', 'loc2'], ['sam', 'hello3,hello2', 'somegroup3,somegroup4', 'bundle2', 'loc3']]

答案 1 :(得分:1)

有效列表,保留分组元素的分组

使用csv module

中的reader功能
from csv import reader

orig_list=['"jason","hello1,hello2,hello3","somegroup2","bundle1","loc1"', '"ruby","hello","somegroup","bundle2","loc2"', '"sam","hello3,hello2","somegroup3,somegroup4","bundle2","loc3"']

new_list = []

for line in reader(orig_list):
  new_list.append(line)

这会输出您要求的结果:

[['jason', 'hello1,hello2,hello3', 'somegroup2', 'bundle1', 'loc1'], ['ruby', 'hello', 'somegroup', 'bundle2', 'loc2'], ['sam', 'hello3,hello2', 'somegroup3,somegroup4', 'bundle2', 'loc3']]

取消组合所有元素

如果要取消组合所有以逗号分隔的元素,可以将列表转换为字符串,然后将其拆分:

orig_list2=['jason,"hello1,hello2,hello3",somegroup2,bundle1,loc1', 'ruby,hello,somegroup,bundle2,loc2', 'sam','hello3,hello2',"somegroup3,somegroup4","bundle2",'loc3']

orig_list2 = str(orig_list2)

# list of characters to remove
bad_chars = ['\'','"','[',']',' ']

for c in bad_chars:
  orig_list2 = orig_list2.replace(c,'')

# put into a list
new_list2 = orig_list2.split(',')

如果你正在处理一个看起来像列表但却无效的字符串,因为某些引号不是完整的对,就像你在JohnZ的注释中留下的那样,你也可以使用这种方法,但你不需要将其转换为字符串。

答案 2 :(得分:1)

如果您需要就地删除引号,则需要将[:]添加到列表理解分配中:

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 for w in orig_list]
orig_list[:] = [g.replace('"', "'") for g in orig_list]
orig_list[:] = [h.split("',") for h in orig_list]
orig_list[:] = [[j.replace("'", '') for j in k] for 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']]

请注意orig_list[:] = ...。这可以确保您不创建列表的副本(因此,使其不在原地)。