POST
|
To add the keys to the dictionary (2nd item of each tuple), try something like this:
lst = [
('id=495, Country=UK', {'emp': ['9999'], 'Code': ['09800']}),
('id=495, Country=UK', {'emp': ['8888'], 'Code': ['09800']}),
]
for t in lst:
d = t[1]
emp = d['emp'][0]
#do whatever you need to here to get the info for the next steps
d['Num'] = ['004']
d['LocCity'] = ['Notts']
#etc
A tuple is immutable, so you won't be able to add a new dict (unless you convert each to a list), but you can add key:value pairs to the existing dict. One thing that bothers me is that all the values in the dict are single item lists. Why not just make them strings and loose the lists? I can think of at least 1 reason why you would need this structure (storing multiple values), but if that is not the case, then this structure just makes things more difficult to get at. good luck Mike Thanks Mike for the reply. Apologies for confusing you. I meant to say I have a new LIST [('key1=1,c=UK', {'Num': ['004'], 'locCity': 'Notts'], 'name': ['Roger'],'uid': [lname.fname@email.com],'Date': [101011]}), ('key1=2,c=UK', {'Num': ['005'], 'locCity': ['Birmi'], 'name': ['Din'],'uid':[lname1.fname1@email.com],'Date': [101111]}), ('key1=3,c=UK', {'Num': ['006'], 'locCity': ['Clift'], 'name': ['Dan'],'uid':[lname2.fname2@email.com],'Date': [111011]})] I have a string with lastname.firstname. With this I should loop thru the above tuples and for the matching entry, I have to store keys like Num, locCity and Date and their values. So I believe it should be a dictionary? so I can use these 3 values later.... Could it be done? Please advise. Like I need to get the field and its value. My list will have around 50 fields in its t[1] and I need to pick few from them(2 or 3) with name, value pairs so I can get those values later.
... View more
11-11-2012
12:50 PM
|
0
|
0
|
277
|
POST
|
Now I need to expand this structure to [('ID1=1,c=UK', {'Num': ['004'], 'locCity': 'Notts'], 'name': ['Roger'],'uid': [lname.fname@email.com],'Date': [101011]}), ('key1=2,c=UK', {'Num': ['005'], 'locCity': ['Birmi'], 'name': ['Din'],'uid':[lname1.fname1@email.com],'Date': [101111]}), ('key1=3,c=UK', {'Num': ['006'], 'locCity': ['Clift'], 'name': ['Dan'],'uid':[lname2.fname2@email.com],'Date': [111011]})] I have a string with lastname.firstname. With this I should loop thru the above tuples and for the matching entry, I have to store keys like Num, locCity and Date and their values. So I believe it should be a dictionary? so I can use these 3 values later.... Could it be done? Please advise.
... View more
11-11-2012
05:01 AM
|
0
|
0
|
277
|
POST
|
This is not a list, but a tuple, and a strange one at that. But that's not unusual, you can run into strange Python data structures from time to time. This one is a tuple with a dictionary as the 2nd item. The dictionary keys are strings, and the values are single-item lists. Strange indeed. Looks like maybe some xml converted to json or something. Anyway, here's how to get the emp number out of it: t = ('id=495, Country=UK', {'emp': ['9999'], 'Code': ['09800']}) emp = t[1]['emp'][0] Now, if you really do have a list of these tuples, then you can search for a particular emp number with a list comprehension like so: lst = [ ('id=495, Country=UK', {'emp': ['9999'], 'Code': ['09800']}), ('id=495, Country=UK', {'emp': ['8888'], 'Code': ['09800']}), ] lst2 = [i for i in lst if i[1]['emp'][0] == '9999'] This will result in a list (lst2) of all tuples where the 'emp' key in the dictionary is a list with the single value of '9999'. If lst2 ends up empty, then no match was found. hope this helps, Mike Thanks Mike Really appreciate it...Will try this code.....TQ.
... View more
11-10-2012
03:51 PM
|
0
|
0
|
277
|
POST
|
Hi, I have a list which looks like ('id=495, Country=UK', {'emp': ['9999'], 'Code': ['09800']}) and I need to check the value of emp number from this list. How do I do that? Basically checking if some other variable is equal to value of employee Id? Thanks.
... View more
11-10-2012
10:35 AM
|
0
|
5
|
680
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:24 AM
|