siple for in expression

M

Matko

Hello!

Can someone help me to understand the following code:

uv_face_mapping = [[0,0,0,0] for f in faces]

Thank You very much!

Matko from Croatia
 
M

Mike Driscoll

Hello!

Can someone help me to understand the following code:

uv_face_mapping = [[0,0,0,0] for f in faces]

Thank You very much!

Matko from Croatia

That looks like a list comprehension. It basically creates a list by
iterating over some kind of collection. See the following sites for
more info:

http://docs.python.org/tutorial/datastructures.html#list-comprehensions
http://www.secnetix.de/~olli/Python/list_comprehensions.hawk
http://www.network-theory.co.uk/docs/pytut/ListComprehensions.html

Mike
 
M

Mark Wooding

Matko said:
Can someone help me to understand the following code:

uv_face_mapping = [[0,0,0,0] for f in faces]

It constructs a fresh list, with the same number of elements as are in
the iterable object referred to by `faces', and where each element is a
distinct list of four zero-valued integer objects; it then makes
uv_face_mapping be a name for this list.

(This is therefore not the same as

uv_face_mapping = [[0, 0, 0, 0]] * len(faces)

which constructs a list, each of whose elements is (well, refers to) the
/same/ list of four zero-valued integers.)

-- [mdw]
 
A

alex23

Can someone help me to understand the following code:
uv_face_mapping = [[0,0,0,0] for f in faces]

As others have mentioned, this is a list comprehension, which is a
simpler way of writing the following:

uv_face_mapping = []
for f in faces:
uv_face_mapping.append([0,0,0,0])

Which seems to be setting a default uv_face_mapping for each element
in faces :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,213
Latest member
DonnellTol

Latest Threads

Top