Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python——附加到字典_Python_Dictionary - Fatal编程技术网

Python——附加到字典

Python——附加到字典,python,dictionary,Python,Dictionary,出于某种原因,当我尝试将元素附加到字典中时,它只会覆盖数据并只存储最新的键和项,而不是添加到字典中。如果这是一个初学者的错误,很抱歉--仍在学习字典 def filetodict(vcfFile): dictionary = {} with open(vcfFile , 'rb') as file: for row in csv.reader(file , delimiter = '\t'): if "#" in row[0]:

出于某种原因,当我尝试将元素附加到字典中时,它只会覆盖数据并只存储最新的键和项,而不是添加到字典中。如果这是一个初学者的错误,很抱歉--仍在学习字典

def filetodict(vcfFile):
    dictionary = {}

    with open(vcfFile , 'rb') as file:


        for row in csv.reader(file , delimiter = '\t'):
            if "#" in row[0]:
                continue
            dictionary.update( {row[0]:row[1:]} )
    return dictionary

您可以使用的是集合。defaultdict:

from collections import defaultdict

def filetodict(vcfFile):
     d = defaultdict(list)

     with open(vcfFile , 'rb') as file:


         for row in csv.reader(file , delimiter = '\t'):
             if "#" in row[0]:
                 continue
             d[row[0]].extend(row[1:])
     return dictionary

不要使用update来追加,只需这样做:

def filetodict(vcfFile):
    dictionary = {}    
    with open(vcfFile , 'rb') as file:
        for row in csv.reader(file , delimiter = '\t'):
            if "#" in row[0]:
                continue
            dictionary[row[0]]=row[1:]
    return dictionary

缩进误差,修正如下

    if "#" in row[0]: 
        dictionary.update( {row[0]:row[1:]} ) 

此外,您还可以尝试使用
字典[row[0]]=row[1:][/code>,

这是一个初学者的错误,但很常见。我认为你在这里混淆视听

字典使用
key:value
对保存信息,其中
键在每个字典中是唯一的,用于索引信息

另一方面,列表是由一系列数字索引的序列。像这样
foo=['a'、'b'、'c']
,可以使用它们的索引号访问它们,例如:
foo[0]
返回
'a'

因此,关于你的问题,我认为你正在试图生成一个通常被称为
集合的
,它是一个字典列表。 例如:

那么,关于你的问题,我想做的是这样的:

def file_to_collection(vcfFile):
    collection = []  # This will hold every dict per row

    with open(vcfFile , 'rb') as file:
        for row in csv.reader(file , delimiter = '\t'):
            if "#" in row[0]:
                continue  # this ignores the first line, it's okay
            collection.append({row[0]: row[1:]})
    return collection
希望能有所帮助,告诉我这是否是你想要的


干杯

请注意,字典键是唯一的。因此,如果您使用已存在的密钥添加到词典中,您将覆盖它以保存您正在添加的较新值。您可以通过将
词典值
s设置为
列表
s来执行所描述的操作,您可以在阅读文件时向其添加
内容。使用
词典=defaultdict(列表)
并继续添加。或者您可以使用
pandas
,这是一个非常好的模块,非常适合这种情况。嘿,您能提供一些有关您正在处理的信息的反馈吗?根据信息和序列化方式的不同,您的问题有不同的解决方案。我尝试运行此操作,它会像原始问题一样覆盖密钥和项否我不希望字典中有任何包含“#”的行
def file_to_collection(vcfFile):
    collection = []  # This will hold every dict per row

    with open(vcfFile , 'rb') as file:
        for row in csv.reader(file , delimiter = '\t'):
            if "#" in row[0]:
                continue  # this ignores the first line, it's okay
            collection.append({row[0]: row[1:]})
    return collection