Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

python - Identify same values for particular key in list of dictionaries

I have a list of dictionaries that look like this:

[
    {'ServiceID': 20, 'primary': '20', 'secondary': '12'},
    {'ServiceID': 20, 'primary': '20', 'secondary': '12'},
    {'ServiceID': 20, 'primary': '20', 'secondary': '12'},
    {'ServiceID': 16, 'primary': '16', 'secondary': '8'},
    {'ServiceID': 20, 'primary': '20', 'secondary': '12'},
    {'ServiceID': 8,  'primary': '8',  'secondary': '16'},
    {'ServiceID': 12, 'primary': '12', 'secondary': '20'},
    {'ServiceID': 8,  'primary': '8',  'secondary': '16'}
]

I would like create a new sorted dictionary where the we have the following:

key = value of 'ServiceID'
key = value of how many times that particular 'ServiceID' is listed as a 'primary'
key = value of how many times that particular 'ServiceID' is listed as a 'secondary'

For example:

[
    {'ServiceID': 8, 'primaryCount': 2, 'secondaryCount': 1},
    {'ServiceID': 12, 'primaryCount': 1, 'secondaryCount': 4},
    {'ServiceID': 16, 'primaryCount': 1, 'secondaryCount': 2},
    {'ServiceID': 120, 'primaryCount': 4, 'secondaryCount': 1}
]

Code that I have so far that doesn't quite seem to do what I desire, meaning that I am unsure as to how to appropriately increment the number of primaries and secondaries across the entire for loop as well as how to only ensure I am capturing the uniques for the 'ServiceID'

I believe there is something wrong with my logic:

temp_count_list = list()
temp_primary_counts = 0
temp_secondary_counts = 0

for sub_dict in new_list:
    temp_dict = dict()

    temp_dict['ServiceID'] = sub_dict['ServiceID']
    
    if sub_dict['ServiceID'] == int(sub_dict['primarySlice']):
        temp_dict['primaryCount'] = temp_primary_counts +=1

    if sub_dict['ServiceID'] == int(sub_dict['secondarySlice']):
        temp_dict['secondaryCount'] = temp_secondary_counts +=1

    temp_count_list.append(temp_dict)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Basic idea is, get all the ServiceID, primary, secondary in a dict (in code k), and then for each unique ServiceID count the frequency of that ServiceID in the primary and secondary.

l = [
    {'ServiceID': 20, 'primary': '20', 'secondary': '12'},
    {'ServiceID': 20, 'primary': '20', 'secondary': '12'},
    {'ServiceID': 20, 'primary': '20', 'secondary': '12'},
    {'ServiceID': 16, 'primary': '16', 'secondary': '8'},
    {'ServiceID': 20, 'primary': '20', 'secondary': '12'},
    {'ServiceID': 8,  'primary': '8',  'secondary': '16'},
    {'ServiceID': 12, 'primary': '12', 'secondary': '20'},
    {'ServiceID': 8,  'primary': '8',  'secondary': '16'}
]

k =     {'ServiceID': [], 'primaryCount': [], 'secondaryCount': []}

for i in l:
    k['ServiceID'].append(i['ServiceID'])
    k['primaryCount'].append(i['primary'])
    k['secondaryCount'].append(i['secondary'])

res = {'ServiceID': 0, 'primaryCount': [], 'secondaryCount': []}

result = []

for i in sorted(set(k['ServiceID'])):
    res['ServiceID']=i
    res['primaryCount'] = k['primaryCount' ].count(str(i))
    res['secondaryCount'] = k['secondaryCount' ].count(str(i))
    result.append(res)
    res = {'ServiceID': 0, 'primaryCount': [], 'secondaryCount': []}

print(result)

output

[
 {'ServiceID': 8, 'primaryCount': 2, 'secondaryCount': 1},
 {'ServiceID': 12, 'primaryCount': 1, 'secondaryCount': 4}, 
 {'ServiceID': 16, 'primaryCount': 1, 'secondaryCount': 2},
 {'ServiceID': 20, 'primaryCount': 4, 'secondaryCount': 1}
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...