首页 > python > 如果m迭代,如何获取字典以及如何在i中为i打破

如果m迭代,如何获取字典以及如何在i中为i打破 (How to get dictionary and how to break from i for i in m if m iteration)

2014-08-17 python

问题

从列表变量开始:

number=[1,2,3,None,4,5]

我可以采用两种不同的方法得到相同的结果:

方法1:

result=[number for number in numbers if number]

方法2:

result=[]
for number in numbers:
    if not number: continue
    result.append(number)

除了将更多代码放在一起之外,方法2的执行速度似乎要慢30%。由于方法1在速度和清晰度部门都获胜,我想更频繁地使用它。

两个问题:

问题1:

我可以使用[i for i in m if i]方法来构建字典变量吗?所以,如果我从列表变量开始:

number=[1,2,3,None,4,5]

如何进入字典,其中每个列表变量号都成为字典键:例如:

result={1:None, 2:None, 3:None, 4:None, 5:None}

甚至:

result={1:{}, 2:{}, 3:{}, 4:{}, 5:{}}

问题2:

如果满足某个条件,我break可以[i for i in m if i]迭代吗?例如:

result=[]
for number in numbers:
    if not number: continue
    if number==5: 
        result.append(number)
        break

显然我不能这样说:

result=[number for number in numbers if number and number==5 break]

第三个问题:[i for i in m if i]迭代的输出 总是列表吗?如果没有,请发一些例子。

解决方法

问题1:你可以这样做:

result = {i: None for i in number if i}

要么:

result = {i: {} for i in number if i}

问题2:您可以使用itertools.takewhile此:

result = [i for i in itertools.takewhile(lambda n: bool(n), number)]

问题3:如果你这样写,那么是的。但如果你写,(i for i in number if i)那么它不是一个列表。在这种情况下,它是一个生成器表达式:

>>> t = (i for i in number if i)
>>> t
<generator object <genexpr> at 0x106039dc0>

这意味着您可以将其用于iteration(for item in t:)而无需构建列表。您也可以通过调用将其转换回列表list(t)

问题

Starting with a list variable:

number=[1,2,3,None,4,5]

I can get the same result taking two different approaches:

Approach 1:

result=[number for number in numbers if number]

Approach 2:

result=[]
for number in numbers:
    if not number: continue
    result.append(number)

Approach 2 aside from taking more code to put together appears to be 30% slower to execute. Since Approach 1 wins in both the speed and clarity department I would like to utilize it more often.

Two questions:

Question 1:

Can I use [i for i in m if i] approach to build a dictionary variable? So if I start with a list variable :

number=[1,2,3,None,4,5]

how can I get to a dictionary where every list variable number becomes a dictionary key: such as:

result={1:None, 2:None, 3:None, 4:None, 5:None}

or even:

result={1:{}, 2:{}, 3:{}, 4:{}, 5:{}}

Question 2:

Can I break from [i for i in m if i] iteration if a certain condition met? For example:

result=[]
for number in numbers:
    if not number: continue
    if number==5: 
        result.append(number)
        break

Apparently I can't put it like this:

result=[number for number in numbers if number and number==5 break]

And third bonus question: Is the output of [i for i in m if i] iteration always a list? If not please post some examples.

解决方法

Question 1: you can do:

result = {i: None for i in number if i}

Or:

result = {i: {} for i in number if i}

Question 2: you can use itertools.takewhile for this:

result = [i for i in itertools.takewhile(lambda n: bool(n), number)]

Question 3: if you write it like that then yes. But if you write (i for i in number if i) then it's not a list. In that case it's a generator expression:

>>> t = (i for i in number if i)
>>> t
<generator object <genexpr> at 0x106039dc0>

This means you can use it for iteration (for item in t:) without constructing the list. You can also convert it back to a list by calling list(t).

相似信息