gpt4 book ai didi

python - 在两个字典中循环以在 python 中创建一个新字典

转载 作者:行者123 更新时间:2023-11-28 21:32:44 24 4
gpt4 key购买 nike

我有两本字典。第一个看起来像这样:

{'15': ['monter', 'boulangerie'], '44': ['être', 'méchant'], '45': ['haïr', 'Foret', 'enchanter', 'être', 'beau', 'mille', 'faux', 'jeune', 'fille', 'être', 'asseoir', 'bor', 'mer'], '47': ['comprendre', 'vie', 'perdre', 'aide', 'stp', 'maman', 'perdre', 'être', 'seul', 'ami', 'aimer']}

第二个是一个包含单词作为键和分数作为值的字典:

values = Positive_score, Neutral_score, Negative_Score

{"beau": ['1', '3', '2'], 'seul': ['0', '1', '2'], 'cité des loups': ['230', '63', '48'], 'pavage mosaïque': ['296', '108', '0'], "répression de toute forme d'opposition": ['0', '3', '2'], 'en prendre pour': ['0', '81', '260'], 'Walt Disney Records': ['2', '3', '1'], 'paraombilical': ['3', '1', '2'], 'Eugênio': ['32', '85', '0'], 'laissant': ['0', '1', '1'], 'Baudouin de Champagne': ['2', '4', '2'], 'loi Goblet': ['3', '2', '1'], 'jeune': ['290', '49', '81'], 'Guezer': ['91', '44', '29'], "aimer": ['134', '48', '0'], 'marque de produit de luxe': ['21', '28', '2'], 'Zostérops de Woodford': ['18', '38', '0'], 'Henry Ludwell Moore': ['3', '7', '0'], 'Charlyne>prénom': ['23', '28', '0'], 'famille Masreliez': ['5', '4', '2'], 'canadienne-américaine': ['10', '3', '0'], 'bédéguar': ['63', '292', '0'], 'prix Lalande': ['2', '3', '1'], 'rapiécetais': ['2', '2', '0'], 'Maysoon': ['0', '30', '0'], 'hospice civil': ['265', '51', '50'], 'soutirer à': ['0', '6', '11'], 'faux': ['18', '21', '0'], "l'homme amphibie": ['2', '2', '1'], 'Johnny Ryan': ['5', '2', '0'], 'marcher des épaules': ['83', '85', '60'], 'faire lit à part': ['5', '5', '2'], 'Fonteny': ['4', '3', '0'], 'effet boomerang': ['96', '92', '70'], 'Guy Tirolien': ['0', '3', '0'], 'Mans-Les': ['5', '2', '0'], 'Stuart Jacques Ier': ['19', '331', '0'], 'Jupiter II': ['26', '33', '18'], 'Reuth bei Erbendorf': ['0', '10', '2'], 'couronne perlée du gland': ['1', '3', '3'], 'Wikipédia en Arabe bédouin': ['1', '2', '1'], 'Karen Karapetian': ['3', '3', '0'], 'trois mille cinq cents': ['307', '93', '0'], 'charge familiale': ['200', '68', '78'], 'Neuville-Saint-Rémy': ['0', '19', '0'], 'méchant': ['0', '25', '79'], 'modèle de simulation numérique': ['187', '111', '45'], 'Akrotiri': ['0', '313', '23']}

我想从这两个字典创建第三个字典,它将包含第一个字典的键,值将是列表 words 中所有单词的正、中和负分数的总和,所以我会有 Something像这样:

如果没有找到任何单词,我会记住分数指的是哪个键,我仍然会将键添加到字典中,但值为 000:

预期结果

{'15'; ['0', '0' , '0'], ['44': ['0', '25', '79'], '45': ['309','73','83'], '47': ['0','1','2']}

我的代码如下:

dico_phrases = {}
for k, v in d_filtered_words: # regarde la liste
for j in v: # regarde les élements de la liste intérieure
triplet = dico_lexique.get(j,-1)
if triplet != -1:
# Ici nous créons notre dictionnaire finale
if dico_phrases.get(repr(v)): # regarde si la cléi existe dans le dictionnaire
#si oui nous ajoutons les valeurs du dictionnaire.
#repr est utilisépour utiliser la liste comme cléde dictionnaire
t = [int(a) + int(b) for a,b in zip(dico_phrases[repr(v)] ,dico_lexique[j])] # zip prend un élement à un temps donnédans chaque
# Ajout élement et storage dans la liste
dico_phrases[repr(v)] = t #ajoute la liste au dictionnaire
else:
dico_phrases[repr(v)] = dico_lexique[j] # si la valeur n'existe pas , on garde juste le dico
# print le dico final

print(dico_phrases)

但结果不是我所期望的:键与原始字典的键不对应,分数也不对应,因为我手动检查它以查看分数是否为真。

{"'5'": [784, 222, 0], "'4'": ['381', '121', '0'], "'7'": ['378', '172', '0']}

我也想像这样在 csv 文件中写入值,但要包含其他列:

id;d_tag;d_lemma;Positive_score;Neutral_score;Negative_score
15;##;##;0;0;0
44;##;##;0;25;79
45;##;##;309;73;83
47;##;##;0;1;2

我的代码:

dicts = d_verbatim, d_tag, d_lemma, d_filtered_words, d_score

with open('my_data.csv', 'w') as ofile:
writer = csv.writer(ofile, delimiter=';')
writer.writerow(['id', 'd_verbatim', 'd_tag', 'd_lemma', 'd_filtered_words', 'Positive Score', 'Neutral Score', 'Negative Score'])
for key in d_verbatim.keys():
writer.writerow([key] + [d[key] for d in dicts])

最佳答案

import numpy as np
import pandas as pd

# i used these names for the dictionaries to not get confused
number_dict = {'15': ['monter', 'boulangerie'], ... }
score_dict = {"beau": ['1', '3', '2'], ... }

# initialise output
out_dict = {}

for number_entry in number_dict:
score_keys = number_dict.get(number_entry)

# intialise scores
temp_score = np.zeros((1,3))
for score_key in score_keys:

# if there is an entry for that word, add its scores to the temp scores
if score_dict.get(score_key):
temp_score += np.array(score_dict.get(score_key), dtype=np.int32)

# assign temp scores to output dictionary
out_dict[int(number_entry)] = [a for a in temp_score.ravel()]

# convert to dataframe, transpose and rename columns
df = pd.DataFrame(out_dict)
df = df.T
df.columns = ['Positive_score', 'Neutral_score', 'Negative_score']

filename = 'filename.csv'

# save to csv with ';' as separator
df.to_csv(filename, sep=';')

关于python - 在两个字典中循环以在 python 中创建一个新字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55894663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com