【发布时间】:2020-07-08 02:05:07
【问题描述】:
我有多个长度相等的列表,我想比较这些列表以便在列表字典中附加一些单词。我已经比较了这些列表以获得每个索引的最大数量。
maxlist = [2,9,6,4,8] #This is the list of max number from the three different lists
a_list = [2,1,4,2,8]
b_list = [1,9,6,3,4]
c_list = [0,3,2,4,1]
现在,我有另一个相同长度的单词列表:
words = ["boy", "girl", "git", "tall", "boss"]
我在这里要做的是将每个列表与 maxlist 进行比较,如果在同一索引的三个列表中的任何一个中找到 maxlist 中的数字,我想创建一个列表字典,将单词附加到那个特定的列表。所以我的最终结果将是:
对于索引 0,maxlist 是在 a_list 中找到的,所以我会有:
{a_list: ["boy"]}
对于索引 1,在 b_list 中找到了 maxlist,所以我会有:
{a_list: ["boy"], b_list: ["girl"]}
在将所有列表与 maxlist 进行比较后,我想要:
{a_list: ["boy", "boss"], b_list:["girl","git"], c_list: ["tall"]}
我在这里以三个列表为例,但在我的例子中,我有 40 个列表。有没有有效的方法来实现这一点?我目前被卡住了。这是我目前正在编写的代码:
label_data = {}
for i in range(len(maxlist)):
if maxlist[i] > 1: #I don't want to consider a max of 1.
if maxlist == a_list[i]:
if a_list in label_data:
label_data["a_list"].append(words[i])
else:
dates_dict["key"] = [words[i]]
不确定上面的代码是否能正常工作,另外我必须继续为所有列表构建 if 函数。有什么有效的方法可以解决这个问题,请发布您的代码。
谢谢
【问题讨论】:
标签: python