Python从Lis中的列表中删除第二项

2024-06-14 04:24:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下格式的列表:

[[[u'Hot Dogs', u'hotdog'], [u'Food Stands', u'foodstands']], [[u'Scandinavian',       u'scandinavian'], [u'Breakfast & Brunch', u'breakfast_brunch'], [u'Coffee & Tea', u'coffee']],    [[u'Burgers', u'burgers']]]

我想从每个列表中删除第一项(它只是第二项的副本),然后返回这些单个标记的简单列表,而不是包含在multiple[]中。我该怎么做呢?你知道吗

编辑:我想返回一个列表列表,每一行代表下面提到的每个列表中的第二个标记


Tags: 标记列表food格式dogshotbrunchhotdog
2条回答

我想你想要的是:

rawList = [[[u'Hot Dogs', u'hotdog'], [u'Food Stands', u'foodstands']], [[u'Scandinavian',       u'scandinavian'], [u'Breakfast & Brunch', u'breakfast_brunch'], [u'Coffee & Tea', u'coffee']],    [[u'Burgers', u'burgers']]]
finalList = []

for l in rawList:
    finalList.append([i[0] for i in l])

输出如下:

[[u'Hot Dogs', u'Food Stands'], [u'Scandinavian', u'Breakfast & Brunch', u'Coffee & Tea'], [u'Burgers']]

您可以使用list comprehesion以简单的方式完成:

[y[1] for x in l for y in x]

其中l是您指定的列表。你知道吗

相关问题 更多 >