使用理解列表将列表的所有 csv 字符串作为单个元素获取到新列表中?
Get all the csv strings of a list as single elements in a new list with a comprehension list?
我有一个列表如下:
listt = ['34','56,67','45,56,67','45']
我想获取单个值的列表。
这是我的代码:
new_list=[]
for element in listt:
if ',' in element:
subl=element.split(',')
new_list = new_list + subl
else:
new_list.append(element)
结果:
['34', '56', '67', '45', '56', '67', '45']
真的有办法用理解列表来做到这一点吗? (即一个班轮)。
这么小的东西看起来代码太多了。
谢谢。
listt = ['34','56,67','45,56,67','45']
print(','.join(listt).split(','))
打印:
['34', '56', '67', '45', '56', '67', '45']
spam = ['34','56,67','45,56,67','45']
eggs = [num for item in spam for num in item.split(',')]
print(eggs)
输出
['34', '56', '67', '45', '56', '67', '45']
我有一个列表如下:
listt = ['34','56,67','45,56,67','45']
我想获取单个值的列表。
这是我的代码:
new_list=[]
for element in listt:
if ',' in element:
subl=element.split(',')
new_list = new_list + subl
else:
new_list.append(element)
结果:
['34', '56', '67', '45', '56', '67', '45']
真的有办法用理解列表来做到这一点吗? (即一个班轮)。 这么小的东西看起来代码太多了。
谢谢。
listt = ['34','56,67','45,56,67','45']
print(','.join(listt).split(','))
打印:
['34', '56', '67', '45', '56', '67', '45']
spam = ['34','56,67','45,56,67','45']
eggs = [num for item in spam for num in item.split(',')]
print(eggs)
输出
['34', '56', '67', '45', '56', '67', '45']