字典值的索引

时间:2016-02-14 20:16:43

标签: python loops for-loop dictionary indexing

在下面的代码中,我试图在下面的MinimunSkew函数中访问字典值的indeces。如果字典存储在变量s中并且我在迭代值以查找变量m的所有索引,则以下代码仅给出第一个索引而不是第二个索引:

for s in svalues():         positions = s.values()。index(m)

如何在字典中获取变量m值的所有索引?

完整代码:     基因组= “TAAAGACTGCCGAGAGGCCAACACGAGTGCTAGAACGAGGGGCGTAAACGCGGGTCCGAT”

def Skew(Genome):
skew = {}
n = len(Genome)
skew[0]=0
for i in range(1,n+1):
    skew[i] = skew[i-1]
    if Genome[i-1] == "G":
        skew[i] = skew[i-1]+1
    elif Genome[i-1] == "C":
        skew[i] = skew[i-1]-1  
    else:
        skew[i]=skew[i-1]
return skew
Skew(Genome)


def MinimumSkew(Genome):
positions=[]
s = Skew(Genome)
m = min(s.values())

for i in s.values():
    positions = s.values().index(m)

return positions
print MinimumSkew(Genome)

1 个答案:

答案 0 :(得分:0)

您的For循环的MinimumSkew不返回位置,因为字典没有索引属性。 min(s.values())仅返回dict的最低值。您需要修改代码。这是修改版本:

 def MinimumSkew(Genome):
            positions =[]
            s = Skew(Genome)
            minimumValue = min(s.values())

            for (k,v) in s.items():
                if v == minimumValue:
                    positions.append(k)
            return positions

 Genome = "GCATGCTTCGCATATGCTTGAACAACG"
 print(MinimumSkew(Genome))

输出:[26]