简体   繁体   English

谁能帮我优化这段代码?

[英]Can anyone help me to optimize this piece of code?

I want to store even number and odd number in a separate list.我想将偶数和奇数存储在单独的列表中。 But, here I am facing a unique problem.但是,在这里我面临一个独特的问题。 I am able to store it in sets but not in lists.我可以将它存储在集合中,但不能存储在列表中。 Is there a way wherein I can store these in a List without repetition.有没有一种方法可以将这些存储在列表中而不重复。

I have tried this in Jupyter notebook我在 Jupyter 笔记本中试过这个

list_loop=[1,2,3,4,5,6,7,8,9,10,11,12,13,1,4,1,51,6,17,]
for i in list_loop:
    if i % 2 == 0 :
        list_even = list_even + [i]
    else:
        list_odd = list_odd + [i]
print(set(list_even))
print(set(list_odd))

Expected output:预期输出:

[2,4,6,8,10,12]
[1,3,5,7,9,11,13,17,51]

You can apply the list function to your set object in order to convert it to a list.您可以将list函数应用于您的set对象,以便将其转换为列表。

list_from_set = list(set(list_even))
>>> print(list_from_set)
[2, 4, 6, 8, 10, 12]

There are a couple of ways you could do this.有几种方法可以做到这一点。 You could use the OrderedDict in the collections library, or you could just sort the set and get a list,您可以使用集合库中的OrderedDict ,或者您可以对集合进行排序并获得一个列表,

...
print(sorted(set(list_even)))
print(sorted(set(list_odd)))

Also, I would personally create those lists using a set comprehension此外,我会亲自使用集合理解来创建这些列表

list_even = sorted({x for x in list_loop if x % 2 == 0})
list_odd = sorted({x for x in list_loop if x % 2 == 1})

Use a comprehension使用理解

>>> list_loop=[1,2,3,4,5,6,7,8,9,10,11,12,13,1,4,1,51,6,17,]
>>> print(list(set(_ for _ in list_loop if _ % 2)))
[1, 3, 5, 7, 9, 11, 13, 17, 51]

Similarly for even numbers.对于偶数也是如此。

Define list_odd and list_even as lists and don't convert them to sets before printing.list_oddlist_even定义为列表,并且在打印前不要将它们转换为集合。 Note that you can use list comprehension to fill list_odd and list_even :请注意,您可以使用列表理解来填充list_oddlist_even

list_odd = []
list_even = []

list_loop=[1,2,3,4,5,6,7,8,9,10,11,12,13,1,4,1,51,6,17,]
list_odd = [elem for elem in list_loop if elem % 2 != 0]
list_even = [elem for elem in list_loop if elem % 2 == 0]

print(list_even)
print(list_odd)

Output:输出:

[2, 4, 6, 8, 10, 12, 4, 6]
[1, 3, 5, 7, 9, 11, 13, 1, 1, 51, 17]

Edit: for uniqueness, turn list_loop into a set:编辑:为了唯一性,将list_loop变成一个集合:

list_loop=set([1,2,3,4,5,6,7,8,9,10,11,12,13,1,4,1,51,6,17,])

Output:输出:

[2, 4, 6, 8, 10, 12]
[1, 3, 5, 7, 9, 11, 13, 17, 51]

You can solve this using a list comprehension with a filter condition - but you then iterate your list twice.您可以使用带有过滤条件的列表理解来解决此问题 -您随后将列表迭代两次。

By using a simple for loop you only need to touch any number once at it will conserve the original order - what putting your numbers through a set might not do - order in a set is not guaranteed:通过使用一个简单的for循环,你只需要触摸任何数量一旦它会保存原始订单-什么通过一组把你的号码也许不会那么做-为了在一组不保证:

Keep a set of seen numbers, only add anything if your current number was not yet seen.保留一组seen数字,如果您当前的数字还没有看到,则只添加任何内容。

list_loop = [1,2,3,4,5,6,7,8,9,10,11,12,13,1,4,1,51,6,17,]

list_even = []
list_odd = [] 
seen = set()

trick = [list_even, list_odd]  # even list is at index 0, odd list at index 1

for i in list_loop:
    if i in seen: 
        continue
    else:
        seen.add(i)
                                 # the trick eliminates the need for an if-clause
        trick[i%2].append(i)     # you use i%2 to get either the even or odd index


print(list_even)
print(list_odd)

Output:输出:

[2, 4, 6, 8, 10, 12]
[1, 3, 5, 7, 9, 11, 13, 51, 17]

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

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