Re: Merging two lists of data (Pythonic way)

2006-02-17 Thread SMB

Jonathan Gardner [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 codes = map(lambda x: x[0], list1)
 for d in list2:
  if d['code'] in codes:
d['VERIFIED'] = 1

 Is this what you were looking for?


Actually, this is not exactly what I was looking for.  I failed to realize 
what map did, in this case using the labmda x: x[0] on each item of the list 
returning a list of the key numbers.  The problem is that the changes made 
to my dictionary (in my example dictionary[VERIFIED]=1) needs to be based 
on the other values in each list in LIST1.

In my example LIST1 was defined as
[[5L, 1L, 1L, 'Personal Information'], [14L, 2L, 1L, '']]

So, the dictionary changes would be involving mylist[1:] for each mylist in 
LIST1 based on the comparison to mylist[0]...

Thank again 


-- 
http://mail.python.org/mailman/listinfo/python-list


Merging two lists of data (Pythonic way)

2006-02-17 Thread SMB
I have two lists of data like the following:

LIST1
 [[5L, 1L, 1L, 'Personal Information'], [14L, 2L, 1L, '']]

LIST2
[{'code': 5L, 'name': 'First Name', 'value': [''], 'label': 'First Name', 
'width': 0L, 'separator': ',', 'height': 0L, 'type': 2L, 'order': 1L}, 
{'code': 14L, 'name': 'Last Name', 'value': [''], 'label': 'Last Name', 
'width': 0L, 'separator': ',', 'height': 0L, 'type': 2L, 'order': 2L}, 
{'code': 16L, 'name': 'Job Title', 'value': [], 'label': 'Job Title', 
'width': 40L, 'separator': '', 'height': 0L, 'type': 2L, 'order': 3L}]

The important comparison values here are list[0] for each list in LIST1 and 
dictionary[code] for each dictionary in LIST2.

What I am doing is looking for a pythonic way to parse the LIST2 code 
values and make appropriate changes to to each dictionary if the code 
value is the first element of a list in LIST1.

The final result may look like this given that the change is adding a new 
key/value of VERIFIED/1 for the matches.
LIST3
 [{'VERIFIED':1,'code': 5L, 'name': 'First Name', 'value': [''], 'label': 
'First Name', 'width': 0L, 'separator': ',', 'height': 0L, 'type': 2L, 
'order': 1L}, {'VERIFIED':1,'code': 14L, 'name': 'Last Name', 'value': [''], 
'label': 'Last Name', 'width': 0L, 'separator': ',', 'height': 0L, 'type': 
2L, 'order': 2L}, {'code': 16L, 'name': 'Job Title', 'value': [], 'label': 
'Job Title', 'width': 40L, 'separator': '', 'height': 0L, 'type': 2L, 
'order': 3L}]

I know I could do this with two for loops, but am looking for a better 
solution maybe involving list comprehension.

Any pointers are appreciated.
Thanks 


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Merging two lists of data (Pythonic way)

2006-02-16 Thread SMB

Jonathan Gardner [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 codes = map(lambda x: x[0], list1)
 for d in list2:
  if d['code'] in codes:
d['VERIFIED'] = 1

 Is this what you were looking for?


That is exactly what I was looking for.  I will have to take a look at map.

Thank you very much 


-- 
http://mail.python.org/mailman/listinfo/python-list