拆分并展平字符串列表

sou*_*gya -1 python list

lst = [
  "Zambia",
  "Zimbabwe",
  "Suite,203,2880,Zanker,Rd,San,Jose,95134",
  "1496A,1st,and,2nd,Floor,19th,main,8th,crossSector,1,HSR,Layout,Bengaluru,560102",
]
Run Code Online (Sandbox Code Playgroud)

在这里,我有一个世界.有些是实际的话只是一个示例("赞"),有些是喜欢的句子只是例子("组曲,203,2880,Zanker,路,三,圣何塞,95134")

我怎样才能将它们转换成以下格式.

lst = [
  "Zambia",
  "Zimbabwe",
  "Suite",
  "203",
  "2880",
  "Zanker",
  "Rd",
  "San",
  "Jose",
  "95134,
  "1496A",
  "1st",
  "and",
  "2nd",
  "Floor",
  "19th",
  "main",
  "8th",
  "crossSector",
  "1",
  "HSR",
  "Layout",
  "Bengaluru",
  "560102",
  "g2crowd_badge2",
  "Created with Sketch."
]
Run Code Online (Sandbox Code Playgroud)

如何使用python将列表转换为此格式

请看看这个.

Aar*_*_ab 10

尝试:

res = []    
for i in lst:
   res.extend(i.split(","))   
Run Code Online (Sandbox Code Playgroud)

另一种选择是使用reduce:

res = list(reduce(lambda a, b: a + b.split(','), lst, []))
Run Code Online (Sandbox Code Playgroud)


yat*_*atu 6

您可以使用列表推导和split每个字符串.最后将结果展平itertools.chain:

from itertools import chain
list(chain(*[i.split(',') for i in lst]))

['Zambia', 'Zimbabwe', 'Suite', '203', '2880', 'Zanker', 'Rd', 'San', 'Jose', 
 '95134', '1496A', '1st', 'and', '2nd', 'Floor', '19th', 'main', '8th', 
 'crossSector', '1', 'HSR', 'Layout', 'Bengaluru', '560102']
Run Code Online (Sandbox Code Playgroud)