【发布时间】:2020-09-23 04:17:48
【问题描述】:
我有一个项目列表,它会输出一个列表列表:
item_list = ['book', 'pen']
output = [
['pencil', 'eraser', 'pen', 'glue', 'folders'],
['notepad', 'book','pencil', 'markers','ruler','scissors']
]
有没有办法将输出作为唯一的项目列表,其中包含每个子列表中的前 3 个项目,但也不在初始 item_list 中?
所以输出看起来像(按照确切的顺序):
new_output = ['pencil', 'eraser', 'glue', 'notepad', 'markers', 'ruler']
或者如果可能的话:
new_output = ['pencil', 'notepad', 'eraser', 'markers', 'glue','ruler']
【问题讨论】:
-
你如何决定哪个项目是顶级的?
-
它最初是一个包含 item 和 value 列的数据框,但我对其进行了排序,只取了 item 列并将其作为列表。
-
我不是问你从哪里得到这个列表,我问的是你如何找到前 3 个项目?
-
当您说“前 3 项”时,是什么意思?例如,什么规则告诉您
'pencil'、'eraser'和'glue'是第一个列表中的“前 3 项”,而'folders'不是其中之一? -
另外:如果给定的输入列表中没有 3 个可用项目,你应该怎么做?
标签: python python-3.x list nested-lists