# 如果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]
``````

``````result=[number for number in numbers if number]
``````

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

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

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

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

``````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]
``````

### 解决方法

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

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

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

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

### 问题

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)`.