Devbrath_R
Devbrath_R

Reputation: 3

Dividing a dictionary into a list of smaller dictionary

I have extracted data from a table, but its not a regular table made with TR/TD tags but made with DIV and SPAN. I have got my data in a dictonary

{'ab1911': '2', 'ab1912': '1', 'ab1938': '1', 'ab1939': '0','ab1965': '18', 'ab1966': '12', 'ab1993': '18', 'ab1994': '10','ab2021': '5', 'ab2022': '21', 'ab2049': '4', 'ab2050': '4'}.

here the keys of the dictionary are unique ID of the element and are dynamic in nature. Every reload the ID changes.

Actual data in the web table is like this

Revision | Iteraion
   2          1
   1          0
   18         12
   18         10
   5          21
   4          4

What I have to is select the row with max Revision, if the Revision is same then the one with max Iteration. Here the right answer will be Revision 18 and Iteration 12

What I am thinking is If I can convert the dictionary into a list of smaller dictionary like this

[{'ab1911': '2', 'ab1912': '1'},{'ab1938': '1', 'ab1939': '0'},{'ab1965': '8', 'ab1966': '18'},{'ab1993': '18', 'ab1994': '10'},{'ab2021': '5', 'ab2022': '21'},{'ab2049': '4', 'ab2050': '4'}]

and then compare the first key value of each element of the list and find the max value. After getting the value, with key(its ID of selector) I can select the max value from the table. So how can I convert the dictionary into the list of smaller dictionary

Upvotes: 0

Views: 51

Answers (2)

Ken N
Ken N

Reputation: 76

Assuming you want to accomplish the task exactly as you described it with no other considerations about cleanliness or portability, the following code generates the array you want to see:

dict = {'ab1911': '2', 'ab1912': '1', 'ab1938': '1', 'ab1939': '0','ab1965': '18', 'ab1966': '12', 'ab1993': '18', 'ab1994': '10','ab2021': '5', 'ab2022': '21', 'ab2049': '4', 'ab2050': '4'}
arr = []
subdict = {}

itr = 0
for key,val in dict.items():
    subdict.update({key : val})
    itr = itr+1
    if(itr == 2):
        itr = 0
        arr.append(subdict)
        subdict = {}

print(arr)

Upvotes: 0

yongjieyongjie
yongjieyongjie

Reputation: 893

Solution from Official Documentation

If you refer to the Itertools Recipes on the official documentation, there is an example of iteration by chunks of n. We can iterate over the keys of your original dict on a pairwise basis and create the list of dict you require.

Original Grouper Method in Itertools Recipes

def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)

Modification into a Pairwise Method

def pairwise(iterable, fillvalue=None):
    args = [iter(iterable)] * 2
    return zip_longest(*args, fillvalue=fillvalue)

Iterating over pairs and creating list of dicts

output = []
for key_one, key_two in pairwise(source_dict.keys()):
    output.append({key_one: source_dict[key_one], key_two: source_dict[key_two]})

Sidenote

As suggested by the comments, you might want to reconsider how you are reading in your data if at all possible.

Upvotes: 1

Related Questions