Doh, the first example should of cours be:
b = []
for x in a:
if len(x) > 4:
b.append(x)
--
http://mail.python.org/mailman/listinfo/python-list
Because you're inserting items into your existing list instead of a new
list. What you probably mean is:
b = []
for x in a:
b.append(x)
which creates a new list b that contains all elements whose length is
greater than four.
A better way to write this would be:
b = [x for x in a if l
Shi Mu wrote:
> In the follwoing code,
> Why the result is "'defenestrate', 'window', 'defenestrate', " before
> the original list instead of 'defenestrate', 'window', ?
>
>
a = ['defenestrate', 'cat', 'window', 'defenestrate']
for x in a[:]:
>
> ... if len(x) > 4: a.insert(0, x)
> ...
27;defenestrate']
>>> for x in a[:]:
... if len(x) > 4: a.insert(0, x)
...
>>> a
['defenestrate', 'window', 'defenestrate', 'defenestrate', 'cat',
'window', 'defenestrate']
On 10/25/05, Fred
Shi Mu wrote:
>I can not understand the use of "cell" and "row" in the code:
>
> # convert the matrix to a 1D list
> matrix = [[13,2,3,4,5],[0,10,6,0,0],[7,0,0,0,9]]
> items = [cell for row in matrix for cell in row]
> print items
working through the Python
I can not understand the use of "cell" and "row" in the code:
# convert the matrix to a 1D list
matrix = [[13,2,3,4,5],[0,10,6,0,0],[7,0,0,0,9]]
items = [cell for row in matrix for cell in row]
print items
--
http://mail.python.org/mailman/listinfo/python-list