简体   繁体   English

如何在Python中附加单个字典键(1_key:[N_values])的值列表?

[英]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.

相关问题 如何将值附加到 python 字典中的现有键? 在没有“”的循环中 - How can i append values to an existing key in a dictionary in python? in a loop without " " 如何 append 重复值到字典列表中的相同键 - How to append the repeated values to same key in dictionary list 将键值附加到列表中的字典 - append key values to dictionary from list 我正在尝试将多个值附加到 python 中的字典中 - I m trying to append multiple values to key in a dictionary in python python 字典如果键包含其他键的值列表,如何创建(结构化)唯一字典列表 - python dictionary how can create (structured) unique dictionary list if the key contains list of values of other keys 具有多个值的 Python 字典单键可能吗? - Python dictionary single key with multiple values possible? 遍历字典以使字典键与值列表匹配并追加字典 - Loop through dictionary to match dictionary key to a list of values and append dictionary 如何在Python中从字典的多个值访问单个键 - How to access single key from multiple values of a dictionary in Python python 字典 - 如果它们匹配,则合并两个字典和 append 键值 - python dictionary - merge two dictionary and append key values if they match 如何在Python 3的同一个键中附加多个值? - How to append multiple values in the same key in Python 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM