Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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_Python 2.7_Dictionary - Fatal编程技术网

在python中添加排序字典的结果

在python中添加排序字典的结果,python,python-2.7,dictionary,Python,Python 2.7,Dictionary,我有一个脚本,可以循环多个文件。 对于每个文件,我计算文件中特定组合出现的频率 我使用以下代码执行此操作: with open("%s" %files) as f: freqs = {} sortedFreqs = [] # read lines of csv file for l in f.readlines(): # some code here (not added) which fills the mutationList value

我有一个脚本,可以循环多个文件。 对于每个文件,我计算文件中特定组合出现的频率

我使用以下代码执行此操作:

with open("%s" %files) as f:
    freqs = {}
    sortedFreqs = []

    # read lines of csv file
    for l in f.readlines():

        # some code here (not added) which fills the mutationList value

    # this dict stores how often which mutation occurs.
    freqs = Counter(mutationList)

    # same list, only sorted.
    sortedFreqs = sorted(freqs.iteritems(), key=operator.itemgetter(1), reverse=True)
因此freqs变量包含一长串条目

例如:

'FAM123Ap.Y550': 1, 'SMARCB1p.D192': 1, 'CSMD3p.T1137': 3
'CSMD3p.T1137': 3, 'FAM123Ap.Y550': 1, 'SMARCB1p.D192': 1
现在我想根据存储在sortedfreq中的第二个值对它们进行排序

例如:

'FAM123Ap.Y550': 1, 'SMARCB1p.D192': 1, 'CSMD3p.T1137': 3
'CSMD3p.T1137': 3, 'FAM123Ap.Y550': 1, 'SMARCB1p.D192': 1
这一切都很顺利,但我现在想循环多个文件,并将所有找到的频率加在一起。因此,如果我再找到'CSMD3p.T1137'值2次,我想存储'CSMD3p.T1137':5

wanted output:
totalFreqs = 'FAM123Ap.Y550': 1, 'SMARCB1p.D192': 1, 'CSMD3p.T1137': 5, 'TRPM1p.R551': 2
totalFreqsSorted = 'CSMD3p.T1137': 5,'TRPM1p.R551': 2 'FAM123Ap.Y550': 1, 'SMARCB1p.D192': 1'

如何在python中“添加”字典的键值?(如何正确归档totalFreqs和totalFreqsSorted的值)

对所有计数使用一个
计数器()
对象,并对每个文件进行更新:

freqs = Counter()

for file in files:
    with open(...) as f:
        #

        freqs.update(mutationList)
或者,您可以简单地将计数器相加:

total_freqs = Counter()


for file in files:
    with open(...) as f:
        #

        freqs = Counter(mutationList)
        total_freqs += freqs
请注意,
Counter()
对象已经提供了频率的反向排序列表;只需使用,而不是自己排序:

sortedFreqs = freqs.most_common()

Counter()
对象有一个
。最常见的()方法
已经为您排序了项目;不需要自己排序。是的,这将是一个很好的解决方案,不幸的是,我想追溯每个文件类型每种变异发生的频率,这就是为什么我为每个文件保留一个freqs变量。@MrFronk:然后使用第二个版本。它将每个文件中的
freqs
对象分开,并在
total\u freqs
中将它们相加
total_freqs
是一个单独的
计数器()
,用于所有文件的频率。