[issue38984] Value add to the wrong key in a dictionary

Gabriele report at bugs.python.org
Thu Dec 5 19:22:50 EST 2019


New submission from Gabriele <gabriele.marras85 at gmail.com>:

hello,
I found this strange issue in my program:

I’m reading a file with 5 columns separated by ‘;’ . Columns 3,4 and 5 can have multiple values and in this case, are separated by ‘,’ .
Some values in column 3 can be repeated in each line in the same column.

Goal:
I want create a dictionary for each line using as a key the value in the first column and another key using the values that are in column 3.
If the key in column 3 is already in the dictionary, I want add the value in column 4 to the dictionary for the key that is repeated.

Example file:
col1;col2;col3;col4;col5
id1;i1;val1;Si1;Da1
id2;i2;val2,val1;Si2,Si1;Da2
id3;i3;val3;Si3;Da3

Expected Result:
{‘id1’ : [ 'id1','i1',['val1'],['Si1'],['Da1’] ] ,
 ‘id2’ : [‘ id2','i2',['val2','val1'] ,[Si2,Si1] ,['Da2’] ] , 
 ‘val1’ : [ 'id1','i1',['val2','val1'],['Si2','Si1'],['Da1','Da2’] ] , 
 ‘val2’ : [ 'id2','i2',['val2','val1'],[Si2,Si1]    ,['Da2’] ] , 
 'id3’ : [ 'id3','i3','val3',['Si3'],['Da3’] ] , 
 ‘val3’ : [ 'id3','i3','val3',['Si3'],['Da3’] ] }

But what I obtaining is 
{'id2': ['id2', 'i2', ['val2', 'val1'], ['Si2', 'Si1'], ['Da2'], '5'], 
'id3': ['id3', 'i3', ['val3'], ['Si3'], ['Da3'], '5’],
'id1': ['id1', 'i1', ['val1'], ['Si1', 'Si2', 'Si1'], ['Da1', 'Da2'], '5'], 
'val3': ['id3', 'i3', ['val3'], ['Si3'], ['Da3'], '5'], 
'val2': ['id2', 'i2', ['val2', 'val1'], ['Si2', 'Si1'], ['Da2'], '5'], 
'val1': ['id1', 'i1', ['val1'], ['Si1', 'Si2', 'Si1'], ['Da1', 'Da2'], '5']} 

My bug:
Key id1 was called just one time in my program, but in my results, I can find that list in position 3 is with 3 value (Si1 , Si2 , Si1) when it supposes to have just Si1.

Am I doing something wrong or this is a potential bug?
I ran the program using a different machines and different python versions, but results don't change.

----------
files: program.py
messages: 357894
nosy: malbianco
priority: normal
severity: normal
status: open
title: Value add to the wrong key in a dictionary
type: behavior
versions: Python 3.6
Added file: https://bugs.python.org/file48759/program.py

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38984>
_______________________________________


More information about the Python-bugs-list mailing list