【发布时间】:2015-11-19 20:23:21
【问题描述】:
我正在使用 Python 2.7。 我正在尝试比较字典中的值项。
我有两个问题。 首先是长度为 1 的字典中的值的迭代。我总是得到一个错误,因为 python 不迭代整数并且单个项目作为值是 python 的整数。 我试图将项目更改为字符串。我试图将整个字典更改为字符串。在这些情况下,迭代也会比较逗号和括号,但这不是目的。因此我复制了该项目。 key:1 和 key:4 中的值是同一个项目,两次。这个解决方案有效,但它自然会改变一点结果。
第二个问题是匹配计数不能正常工作。 是的,因为第一个问题,但还有另一个问题。 我的代码将所有值项与字典中的所有其他值项进行比较。不比较相同的模块。如果比较中有一个匹配,则没有问题。 但是,如果比较有两个匹配项,则代码将两次输出结果作为一个匹配项。但我只想要一个结果,有两场比赛。 代码中包含很多cmets,希望大家能看懂。
#dicModul is a dictionary with the modulnames(keys of dictionary) and the components of the modul(value of dictionary).
# If a modul consists of one component, it is impossible to iterate the modul, therefore this component is two times in a modul.
# Any ideas how to iterate a single value item in a dictionary?
dicModul = {0:(0,1),1:(1,1), 2:(2,3,4,5,6,1,7,2),
3:(8,1),4:(9,9), 5:(10,10,5,11,0,12,13), 6:(10,11,9,7)}
#The list is needed for the iteration and to operate the different modul in the dictionary by following for loops.
ModulList = [0,1,2,3,4,5,6]
#Counter is needed for counting same components in different moduls.
Counter = 0
for modKey in ModulList: #For-loop is needed for iteration of keys
for modKey2 in ModulList: #2nd for-loop of Modulkeys.The components of moduls have to be checked for matches in other moduls.
#Therefore a second iteration of the modul list is needed.
if modKey == modKey2: #Same moduls do not have to be checked.
print "Skip same Moduls"
Counter = 0 #The modkey2 have to changed by iteration. The counter have to be reset.
elif modKey != modKey2: #Different moduls have to be checked.
for modVal in dicModul[modKey]: #Iteration of components for iterated modul.
if modVal in dicModul[modKey2]: #Checking iterated components in different modul
Counter = Counter + 1 #If component was in different moduls, counter will add +1
print "Modul: "+str(modKey)+" % Modul: "+str(modKey2)+ " Matches= "+str(Counter)
#print function for both moduls and number of same components
Counter =0
我认为,如果可以在这个时刻将前一个已检查的键和已检查的键分开,则计数器不会每次都从 0 开始。 我试图解决问题并寻找解决方案,但我没有找到这个案例。
loop on a dictionary (previous values) python
How to access previous/next element while for looping?
我想解决方案必须看起来像下一个代码。
我用#->
#dicModul is a dictionary with the modulnames(keys of dictionary) and the components of the modul(value of dictionary).
# If a modul consists of one component, it is impossible to iterate the modul, therefore this component is two times in a modul.
# Any ideas how to iterate a single value item in a dictionary?
dicModul = {0:(0,1),1:(1,1), 2:(2,3,4,5,6,1,7,2),
3:(8,1),4:(9,9), 5:(10,10,5,11,0,12,13), 6:(10,11,9,7)}
#The list is needed for the iteration and to operate the different modul in the dictionary by following for loops.
ModulList = [0,1,2,3,4,5,6]
#Counter is needed for counting same components in different moduls.
Counter = 0
for modKey in ModulList: #For-loop is needed for iteration of keys
for modKey2 in ModulList: #2nd for-loop of Modulkeys.The components of moduls have to be checked for matches in other moduls.
#Therefore a second iteration of the modul list is needed.
if modKey == modKey2: #Same moduls do not have to be checked.
print "Skip same Moduls"
Counter = 0 #The modkey2 have to changed by iteration. The counter have to be reset
elif modKey != modKey2: #Different moduls have to be checked.
for modVal in dicModul[modKey]: #Iteration of components for iterated modul.
if modVal in dicModul[modKey2]: #Checking iterated components in different modul
#-> if modKey2 == previous modKey2: #Checking if is the previous modul, so counter is not reset.
#-> Counter = Counter +1
#-> print "Modul: "+str(modKey)+" % Modul: "+str(modKey2)+ " Matches= "+str(Counter)
#-> else:
#-> Counter = 1 #Counter is setted 1, because a same component is found in a different modul.
elif:
Counter = 0
这是我预期的解决方案
Modul Modul Matches
skip same modul
0 1 1
0 2 1
0 3 1
0 4 0
0 5 1
skip same modul
1 2 1
1 3 1
1 4 0
1 5 0
skip same modul
2 3 1
2 4 0
2 5 1
2 6 1
skip same modul
3 4 0
3 5 0
skip same modul
4 5 0
4 6 1
skip same modul
5 6 3
【问题讨论】:
-
您将值存储为元组而不是列表是否有原因?
-
一句话,你想达到什么目的?
-
@Casey:我使用元组,因为我需要命名我的模数/值。元组是获取值和键的最简单方法。 @Padraic:我想实现所有模块项目之间的比较。我有一个大的 csv 文件,我想分析关于相同项目的所有模值。
标签: python for-loop dictionary counting