辞書値がカウント付きの個別リストである出力を取得しようとしています。ここPython辞書が別個のリストカウントを取得する
は私の入力データである:ここ
event = [{'1a': ['tv', 'energy star tv']}, {'2a':['tv', 'energy star tv']}, {'3a':['tv', 'brand']}]
は私の所望の出力である:ここ
{'tv': ['energy star tv', 2],['brand', 1]}
は私の現在のコードである:ここ
event = [{'1a': ['tv', 'energy star tv']}, {'2a':['tv', 'energy star tv']}, {'3a':['tv', 'brand']}]
event_values = []
for e in event:
event_values.append(e.values())
e_dict = {}
for e in event_values:
if 'tv' == e[0][0]:
new_key = ''
new_key = e[0][0]
new_facet_value = ''
new_facet_value = e[0][1]
if new_key not in e_dict: # detects first key is not in dict
e_dict[new_key] = [new_facet_value, 1]
else:
i = e_dict[new_key][1] + 1 # increments count
j = []
j = [new_facet_value, i]
e_dict[new_key] = j
print e_dict
は(望ましくない)私の電流であります出力:
{'tv': ['brand', 3]}