简体   繁体   English

从python中的19个列表中制作一个列表

[英]making a single list from 19 lists in python

I have 19 big lists of numbers in python and want to make a new list from all of them. 我在python中有19个大数字列表,并希望从所有这些列表中新建一个列表。 so at the end I would have one list. 所以最后我会有一个清单。 I made the following code for two lists and works perfectly: 我为两个列表制作了以下代码,并且效果很好:

a2 = [x for x in l2 if x+3 in l1] 

this code looks for the elements in two lists only if the difference between them is 3. which means it finds any element in l1 if there is an element in l2 which is 3 units bigger than that element in l1 and the new list would have the mentioned elements of l1 该代码仅在两个列表中的元素之差为3时才查找它们。这意味着,如果l2中的元素比l1中的元素大3个单位,则它将在l1中找到任何元素。 l1提到的要素

for example if l1 = [1,2,3,6,5,7,8,9] and l2 = [4, 6,10,11,13,14] 例如,如果l1 = [1,2,3,6,5,7,8,9]l2 = [4, 6,10,11,13,14]

new = [1,3,7,8]

but I don't know how to include the rest of lists. 但我不知道如何包括其余列表。 so, instead of looking for the elements in l2 , it looks for the "3 units bigger elements than l1 " in l 2 .... l19 and returns the 3 units smaller element in l1 (the the new list). 因此,与其在l2中寻找元素,不如在l 2 .... l19 “比l1大3个单位的元素” 2 .... l19并返回l1的3个小单位的元素(新列表)。 do you know how to do that in a single line? 你知道怎么一行吗?

Put all of your other lists into a larger list and combine into one set using set.union : 将所有其他列表放入一个较大的列表中,并使用set.union合并为一组:

superlist = [l3, ..., l19]
s = set(l2).union(*superlist)
a2 = [x for x in l1 if x+3 in s]

You can, of course, decide to pass the lists directly to set.union . 当然,您可以决定将列表直接传递给set.union

Membership lookup with sets - O(1) complexity - is way faster than that for lists - O(n). 具有集合的成员资格查找-O(1)复杂度-快于列表的成员查找-O(n)。

a2 = [x for x in l if x+3 in l1+l2+l3+l4+l5+...+l19]

For the sake of completeness, here also another one-liner inspired by the other answers you can use in case of more than 19 lists: 为了完整起见,这里还有另外一种方法,它受其他答案启发,在超过19个列表的情况下可以使用:

a2=[x for x in l if x+3 in set().union(*[eval('l'+str(i+1)) for i in range(19)])]

but ... 但是...

ATTENTION 注意

such kind of one-liner look very nice, but are deadly in case of really large lists ( they can unnecessary take all of your CPU-power for extremely looooong time ), so you will be glad to use a multi-liner instead: 这样的单行代码看起来非常好,但是在列表很大的情况下是致命的它们可能在非常漫长的时间内不必要地占用您的所有CPU能力 ),因此您将很高兴使用多行代码:

bigL = l1+l2+l3+l4+l5+...+l19
setBigL = set(bigL)
a2 = [x for x in l if x+3 in setBigL]

For the sake of completeness here how to get bigL without the need of writing all the list names: 为了完整起见,在不编写所有列表名称的情况下如何获取bigL

noOfLists = 19; strListAddition = ''
for i in range(19): strListAddition+='l'+str(i+1)+'+'
bigL = eval(strListAddition[0:-1])

If I don't misunderstand your meaning, you can try this, and if you hava 19 list, just change the range part: 如果我不误解您的意思,可以尝试一下,如果您有19个列表,只需更改range部分:

l1 = [4, 6,10,11,13,14]
l2 = [1,2,3,6,5,7,8,9]
l3=[3,7,10,11]

print([x for i in range(2,4) for x in eval('l'+str(i)) if x+3 in l1])

Output: 输出:

[1, 3, 7, 8, 3, 7, 10, 11]

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

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