JeanP
JeanP

Reputation: 507

Trying to split a list of dictionaries

Hello I am trying to compare this list of dictionaries:
Call this list Animals:

[{'Fishs=16': 'Fishs=16',
  'Birds="6"': 'Birds="6"',
  'Dogs=5': 'Dogs=5',
  'Bats=10': 'Bats=10',
  'Tigers=11': 'Tigers=11',
  'Cats=4': 'Cats=4'},
 {'Cats=40': 'Cats=40',
  'Tigers': 'Tigers = 190',
  'Birds=4': 'Birds=4',
  'Bats': 'Bats = Null',
  'Fishs': 'Fishs = 24',
  'Dogs': 'Dogs = 10'}]

I want to make the list look like this

[{'Tigers': 'Tigers=11',
  'Dogs': 'Dogs=5',
  'Cats': 'Cats=4',
  'Bats': 'Bats=10',
  'Fishs': 'Fishs=16',
  'Birds': 'Birds="6"'},
 {'Tigers': 'Tigers=190',
  'Dogs': 'Dogs=10',
  'Cats': 'Cats=40',
  'Bats': 'Bats=Null',
  'Fishs': 'Fishs=24',
  'Birds': 'Birds=4'}]

so that I can compare it to this other list:

{'Tigers': '19',
 'Dogs': '10',
 'Cats': '40',
 'Bats': '10',
 'Fishs': '234',
 'Birds': '3'}

Heres the code i've tried to use inorder to split the list:

animals = []
for d in setData:
    animals.append({k: v.split('=')[1] for k, v in d.items()})

however It will not split the list since my keys in the dictionaries are in this format Dogs=4 rather than Dogs = 4. I need to be able to split this list even if they are in that format.

On a completely different side note, once this part of the code is fixed I need to figure out how to compare the data from these keys against each other.

for example: Lets say I have Dogs="23" and the compared list is Dogs="50" According to my code this should be Incorrect, but due to the quotes ("23") it says it is, it does not compare the value inside. This is the code i have to compare:

correct_parameters = dict(re.match(r'(\w*)="?(\d*)"?', s).group(1, 2) for s in dataDefault[1:])
print correct_parameters
count = 0
while (count < (len(setNames))):
    for number, item in enumerate(animals, 1):
        print setNames[count]
        count = count + 1
        for param, correct in correct_parameters.items():
            if item[param] == correct:
                print('{} = {} which is correct'.format(param, correct))

However for now I am just trying to fix the list split issue i am having.

Upvotes: 0

Views: 80

Answers (2)

Gerard van Helden
Gerard van Helden

Reputation: 1602

Just strip the string after you split it:

 >>> [v.strip() for v in "   a   =    b    ".split('=')]
 ['a', 'b']

Upvotes: 0

taesu
taesu

Reputation: 4580

lst = [{'Fishs=16': 'Fishs=16', 'Birds="6"': 'Birds="6"', 'Dogs=5': 'Dogs=5', 'Bats=10': 'Bats=10', 'Tigers=11': 'Tigers=11', 'Cats=4': 'Cats=4'}, {'Cats=40': 'Cats=40', 'Tigers': 'Tigers = 190', 'Birds=4': 'Birds=4', 'Bats': 'Bats = Null', 'Fishs': 'Fishs = 24', 'Dogs': 'Dogs = 10'}]

# for each element in that list, loop with index
for idx, val in enumerate(lst):
    # create temp object
    o = {}
    # loop the dictionary
    for k,v in val.iteritems():
        # if = is found in key
        if '=' in k:
            # change the key
            k = k.split('=')[0]
        # insert to temp object
        o[k] = v
    # change the temp object to current element in the list
    lst[idx] = o

Upvotes: 1

Related Questions