I have 2 lists that I combined their elements into a dictionary. Both lists have size of 20, and the created dictionary is almost always 13. The code is:
nodes = []
for x in range(1,21):
nodes.append(str(random.randint(1,20)))
print(nodes)
Output:
['3', '6', '10', '12', '12', '10', '11', '17', '6', '19', '20', '19', '7', '16', '9', '13', '15', '9', '12', '5']
The rest of the code:
lines=[]
fp = open("work.txt") # open file on read mode
lines = fp.read().split("\n") # create a list containing all lines
fp.close() # close file
print(lines)
Output:
['5', '1', '7', '1', '1', '3', '12', '1', '1', '8', '7', '5', '12', '5', '5', '3', '7', '7', '13', '1', '']
To make a dictionary:
dictionary = dict(zip(nodes,lines))
print(dictionary)
{'3': '1', '6': '5', '10': '12', '12': '13', '10': '12', '11': '7', '17': '7', '6': '5', '19': '3', '20': '1', '19': '1', '17': '3', '16': '5', '9': '7'}
As you see, the size is got smaller to 14 when zipped. Do you know what is the reason and how can I fix it?
Aucun commentaire:
Enregistrer un commentaire