Ich arbeite seit mehr als einer Stunde an einem dummen Problem, aber ich kann die Lösung nicht herausfinden. Ich erstelle ein defaultdict (Liste) mit einer anfänglichen Liste und aktualisiere diese Liste durch eine for-Schleife. Jedes Mal, wenn ich einen Wert aktualisiere, werden alle anderen Werte mit demselben Wert aktualisiert. Kann mir bitte jemand helfen? Hier ist mein Code:Python: defaultdict jeder Wert wird aktualisiert
from collections import defaultdict
base = ["coucou", "salut", "tchao"]
initial_vector = [0]*len(base)
dict_vectorized_documents = defaultdict(lambda: initial_vector)
inversed_index = {"coucou": [(1, 3), (100, 4)], "salut": [(1, 1), (99, 2), (33, 3)], "tchao": [(1, 5)]}
for i, word in enumerate(base):
print(word)
for element in inversed_index[word]:
print(element[0])
print(i)
print(element[1])
print(dict_vectorized_documents[element[0]][i])
dict_vectorized_documents[element[0]][i] = element[1]
print(dict_vectorized_documents)
print(dict_vectorized_documents)
Und hier meine Protokolle ist, wenn ich es laufen:
coucou
1
0
3
0
defaultdict(<function <lambda> at 0x7fcc5fac1f28>, {1: [3, 0, 0]})
100
0
4
3
defaultdict(<function <lambda> at 0x7fcc5fac1f28>, {1: [4, 0, 0], 100: [4, 0, 0]})
salut
1
1
1
0
defaultdict(<function <lambda> at 0x7fcc5fac1f28>, {1: [4, 1, 0], 100: [4, 1, 0]})
99
1
2
1
defaultdict(<function <lambda> at 0x7fcc5fac1f28>, {1: [4, 2, 0], 99: [4, 2, 0], 100: [4, 2, 0]})
33
1
3
2
defaultdict(<function <lambda> at 0x7fcc5fac1f28>, {1: [4, 3, 0], 99: [4, 3, 0], 100: [4, 3, 0], 33: [4, 3, 0]})
tchao
1
2
5
0
Vielen Dank!
Vielen Dank! : D – guillaumegg10