Posts: 5
Threads: 1
Joined: May 2019
I have a list of ordereddict as follows
OrderedDict([('Numbers', '15'), ('FirstName', 'John'), ('SecondName', 'Raul'), ('MiddleName', 'Kyle')])
OrderedDict([('Names', 'John'), ('NewFirstName', 'Mark'), ('NewSecondName', 'Sachel'), ('NewThirdName', 'Raul')]) i want to search whether the values of Names,NewFirstName,NewSecondName,NewThirdName is present in value of SecondName, if it exists it should return the corresponding Numbers value. How can i do this, is there any builtin function for it?
Posts: 1,950
Threads: 8
Joined: Jun 2018
List of dictionaries makes usually sense when keys are the same. Then it's easy to manipulate data with comprehension.
This structure is prone to errors (what if new dictionaries will be added? new dictionaries with some keys overlapping with existing ones etc). I recommend to think about your needs and determine whether there can be better ways to store data.
It can be done manually, but this is not generalised solution, it works on this particular dataset and may break if changes are made (code assumes that any match will suffice, not all must match):
>>> from collections import OrderedDict
>>> lst = [OrderedDict([('Numbers', '15'), ('FirstName', 'John'), ('SecondName', 'Raul'), ('MiddleName', 'Kyle')]),
... OrderedDict([('Names', 'John'), ('NewFirstName', 'Mark'), ('NewSecondName', 'Sachel'), ('NewThirdName', 'Raul')])]
>>> value_to_search = lst[0]['SecondName']
>>> if value_to_search in lst[1].values():
... print(lst[0]['Numbers'])
...
15
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 5
Threads: 1
Joined: May 2019
I have a list of ordereddict that look like below, these are already sorted
[OrderedDict([('Numbers', '15'), ('FirstName', 'John'), ('SecondName', 'Raul'), ('MiddleName', 'Kyle')]),
OrderedDict([('Names', 'John'), ('NewFirstName', 'Mark'), ('NewSecondName', 'Sachel'), ('NewThirdName', 'Raul')]),
OrderedDict([('Numbers', '25'), ('FirstName', 'Kyle'), ('SecondName', 'Venn'), ('MiddleName', 'Marcus')]),
OrderedDict([('Names', 'Sachel'), ('NewFirstName', 'Venn'), ('NewSecondName', 'Kyle'), ('NewThirdName', 'John')]),
OrderedDict([('Names', 'Kyle'), ('NewFirstName', 'Son'), ('NewSecondName', 'Son'), ('NewThirdName', 'Sachel')]),
OrderedDict([('Numbers', '31'), ('FirstName', 'Son'), ('SecondName', 'John'), ('MiddleName', 'John')]),
OrderedDict([('Numbers', '43'), ('FirstName', 'Marcus'), ('SecondName', 'Venn'), ('MiddleName', 'Venn')]),
OrderedDict([('Numbers', '57'), ('FirstName', 'Sachel'), ('SecondName', 'Sachel'), ('MiddleName', 'Raul')]),
OrderedDict([('Names', 'Moore'), ('NewFirstName', 'Sachel'), ('NewSecondName', 'Kyle'), ('NewThirdName', 'Raul')]),
OrderedDict([('Names', 'Venn'), ('NewFirstName', 'Mark'), ('NewSecondName', 'Kyle'), ('NewThirdName', 'Sachel')]),
OrderedDict([('Names', 'Mark'), ('NewFirstName', 'Mark'), ('NewSecondName', 'John'), ('NewThirdName', 'Mark')]),
OrderedDict([('Numbers', '63'), ('FirstName', 'Kyle'), ('SecondName', 'Marcus'), ('MiddleName', 'Mark')]),
OrderedDict([('Names', 'Son'), ('NewFirstName', 'Venn'), ('NewSecondName', 'Moore'), ('NewThirdName', 'Sachel')]),
OrderedDict([('Numbers', '71`'), ('FirstName', 'Son'), ('SecondName', 'John'), ('MiddleName', 'Moore')]),
OrderedDict([('Names', 'Marcus'), ('NewFirstName', 'Marcus'), ('NewSecondName', 'John'), ('NewThirdName', 'Sachel')]),
OrderedDict([('Numbers', '88'), ('FirstName', 'Marcus'), ('SecondName', 'Mark'), ('MiddleName', 'John')]),
OrderedDict([('Names', 'Raul'), ('NewFirstName', 'John'), ('NewSecondName', 'Venn'), ('NewThirdName', 'Moore')]),
OrderedDict([('Numbers', '94'), ('FirstName', 'John'), ('SecondName', 'Marcus'), ('MiddleName', 'Moore')]),
OrderedDict([('Numbers', '101'), ('FirstName', 'Kyle'), ('SecondName', 'Venn'), ('MiddleName', 'Mark')]),
OrderedDict([('Names', 'Ellen'), ('NewFirstName', 'Raul'), ('NewSecondName', 'Venn'), ('NewThirdName', 'Sachel')])] For each item that contains the Names key i want to take the value of it and check the existence of it in SecondName of the item which has Numbers key in it and append that as value to it.
**For Example:** First item did not have Names key, so i need to go to second line which has a Names key with the value John . Take that and search for value John in SecondName key in all the items that has Numbers key in it, here Numbers 31 and Numbers 71 has that. So the new list of ordereddict should have these values appended in it as follows
OrderedDict([('Names', 'John(31,71)'), ('NewFirstName', 'Mark'), ('NewSecondName', 'Sachel'), ('NewThirdName', 'Raul')]) if the John does not exist in any of the items that has Numbers key in it, John should simply be appended with the value of the Numbers key that exists before it with * appended
The above process should work for the NewFirstName , NewSecondName , NewThirdName as well with respect to SecondName
Mark exists as SecondName in Number 88 and Sachel exists as SecondName in Number 57 and Raul as 15 so the new list of ordereddict would contain this, the complete expected new list of ordereddict is as below
[OrderedDict([('Numbers', '15'), ('FirstName', 'John'), ('SecondName', 'Raul'), ('MiddleName', 'Kyle')]),
OrderedDict([('Names', 'John(31,71)'), ('NewFirstName', 'Mark(88)'), ('NewSecondName', 'Sachel(57)'), ('NewThirdName', 'Raul(15)')]),
OrderedDict([('Numbers', '25'), ('FirstName', 'Kyle'), ('SecondName', 'Venn'), ('MiddleName', 'Marcus')]),
OrderedDict([('Names', 'Sachel(57)'), ('NewFirstName', 'Venn(25,43,101)'), ('NewSecondName', 'Kyle(25*)'), ('NewThirdName', 'John(31,71)')]),
OrderedDict([('Names', 'Kyle(25*)'), ('NewFirstName', 'Son(63*)'), ('NewSecondName', 'Son(63*)'), ('NewThirdName', 'Sachel(57)')]),
OrderedDict([('Numbers', '31'), ('FirstName', 'Son'), ('SecondName', 'John'), ('MiddleName', 'John')]),
OrderedDict([('Numbers', '43'), ('FirstName', 'Marcus'), ('SecondName', 'Venn'), ('MiddleName', 'Venn')]),
OrderedDict([('Numbers', '57'), ('FirstName', 'Sachel'), ('SecondName', 'Sachel'), ('MiddleName', 'Raul')]),
OrderedDict([('Names', 'Moore(57*)'), ('NewFirstName', 'Sachel(57)'), ('NewSecondName', 'Kyle(25*)'), ('NewThirdName', 'Raul(15)']),
OrderedDict([('Names', 'Venn(25,43,101)'), ('NewFirstName', 'Mark(88)'), ('NewSecondName', 'Kyle(25*)'), ('NewThirdName', 'Sachel(57)')]),
OrderedDict([('Names', 'Mark(88)'), ('NewFirstName', 'Mark(88)'), ('NewSecondName', 'John(31,71)'), ('NewThirdName', 'Mark(88)')]),
OrderedDict([('Numbers', '63'), ('FirstName', 'Kyle'), ('SecondName', 'Marcus'), ('MiddleName', 'Mark')]),
OrderedDict([('Names', 'Son(63*)'), ('NewFirstName', 'Venn(25,43,101)'), ('NewSecondName', 'Moore(57*)'), ('NewThirdName', 'Sachel(57)')]),
OrderedDict([('Numbers', '71`'), ('FirstName', 'Son'), ('SecondName', 'John'), ('MiddleName', 'Moore')]),
OrderedDict([('Names', 'Marcus(94)'), ('NewFirstName', 'Marcus(94)'), ('NewSecondName', 'John(31,71)'), ('NewThirdName', 'Sachel(57)')]),
OrderedDict([('Numbers', '88'), ('FirstName', 'Marcus'), ('SecondName', 'Mark'), ('MiddleName', 'John')]),
OrderedDict([('Names', 'Raul(15)'), ('NewFirstName', 'John(31,71)'), ('NewSecondName', 'Venn(25,43,101)'), ('NewThirdName', 'Moore(57*)')]),
OrderedDict([('Numbers', '94'), ('FirstName', 'John'), ('SecondName', 'Marcus'), ('MiddleName', 'Moore')]),
OrderedDict([('Numbers', '101'), ('FirstName', 'Kyle'), ('SecondName', 'Venn'), ('MiddleName', 'Mark')]),
OrderedDict([('Names', 'Ellen(101*)'), ('NewFirstName', 'Raul(15)'), ('NewSecondName', 'Venn(25,43,101)'), ('NewThirdName', 'Sachel(57)'])] To achieve this, i started of with the following code, but i feel like going nowhere
for i in list1:
if 'Names' in i.keys():
if 'John' in i.values():
print(i.items()) Please let me know the right pythonic way of achieving the expected result
Posts: 12,030
Threads: 485
Joined: Sep 2016
May-02-2019, 04:41 PM
(This post was last modified: May-02-2019, 04:42 PM by Larz60+.)
This is really a continuation of your previous thread, and should have been added to that thread.
This might squeeze by as a new topic, but very iffy.
In the future, please know that this is against forum rules.
Posts: 5
Threads: 1
Joined: May 2019
(May-02-2019, 04:41 PM)Larz60+ Wrote: This is really a continuation of your previous thread, and should have been added to that thread.
This might squeeze by as a new topic, but very iffy.
In the future, please know that this is against forum rules.
Oops, sorry for that. Will take a note of it
Posts: 5
Threads: 1
Joined: May 2019
Can someone help me on this please
Posts: 5
Threads: 1
Joined: May 2019
I have a list of ordereddict as follows
list1= [OrderedDict([('Numbers', '15'), ('FirstName', 'John'), ('SecondName', 'Raul'), ('MiddleName', 'Kyle')]),
OrderedDict([('Names', 'John'), ('NewFirstName', 'Mark'), ('NewSecondName', 'Sachel'), ('NewThirdName', 'Raul')]),
OrderedDict([('Numbers', '25'), ('FirstName', 'Kyle'), ('SecondName', 'Venn'), ('MiddleName', 'Marcus')]),
OrderedDict([('Names', 'Sachel'), ('NewFirstName', 'Venn'), ('NewSecondName', 'Kyle'), ('NewThirdName', 'John')])] I would like to edit some of the values in it by picking the value from another list
list2= [('John', ['1023', '1235']), ('Venn', ['618']), ('Marcus', ['909']), ('Kyle', ['109', '1233', '987']), ('Raul', '5*'), ('Sachel', '10*')]
Expected Output:
From list2 i need to pick the value of John i.e 1023,1235 and paste that next to John in list1 wherever present , so the list1 would become
list1= [OrderedDict([('Numbers', '15'), ('FirstName', 'John(1023,1235)'), ('SecondName', 'Raul'), ('MiddleName', 'Kyle')]),
OrderedDict([('Names', 'John(1023,1235)'), ('NewFirstName', 'Mark'), ('NewSecondName', 'Sachel'), ('NewThirdName', 'Raul')]),
OrderedDict([('Numbers', '25'), ('FirstName', 'Kyle'), ('SecondName', 'Venn'), ('MiddleName', 'Marcus')]),
OrderedDict([('Names', 'Sachel'), ('NewFirstName', 'Venn'), ('NewSecondName', 'Kyle'), ('NewThirdName', 'John(1023,1235)')])]
Posts: 12,030
Threads: 485
Joined: Sep 2016
Next time you post a new thread for a topic that you already have a running thread for, you will get a warning.
I have mentioned this before.
|