Di bawah ini adalah daftarnya:
text = ['is', 'ramping', 'brings']
head = ['ramping', 'ramping', 'ramping']
childWord = [[],
['Cola', 'is', 'up', 'launches', 'brings', 'drink', '.'],
['as', 'it', 'mineral']]
Dan menggunakan kode di bawah ini saya memperbarui item daftar ini dengan nilai kata kerja kamus.
verb = {}
for i in range(0 , len(text)):
verb [i] = {'Verb Text': text[i], 'Text Head of the verb' : head[i], 'Child Word of the verb' : childWord[i]}
verb.update(verb [i])
verb
Dan saya mendapatkan output sebagai berikut:
{0: {'Verb Text': 'is',
'Text Head of the verb': 'ramping',
'Child Word of the verb': []},
1: {'Verb Text': 'ramping',
'Text Head of the verb': 'ramping',
'Child Word of the verb': ['Cola',
'is',
'up',
'launches',
'brings',
'drink',
'.']},
2: {'Verb Text': 'brings',
'Text Head of the verb': 'ramping',
'Child Word of the verb': ['as', 'it', 'mineral']},
'Verb Text': 'brings',
'Text Head of the verb': 'ramping',
'Child Word of the verb': ['as', 'it', 'mineral']}
Dalam output masalahnya adalah pembuatannya di bawah pasangan nilai kunci dua kali
'Verb Text': 'brings',
'Text Head of the verb': 'ramping',
'Child Word of the verb': ['as', 'it', 'mineral']
Komentar apapun akan dihargai..!!
2 jawaban
Baris verb.update(verb[i])
memperbarui kamus dengan elemen terakhir setelah perulangan for, sehingga elemen terakhir muncul dua kali.
Hapus baris itu dan kode Anda akan berfungsi dengan baik
Karenanya kode berubah menjadi
text = ['is', 'ramping', 'brings']
head = ['ramping', 'ramping', 'ramping']
childWord = [[],
['Cola', 'is', 'up', 'launches', 'brings', 'drink', '.'],
['as', 'it', 'mineral']]
verb = {}
for i in range(0 , len(text)):
verb [i] = {'Verb Text': text[i], 'Text Head of the verb' : head[i], 'Child Word of the verb' : childWord[i]}
#Remove this line since it is updating the dictionary with the last element again
#verb.update(verb [i])
print(verb)
Anda juga dapat menyederhanakan kode dengan menyusun ulang kamus Anda dan kemudian menggunakan pemahaman daftar untuk menghitung daftar akhir Anda
#Dictionary of keys to list of values
dct = { 'Verb Text': ['is', 'ramping', 'brings'],
'Text Head of the verb': ['ramping', 'ramping', 'ramping'],
'Child Word of the verb': [[],
['Cola', 'is', 'up', 'launches', 'brings', 'drink', '.'],
['as', 'it', 'mineral']]
}
#Keys of the dictionary
keys = dct.keys()
#Iterate over the indexes and keys to make your list via list and dictionary comprehension
verb = [{key:dct[key][idx]} for idx in range(len(dct['Verb Text'])) for key in keys]
print(verb)
Dan outputnya akan sama dalam kedua kasus yaitu
{
0: {'Verb Text': 'is', 'Text Head of the verb': 'ramping', 'Child Word of the verb': []},
1: {'Verb Text': 'ramping', 'Text Head of the verb': 'ramping', 'Child Word of the verb': ['Cola', 'is', 'up', 'launches', 'brings', 'drink', '.']},
2: {'Verb Text': 'brings', 'Text Head of the verb': 'ramping', 'Child Word of the verb': ['as', 'it', 'mineral']}
}
Anda memperbarui kamus di akhir di luar loop dan itu menyebabkan duplikasi nilai terakhir. Hapus dict.update
loop luar:
verb = {}
for i in range(0 , len(text)):
verb[i] = {'Verb Text': text[i], 'Text Head of the verb' : head[i], 'Child Word of the verb' : childWord[i]}
#verb.update(verb [i]) <- Remove this line
print(verb)
Cara yang lebih baik adalah menggunakan zip
dan enumerate
bawaan seperti:
verb = {}
for i, (x, y, z) in enumerate(zip(text, head, childWord)):
verb[i] = {'Verb Text': x, 'Text Head of the verb': y, 'Child Word of the verb': z}
print(verb)
Yang lagi-lagi menggunakan pemahaman kamus:
verb = {i: {'Verb Text': x, 'Text Head of the verb': y, 'Child Word of the verb': z} for i, (x, y, z) in enumerate(zip(text, head, childWord))}