2017-02-10 1 views
1

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!

Antwort

2

Da Sie die gleiche Liste in Ihrer defaultdict Fabrik zurückgeben. Die einfachste Lösung? Explizit kopieren Sie sie mit list:

>>> from collections import defaultdict 
>>> base = ["coucou", "salut", "tchao"] 
>>> initial_vector = [0]*len(base) 
>>> dict_vectorized_documents = defaultdict(lambda: list(initial_vector)) 

Hier ist ein konstruiertes Beispiel, dass es vielleicht mehr deutlich macht:

>>> initial_list = [0, 0, 0] 
>>> def get_initial(): 
...  return initial_list 
... 
>>> d = {} 
>>> for k, i in zip(['key1','key2','key3'],range(3)): 
...  new_list = get_initial() 
...  new_list[i] = 'mutated' 
...  d[k] = new_list 
... 
>>> d 
{'key2': ['mutated', 'mutated', 'mutated'], 'key3': ['mutated', 'mutated', 'mutated'], 'key1': ['mutated', 'mutated', 'mutated']} 

So new_listkeine neue Liste nach allen war. Wenn wir jedoch tun:

>>> initial_list = [0, 0, 0] 
>>> def get_initial(): 
...  return list(initial_list) 
... 
>>> d = {} 
>>> for k, i in zip(['key1','key2','key3'],range(3)): 
...  new_list = get_initial() 
...  new_list[i] = 'mutated' 
...  d[k] = new_list 
... 
>>> d 
{'key2': [0, 'mutated', 0], 'key3': [0, 0, 'mutated'], 'key1': ['mutated', 0, 0]} 
>>> 
+0

Vielen Dank! : D – guillaumegg10

0

Sind Sie nicht auf dem defaultdict Objekt anhängen tun soll, wie:

dict_vectorized_documents[element[0]][i].append(element[1]) 
Verwandte Themen