Assign values from list to list of instances
duncan smith
buzzard at urubu.freeserve.co.uk
Tue Nov 1 11:37:20 EDT 2011
On 01/11/11 15:05, Gnarlodious wrote:
> 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. You could use izip or
maybe,
for i, val in enumerate(locus):
Orders[i].locus = val
Duncan
More information about the Python-list
mailing list