简体   繁体   English

将列表与列表列表进行比较

[英]Comparing list with a list of lists

I have a list string_array = ['1', '2', '3', '4', '5', '6'] and a list of lists multi_list = [['1', '2'], ['2', '3'], ['2', '4'], ['4', '5'], ['5', '6']]我有一个列表string_array = ['1', '2', '3', '4', '5', '6']和一个列表multi_list = [['1', '2'], ['2', '3'], ['2', '4'], ['4', '5'], ['5', '6']]

The first element of each sub-list in multi_list will have an associated entry in string_array . multi_list中每个子列表的第一个元素将在string_array有一个关联条目。

(Sublists will not have duplicate elements) (子列表不会有重复的元素)

How do I map these associated elements into a dictionary like:如何将这些关联元素映射到字典中,例如:

{
'1': ['2'],
'2': ['3', '4'],
'3': [],
'4': ['5'],
'5': ['6'],
'6': []
}

Here's a few concepts that will help you, but I'm not going to give you the complete solution.这里有一些概念可以帮助您,但我不会为您提供完整的解决方案。 You should do so on your own to sink in the concepts.您应该自己这样做以深入了解概念。

To make an empty dictionary of lists制作一个空的列表字典

{
    '1': [],
    '2': [],
    '3': [],
    '4': [],
    '5': [],
    '6': [],
}

you can use a for loop:您可以使用 for 循环:

list_one = ['1', '2', '3', '4', '5', '6']

my_dict = {}
for value in list_one:
    my_dict[value] = []

You could also get fancy and use a dictionary comprehension:你也可以花哨并使用字典理解:

my_dict = {value: [] for value in list_one}

Now you'll need to loop over the second list and append to the current list.现在您需要遍历第二个列表append加到当前列表。 eg to append to a list you can do so in a few ways:例如,要附加到列表中,您可以通过以下几种方式执行此操作:

a = [1,2]
b = [3,4]
c = 5

# add a list to a list
a += b
# now a = [1,2,3,4]

# add a list to a list
b.append(c)
# now b = [3,4,5]

To chop up lists you can use slice notation:要切碎列表,您可以使用切片表示法:

a = [1,2,3,4]
b = a[:2]
c = a[2:]
# now b = [1,2], and c = [3,4]

And to access an item in a dictionary, you can do so like this:要访问字典中的项目,您可以这样做:

a = { '1': [1,2,3] }
a['1'] += [4,5,6]
# now a = { '1': [1,2,3,4,5,6] }
flat = ['1', '2', '3', '4', '5', '6']
nested = [['1', '2'], ['2', '3'], ['2', '4'], ['4', '5'], ['5', '6']]

result = {key: [] for key in flat}

for key in result:
    result[key] = [nest[1] for nest in nested if key == nest[0]]


#{'1': ['2'], '2': ['3', '4'], '3': [], '4': ['5'], '5': ['6'], '6': []}

Using your flat list elements as keys, you can generate a dictionary and use empty lists are the values.使用平面列表元素作为键,您可以生成字典并使用空列表作为值。 Empty strings or None would also work if using list comprehension to generate the values later.如果稍后使用列表理解来生成值,空字符串或None也可以使用。

Then just iterate over the keys in the dictionary and your nested list to append the results to the appropriate keys.然后只需遍历字典和嵌套列表中的键,将结果附加到适当的键。

Furthermore;此外;

You can reduce the above down to a single line outputting the same result.您可以将上面的内容减少到输出相同结果的一行。

result = {key: [nest[1] for nest in nested if key == nest[0]] for key in flat}

Output:输出:

{'1': ['2'], '2': ['3', '4'], '3': [], '4': ['5'], '5': ['6'], '6': []}

EDIT:编辑:

After assessing Juanpa's comment and testing the efficiency of the above code, it is clear there is much better ways to run this when encountering large data sets.在评估了 Juanpa 的评论并测试了上述代码的效率后,很明显在遇到大数据集时有更好的方法来运行它。

Comment for reference:评论供参考:

This solution is O(N*M) where N and M are the sizes of flat and nested.这个解决方案是 O(N*M),其中 N 和 M 是平面和嵌套的大小。 You can do this in O(MAX(N, M)).你可以在 O(MAX(N, M)) 中做到这一点。 Basically, loop over nested instead of result, and do for a,b in nested: result[a].append(b)基本上,循环嵌套而不是结果,并在嵌套中执行 a,b:result[a].append(b)

Running the above code, 1000 times, with 1000 elements in the flat list and 1000 nested lists in nested.. the run time is;运行上面的代码,1000 次,平面列表中有 1000 个元素,嵌套中有 1000 个嵌套列表。运行时间是;

print(t.timeit(1000))
39.37227249999978

However running it with the below code boasts a much more efficient run time.然而,使用下面的代码运行它拥有更高效的运行时间。

print(j.timeit(1000))
0.17638869999973394

Code:代码:

result = {key: [] for key in flat}
for a, b in nested:
    if a in result:
        result[a].append(b)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM