[英]How can I append a list of values for single dictionary key (1_key : [N_values]) in Python?
I am trying to generate a {single-key : [multi-value]} dictionary in Python from a .txt file. 我正在尝试从.txt文件用Python生成一个{单键:[多值]}字典。
This is the text file (tab-separated), 这是文本文件(制表符分隔),
A02.835.583.748 A02.880 0.818181818181818
A02.835.583.748 A02.513 0.818181818181818
A02.835.583.748 A01.378.800.750 0.636363636363636
A02.835.583 A02.880 0.863636363636364
A02.835.583 A02.513 0.863636363636364
A02.835.583 A01.378.800.750 0.681818181818182
A01.378.800.750 A02.880 0.727272727272727
A01.378.800.750 A02.513 0.727272727272727
A01.378.800.750 A01.378.800.750 1
For the same, I use "defaultdict()" function, but somehow I am unable to properly generate the dictionary. 同样,我使用“ defaultdict()”函数,但是由于某种原因我无法正确生成字典。 I am able to generate a dictionary through this, but it is weird. 我可以通过它生成一个字典,但这很奇怪。 So, I fetch one of the keys from this weird dictionary. 因此,我从这本怪异的字典中获取了其中一个键。
print(anaDict.get('A02.835.583.748'))
Output: 输出:
['A02.880=0.818181818181818', [...], ['A02.513=0.818181818181818'], ['A01.378.800.750=0.636363636363636']]
However, the [...] in this dictionary are actually nesting the other values of the same key in an inception kind of way. 但是,此字典中的[...]实际上是以一种初始方式嵌套了同一键的其他值。
The code I write, 我写的代码
anaDict = defaultdict()
anaSet = set()
with open(f, 'r') as anaFile:
if '148' in f:
for line in anaFile:
key = line.split('\t')[0].rstrip()
conclusionVal = line.split('\t')[1].strip()
simScore = line.split('\t')[2].strip()
value = [conclusionVal + "=" + simScore]
if key not in anaDict:
print("Here it goes: " , key, value)
anaDict[key] = value
if key in anaDict:
print("Different value: ", key, value)
anaDict[key].append(value)
print(anaDict.get('A02.835.583.748'))
I expected the code to generate following dictionary (shown as key-value pairs). 我希望代码能够生成以下字典(显示为键值对)。
A02.835.583.748 : [A02.880 = 0.818181818181818 , A02.513 = 0.818181818181818, A01.378.800.750 = 0.636363636363636]
A02.835.583 : [A02.880 = 0.863636363636364, A02.513 = 0.863636363636364, A01.378.800.750 = 0.681818181818182]
A01.378.800.750 : [A02.880 = 0.727272727272727, A02.513 = 0.727272727272727, A01.378.800.750 = 1]
I cannot figure out what it is that I am doing wrong. 我无法弄清楚我做错了什么。 I will be helpful for any help or direction. 我将为您提供任何帮助或指导。
This line here is your problem: 这行是您的问题:
anaDict[key].append(value)
When you use list#append
, you're putting the argument, in its entirety into the list. 使用list#append
,您会将参数完整地放入列表中。 But since you're passing in a list, you're putting a list in the list. 但是由于要传递列表,因此要在列表中放置一个列表。 What you've described wanting to do is to append all of the values from the argument list, not the list itself. 您描述的要执行的操作是附加参数列表中的所有值,而不是列表本身。 All you have to do is replace append
with extend
. 您所要做的就是用extend
替换append
。
anaDict[key].extend(value)
Then you're telling the interpreter to unpack the argument list and append each of the values. 然后,您要告诉解释器解压缩参数列表并附加每个值。
The modified code according to the suggestion from @mypetlion (that works) is updated under. 根据@mypetlion(有效)建议的修改后的代码将在下面更新。
anaDict = defaultdict()
anaSet = set()
#print(f)
with open(f, 'r') as anaFile:
if '148' in f:
for line in anaFile:
key = line.split('\t')[0].rstrip()
conclusionVal = line.split('\t')[1].strip()
simScore = line.split('\t')[2].strip()
value = [conclusionVal + "=" + simScore]
if key not in anaDict:
anaDict[key] = value
if key in anaDict:
anaDict[key].extend(value)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.