Assign values from list to list of instances

G

Gnarlodious

I want to assign a list of variables:
locus=[-2, 21, -10, 2, 12, -11, 0, 3]

updating a list of objects each value to its respective instance:

for order in range(len(Orders)):
Orders[order].locus=locus[order]

This works, even though it reads like doggerel. Is there a more
pythonesque way using map or comprehension?

-- Gnarlie
 
P

Peter Otten

Gnarlodious said:
I want to assign a list of variables:
locus=[-2, 21, -10, 2, 12, -11, 0, 3]

updating a list of objects each value to its respective instance:

for order in range(len(Orders)):
Orders[order].locus=locus[order]

This works, even though it reads like doggerel. Is there a more
pythonesque way using map or comprehension?

for order, place in zip(Orders, locus):
order.locus = place
 
D

Dave Angel

I want to assign a list of variables:
locus=[-2, 21, -10, 2, 12, -11, 0, 3]

updating a list of objects each value to its respective instance:

for order in range(len(Orders)):
Orders[order].locus=locus[order]

This works, even though it reads like doggerel. Is there a more
pythonesque way using map or comprehension?

-- Gnarlie


You can do that with the enumerate function, or with zip
for index, instance in enumerate(Orders):
instance.locus = locus[index]

for instance, place in zip(Orders, locus):
instance.locus = locus[index]

It would be clearer if you used singular for individual items, and
plural for the collections,

loci [-2, 21, ....
orders = [list of order objects ...]

for order, locus in zip(orders, loci):
order.locus = locus
 
U

Ulrich Eckhardt

Am 01.11.2011 16:05, schrieb Gnarlodious:
I want to assign a list of variables:
locus=[-2, 21, -10, 2, 12, -11, 0, 3]

updating a list of objects each value to its respective instance:

for order in range(len(Orders)):
Orders[order].locus=locus[order]

This works, even though it reads like doggerel. Is there a more
pythonesque way using map or comprehension?

Use enumerate:

for object, index in enumerate(Orders):
object.locus = locus[index]


I'm not 100% I understood your description though, but it looks like
this would do what you want and be descriptive at the same time.

Uli
 
T

Terry Reedy

I want to assign a list of variables:
locus=[-2, 21, -10, 2, 12, -11, 0, 3]

updating a list of objects each value to its respective instance:

for order in range(len(Orders)):
Orders[order].locus=locus[order]

This works, even though it reads like doggerel. Is there a more
pythonesque way using map or comprehension?

for obj, val in zip(Orders, locus):
obj.locus = val


I'm not sure how worthwhile it is converting the above to a list
comprehension (when the list would just be thrown away). Having said
that the call to zip creates an unnecessary list.

Not in Py 3
 

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
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top