2017-01-09 55 views
-2
global_sulfur=0 
global_charcoal=0 
global_gun_powder=0 
global_low_grade_fuel=0 
global_metal_frags=0 
global_stone=0 
global_tech_trash=0 
global_cloth=0 
global_explosives=0 
global_rope=0 
global_small_stash=0 
global_beancan_grenade=0 
global_metal_pipe=0 
global_spring=0 
global_animal_fat=0 

def calculate(): 
    list={"Sulfur" : global_sulfur ,"Charcoal" : global_charcoal ,"Gun Powder" : global_gun_powder ,"Low Grade Fuel" : global_low_grade_fuel , "Metal Frags" : global_metal_frags , "Stone" : global_stone , \ 
     "Tech Trash" : global_tech_trash ,"Cloth" : global_cloth ,"Explosives" : global_explosives ,"Rope" : global_rope ,"Small Stash" : global_small_stash ,"Beancan Grenades" : global_beancan_grenade ,"Metal Pipe" : global_metal_pipe \ 
     ,"Spring" : global_spring ,"Animal Fat" : global_animal_fat} 

Theres 700+ more lines,but we want to make this questions simple,just think of the least of the variables will have a value over one。如果字典中的语句打印一个键和值,如果该值大于一,我该怎么办?

我试过多种方式和无数的时间,试图找到一种方法,使该值是否超过键值,将打印价值和键超过1

+2

你不应该影响'list',特别是不能用*实际上不是列表*的东西。你也应该阅读教程。 – jonrsharpe

回答

2

使用dict.items循环功能对。

data = { ... } 

for k, v in data.items(): 
    if v > 1: 
     print(k, v) 
+0

谢谢,就是这样。如果你不介意,你可以解释'K,V',我不明白它是如何工作的,除了它使两个不同的变量,谢谢 – Seavan

+0

'K'和'V'是我给每个键和值的名称为对迭代通过。 –

相关问题