连接两个变量名称以自动在列表 python 中查找内容
Concatenating two variable names to automate finding something in list python
我是初学者,正在通过制作口袋妖怪战斗模拟器练习 python,并且正在创建 伤害修正函数。
想法是:
如果在 "attack_type_2x_dmglist" 中找到宠物小精灵的 "attack type",则宠物小精灵造成双倍的伤害。 "Very effective"
如果宠物小精灵的 "attack type" 在 "attack_type_half_dmglist" 中找到,宠物小精灵只会造成一半的伤害。 "Isn't very effective"
我现在使用 print 来简化调试。
为了验证这一点,我已经完成了类型与类型的条件判断。
normal_2x_dmglist = []
normal_half_dmglist = ["Rock","Dragon"]
rock_2x_dmglist = ["Fire","Ice","Fly","Bug",]
rock_half_dmglist = ["Fighting","Ground","Steel"]
water_2x_dmglist = ["Fire","Ground","Rock"]
water_half_dmglist = ["Water","Grass","Dragon"]
grass_2x_dmglist = ["Water","Ground","Rock"]
grass_half_dmglist = ["Fire","Grass","Poison","Fly","Bug","Dragon","Steel"]
fighting_2x_dmglist = ["Normal","Ice","Rock","Dark","Steel"]
fighting_half_dmglist = ["Poison","Fly","Psychic","Bug","Fairy"]
ground_2x_dmglist = ["Fire","Electric","Poison","Rock","Steel"]
ground_half_dmglist = ["Grass","Bug"]
electric_2x_dmglist = ["Water","Fly"]
electric_half_dmglist = ["Electric","Grass","Dragon"]
def dmg_modifier(attack_type, receiver_pokemon_type):
if attack_type == "Normal" and receiver_pokemon_type in normal_2x_dmglist:
print "Normal deals 2x damage!"
elif attack_type == "Normal" and receiver_pokemon_type in normal_half_dmglist:
print "Normal deals 1/2 damage!"
elif attack_type == "Rock" and receiver_pokemon_type in rock_2x_dmglist:
print "Rock deals 2x damage!"
elif attack_type == "Rock" and receiver_pokemon_type in rock_half_dmglist:
print "Rock deals 1/2x damage!"
elif attack_type == "Water" and receiver_pokemon_type in water_2x_dmglist:
print "Water deals 2x damage!"
elif attack_type == "Water" and receiver_pokemon_type in water_half_dmglist:
print "Water deals 1/2x damage!"
elif attack_type == "Grass" and receiver_pokemon_type in grass_2x_dmglist:
print "Grass deals 2x damage!"
elif attack_type == "Grass" and receiver_pokemon_type in grass_half_dmglist:
print "Grass deals 1/2x damage!"
elif attack_type == "Fighting" and receiver_pokemon_type in fighting_2x_dmglist:
print "Fighting deals 2x damage!"
elif attack_type == "Fighting" and receiver_pokemon_type in fighting_half_dmglist:
print "Fighting deals 1/2x damage!"
elif attack_type == "Ground" and receiver_pokemon_type in ground_2x_dmglist:
print "Ground deals 2x damage!"
elif attack_type == "Ground" and receiver_pokemon_type in ground_half_dmglist:
print "Ground deals 1/2x damage!"
elif attack_type == "Electric" and receiver_pokemon_type in electric_2x_dmglist:
print "Electric deals 2x damage!"
elif attack_type == "Electric" and receiver_pokemon_type in electric_half_dmglist:
print "Electric deals 1/2x damage!"
else:
print "Type deals 1x damage - stays the same"
这很完美,只是非常重复和肮脏。
为了自动执行此操作,我计划将声明的 "attack type" 附加到“_2x_dmglist”,以便它自动调用列表。
首先我将声明的攻击类型小写,例如"Rock",使其变成rock。然后我将它附加到 _2x_dmglist,这样它就变成了 rock_2x_dmglist。然后它可以找到列表。
def dmg_modifier_2(attack_type, receiver_pokemon_type):
lower_case_attack_type = attack_type
lower_cased_attack_type = lower_case_attack_type.lower()
if attack_type == attack_type and receiver_pokemon_type in lower_cased_attack_type+_2x_dmglist:
print attack_type+"deals 2x damage"
elif attack_type == attack_type and receiver_pokemon_type in lower_cased_attack_type+_2x_dmglist:
print attack_type+"deals 1/2 damage"
else:
print "Not working"
输入:
dmg_modifier_2("Rock","Rock")
输出:
Traceback (most recent call last):
File "poke_game_test.py", line 98, in <module>
dmg_modifier_2("Rock","Rock")
File "poke_game_test.py", line 90, in dmg_modifier_2
finder = lower_case_attack_type.lower() + _2x_dmglist
NameError: global name '_2x_dmglist' is not defined
我理解回溯,但如何连接两个变量名以便整个过程自动化?在 PHP 中,我可以通过使用 + 号来做到这一点,我在这里尝试过。我也明白小写 "Rock" 也是一个字符串,而 _2x_dmglist 不是。如果变量连接在 python 中起作用,我真的很困惑,因为我在这里找不到类似的问题。
或者我的代码存在根本性错误?
谢谢!
虽然你可以这样做
globals().get(lower_cased_attack_type+'_2x_dmglist')
其中 globals 是 python 内置方法,它是 returns 调用方法中可访问的所有全局变量的字典。
但我建议将你的损坏列表存储在字典中,这将是一个更好的方法。
你应该做
damages = {
'rock_2x_dmglist': ["Fire","Ice","Fly","Bug",],
'rock_half_dmglist': ["Fighting","Ground","Steel"],
'water_2x_dmglist': ["Fire","Ground","Rock"],
'water_half_dmglist': ["Water","Grass","Dragon"],
}
那么你可以damages.get(lower_cased_attack_type+'_2x_dmglist')
我觉得是时候介绍一下了classes
,不过你也可以试试字典:
dict_2x_dmg = {
"Normal":set([]),
"Rock":{"Fire","Ice","Fly","Bug"},
"Water":{"Fire","Ground","Rock"},
"Grass":{"Water","Ground","Rock"},
"Fighting":{"Normal","Ice","Rock","Dark","Steel"},
"Ground":{"Fire","Electric","Poison","Rock","Steel"},
"Electric":{"Water","Fly"}
}
dict_half_dmg = {
"Normal":{"Rock","Dragon"},
"Rock":{"Fighting","Ground","Steel"},
"Water":{"Water","Grass","Dragon"},
"Grass":{"Fire","Grass","Poison","Fly","Bug","Dragon","Steel"},
"Fighting":{"Poison","Fly","Psychic","Bug","Fairy"},
"Ground":{"Grass","Bug"},
"Electric":{"Electric","Grass","Dragon"}
}
def dmg_modifier(attack_type, receiver_pokemon_type):
if receiver_pokemon_type in dict_2x_dmg[attack_type]:
dmg = "2x"
elif receiver_pokemon_type in dict_half_dmg[attack_type]:
dmg = "1/2x"
else:
dmg = "1x"
print "{0} deals {1} damage".format(attack_type, dmg)
>>> dmg_modifier("Water", "Ground")
Water deals 2x damage
您可以做的另一件事 - 是使用字典中的字典:
dict_dmg = {
"Normal":{"Rock":0.5,"Dragon":0.5},
"Rock":{"Fighting":0.5,"Ground":0.5,"Steel":0.5,"Fire":2,"Ice":2,"Fly":2,"Bug":2},
"Water":{"Water":0.5,"Grass":0.5,"Dragon":0.5,"Fire":2,"Ground":2,"Rock":2},
"Grass":{"Fire":0.5,"Grass":0.5,"Poison":0.5,"Fly":0.5,"Bug":0.5,"Dragon":0.5,"Steel":0.5,"Water":2,"Ground":2,"Rock":2},
"Fighting":{"Poison":0.5,"Fly":0.5,"Psychic":0.5,"Bug":0.5,"Fairy":0.5,"Normal":2,"Ice":2,"Rock":2,"Dark":2,"Steel":2},
"Ground":{"Grass":0.5,"Bug":0.5,"Fire":2,"Electric":2,"Poison":2,"Rock":2,"Steel":2},
"Electric":{"Electric":0.5,"Grass":0.5,"Dragon":0.5,"Water":2,"Fly":2}
}
def dmg_modifier2(attack_type, receiver_pokemon_type, dict_dmg):
dmg = dict_dmg[attack_type].get(receiver_pokemon_type, 1)
print "{0} deals {1}x damage".format(attack_type, dmg)
或者您甚至可以使用 pandas DataFrame
:
>>> import pandas as pd
>>> df_dmg = pd.DataFrame.from_dict(dict_dmg)
>>> df
Electric Fighting Grass Ground Normal Rock Water
Bug NaN 0.5 0.5 0.5 NaN 2.0 NaN
Dark NaN 2.0 NaN NaN NaN NaN NaN
Dragon 0.5 NaN 0.5 NaN 0.5 NaN 0.5
Electric 0.5 NaN NaN 2.0 NaN NaN NaN
Fairy NaN 0.5 NaN NaN NaN NaN NaN
Fighting NaN NaN NaN NaN NaN 0.5 NaN
Fire NaN NaN 0.5 2.0 NaN 2.0 2.0
Fly 2.0 0.5 0.5 NaN NaN 2.0 NaN
Grass 0.5 NaN 0.5 0.5 NaN NaN 0.5
Ground NaN NaN 2.0 NaN NaN 0.5 2.0
Ice NaN 2.0 NaN NaN NaN 2.0 NaN
Normal NaN 2.0 NaN NaN NaN NaN NaN
Poison NaN 0.5 0.5 2.0 NaN NaN NaN
Psychic NaN 0.5 NaN NaN NaN NaN NaN
Rock NaN 2.0 2.0 2.0 0.5 NaN 2.0
Steel NaN 2.0 0.5 2.0 NaN 0.5 NaN
Water 2.0 NaN 2.0 NaN NaN NaN 0.5
def dmg_modifier3(attack_type, receiver_pokemon_type, df_dmg):
dmg = df_dmg[attack_type][receiver_pokemon_type]
print "{0} deals {1}x damage".format(attack_type, dmg)
您可以执行以下操作:
damageDict = {}
damageDict['normal']: {'2x':[], 'half':["Rock","Dragon"]}
damageDict['rock']: {'2x':["Fire","Ice","Fly","Bug",], 'half':["Fighting","Ground","Steel"]}
damageDict['water']: {'2x':["Fire","Ground","Rock"], 'half':["Water","Grass","Dragon"]}
damageDict['grass']: {'2x':["Water","Ground","Rock"], 'half':["Fire","Grass","Poison","Fly","Bug","Dragon","Steel"]}
damageDict['fighting']: {'2x':["Normal","Ice","Rock","Dark","Steel"], 'half':["Poison","Fly","Psychic","Bug","Fairy"]}
damageDict['ground']: {'2x':["Fire","Electric","Poison","Rock","Steel"], 'half':["Grass","Bug"]}
damageDict['electric']: {'2x':["Water","Fly"], 'half':["Electric","Grass","Dragon"]}
# test case
attack_type = 'normal'
receiver_type = 'grass'
if receiver_type in damageDict[attack_type]['2x']:
print ("%s deals 2x damage!" % (attack_type)
elif receiver_type in damageDict[attack_type]['half']:
print ("%s deals 0.5x damage!" % (attack_type)
else:
print ("%s deals 1x damage!" % (attack_type)
我是初学者,正在通过制作口袋妖怪战斗模拟器练习 python,并且正在创建 伤害修正函数。
想法是:
如果在 "attack_type_2x_dmglist" 中找到宠物小精灵的 "attack type",则宠物小精灵造成双倍的伤害。 "Very effective"
如果宠物小精灵的 "attack type" 在 "attack_type_half_dmglist" 中找到,宠物小精灵只会造成一半的伤害。 "Isn't very effective"
我现在使用 print 来简化调试。
为了验证这一点,我已经完成了类型与类型的条件判断。
normal_2x_dmglist = []
normal_half_dmglist = ["Rock","Dragon"]
rock_2x_dmglist = ["Fire","Ice","Fly","Bug",]
rock_half_dmglist = ["Fighting","Ground","Steel"]
water_2x_dmglist = ["Fire","Ground","Rock"]
water_half_dmglist = ["Water","Grass","Dragon"]
grass_2x_dmglist = ["Water","Ground","Rock"]
grass_half_dmglist = ["Fire","Grass","Poison","Fly","Bug","Dragon","Steel"]
fighting_2x_dmglist = ["Normal","Ice","Rock","Dark","Steel"]
fighting_half_dmglist = ["Poison","Fly","Psychic","Bug","Fairy"]
ground_2x_dmglist = ["Fire","Electric","Poison","Rock","Steel"]
ground_half_dmglist = ["Grass","Bug"]
electric_2x_dmglist = ["Water","Fly"]
electric_half_dmglist = ["Electric","Grass","Dragon"]
def dmg_modifier(attack_type, receiver_pokemon_type):
if attack_type == "Normal" and receiver_pokemon_type in normal_2x_dmglist:
print "Normal deals 2x damage!"
elif attack_type == "Normal" and receiver_pokemon_type in normal_half_dmglist:
print "Normal deals 1/2 damage!"
elif attack_type == "Rock" and receiver_pokemon_type in rock_2x_dmglist:
print "Rock deals 2x damage!"
elif attack_type == "Rock" and receiver_pokemon_type in rock_half_dmglist:
print "Rock deals 1/2x damage!"
elif attack_type == "Water" and receiver_pokemon_type in water_2x_dmglist:
print "Water deals 2x damage!"
elif attack_type == "Water" and receiver_pokemon_type in water_half_dmglist:
print "Water deals 1/2x damage!"
elif attack_type == "Grass" and receiver_pokemon_type in grass_2x_dmglist:
print "Grass deals 2x damage!"
elif attack_type == "Grass" and receiver_pokemon_type in grass_half_dmglist:
print "Grass deals 1/2x damage!"
elif attack_type == "Fighting" and receiver_pokemon_type in fighting_2x_dmglist:
print "Fighting deals 2x damage!"
elif attack_type == "Fighting" and receiver_pokemon_type in fighting_half_dmglist:
print "Fighting deals 1/2x damage!"
elif attack_type == "Ground" and receiver_pokemon_type in ground_2x_dmglist:
print "Ground deals 2x damage!"
elif attack_type == "Ground" and receiver_pokemon_type in ground_half_dmglist:
print "Ground deals 1/2x damage!"
elif attack_type == "Electric" and receiver_pokemon_type in electric_2x_dmglist:
print "Electric deals 2x damage!"
elif attack_type == "Electric" and receiver_pokemon_type in electric_half_dmglist:
print "Electric deals 1/2x damage!"
else:
print "Type deals 1x damage - stays the same"
这很完美,只是非常重复和肮脏。
为了自动执行此操作,我计划将声明的 "attack type" 附加到“_2x_dmglist”,以便它自动调用列表。
首先我将声明的攻击类型小写,例如"Rock",使其变成rock。然后我将它附加到 _2x_dmglist,这样它就变成了 rock_2x_dmglist。然后它可以找到列表。
def dmg_modifier_2(attack_type, receiver_pokemon_type):
lower_case_attack_type = attack_type
lower_cased_attack_type = lower_case_attack_type.lower()
if attack_type == attack_type and receiver_pokemon_type in lower_cased_attack_type+_2x_dmglist:
print attack_type+"deals 2x damage"
elif attack_type == attack_type and receiver_pokemon_type in lower_cased_attack_type+_2x_dmglist:
print attack_type+"deals 1/2 damage"
else:
print "Not working"
输入:
dmg_modifier_2("Rock","Rock")
输出:
Traceback (most recent call last):
File "poke_game_test.py", line 98, in <module>
dmg_modifier_2("Rock","Rock")
File "poke_game_test.py", line 90, in dmg_modifier_2
finder = lower_case_attack_type.lower() + _2x_dmglist
NameError: global name '_2x_dmglist' is not defined
我理解回溯,但如何连接两个变量名以便整个过程自动化?在 PHP 中,我可以通过使用 + 号来做到这一点,我在这里尝试过。我也明白小写 "Rock" 也是一个字符串,而 _2x_dmglist 不是。如果变量连接在 python 中起作用,我真的很困惑,因为我在这里找不到类似的问题。
或者我的代码存在根本性错误?
谢谢!
虽然你可以这样做
globals().get(lower_cased_attack_type+'_2x_dmglist')
其中 globals 是 python 内置方法,它是 returns 调用方法中可访问的所有全局变量的字典。 但我建议将你的损坏列表存储在字典中,这将是一个更好的方法。
你应该做
damages = {
'rock_2x_dmglist': ["Fire","Ice","Fly","Bug",],
'rock_half_dmglist': ["Fighting","Ground","Steel"],
'water_2x_dmglist': ["Fire","Ground","Rock"],
'water_half_dmglist': ["Water","Grass","Dragon"],
}
那么你可以damages.get(lower_cased_attack_type+'_2x_dmglist')
我觉得是时候介绍一下了classes
,不过你也可以试试字典:
dict_2x_dmg = {
"Normal":set([]),
"Rock":{"Fire","Ice","Fly","Bug"},
"Water":{"Fire","Ground","Rock"},
"Grass":{"Water","Ground","Rock"},
"Fighting":{"Normal","Ice","Rock","Dark","Steel"},
"Ground":{"Fire","Electric","Poison","Rock","Steel"},
"Electric":{"Water","Fly"}
}
dict_half_dmg = {
"Normal":{"Rock","Dragon"},
"Rock":{"Fighting","Ground","Steel"},
"Water":{"Water","Grass","Dragon"},
"Grass":{"Fire","Grass","Poison","Fly","Bug","Dragon","Steel"},
"Fighting":{"Poison","Fly","Psychic","Bug","Fairy"},
"Ground":{"Grass","Bug"},
"Electric":{"Electric","Grass","Dragon"}
}
def dmg_modifier(attack_type, receiver_pokemon_type):
if receiver_pokemon_type in dict_2x_dmg[attack_type]:
dmg = "2x"
elif receiver_pokemon_type in dict_half_dmg[attack_type]:
dmg = "1/2x"
else:
dmg = "1x"
print "{0} deals {1} damage".format(attack_type, dmg)
>>> dmg_modifier("Water", "Ground")
Water deals 2x damage
您可以做的另一件事 - 是使用字典中的字典:
dict_dmg = {
"Normal":{"Rock":0.5,"Dragon":0.5},
"Rock":{"Fighting":0.5,"Ground":0.5,"Steel":0.5,"Fire":2,"Ice":2,"Fly":2,"Bug":2},
"Water":{"Water":0.5,"Grass":0.5,"Dragon":0.5,"Fire":2,"Ground":2,"Rock":2},
"Grass":{"Fire":0.5,"Grass":0.5,"Poison":0.5,"Fly":0.5,"Bug":0.5,"Dragon":0.5,"Steel":0.5,"Water":2,"Ground":2,"Rock":2},
"Fighting":{"Poison":0.5,"Fly":0.5,"Psychic":0.5,"Bug":0.5,"Fairy":0.5,"Normal":2,"Ice":2,"Rock":2,"Dark":2,"Steel":2},
"Ground":{"Grass":0.5,"Bug":0.5,"Fire":2,"Electric":2,"Poison":2,"Rock":2,"Steel":2},
"Electric":{"Electric":0.5,"Grass":0.5,"Dragon":0.5,"Water":2,"Fly":2}
}
def dmg_modifier2(attack_type, receiver_pokemon_type, dict_dmg):
dmg = dict_dmg[attack_type].get(receiver_pokemon_type, 1)
print "{0} deals {1}x damage".format(attack_type, dmg)
或者您甚至可以使用 pandas DataFrame
:
>>> import pandas as pd
>>> df_dmg = pd.DataFrame.from_dict(dict_dmg)
>>> df
Electric Fighting Grass Ground Normal Rock Water
Bug NaN 0.5 0.5 0.5 NaN 2.0 NaN
Dark NaN 2.0 NaN NaN NaN NaN NaN
Dragon 0.5 NaN 0.5 NaN 0.5 NaN 0.5
Electric 0.5 NaN NaN 2.0 NaN NaN NaN
Fairy NaN 0.5 NaN NaN NaN NaN NaN
Fighting NaN NaN NaN NaN NaN 0.5 NaN
Fire NaN NaN 0.5 2.0 NaN 2.0 2.0
Fly 2.0 0.5 0.5 NaN NaN 2.0 NaN
Grass 0.5 NaN 0.5 0.5 NaN NaN 0.5
Ground NaN NaN 2.0 NaN NaN 0.5 2.0
Ice NaN 2.0 NaN NaN NaN 2.0 NaN
Normal NaN 2.0 NaN NaN NaN NaN NaN
Poison NaN 0.5 0.5 2.0 NaN NaN NaN
Psychic NaN 0.5 NaN NaN NaN NaN NaN
Rock NaN 2.0 2.0 2.0 0.5 NaN 2.0
Steel NaN 2.0 0.5 2.0 NaN 0.5 NaN
Water 2.0 NaN 2.0 NaN NaN NaN 0.5
def dmg_modifier3(attack_type, receiver_pokemon_type, df_dmg):
dmg = df_dmg[attack_type][receiver_pokemon_type]
print "{0} deals {1}x damage".format(attack_type, dmg)
您可以执行以下操作:
damageDict = {}
damageDict['normal']: {'2x':[], 'half':["Rock","Dragon"]}
damageDict['rock']: {'2x':["Fire","Ice","Fly","Bug",], 'half':["Fighting","Ground","Steel"]}
damageDict['water']: {'2x':["Fire","Ground","Rock"], 'half':["Water","Grass","Dragon"]}
damageDict['grass']: {'2x':["Water","Ground","Rock"], 'half':["Fire","Grass","Poison","Fly","Bug","Dragon","Steel"]}
damageDict['fighting']: {'2x':["Normal","Ice","Rock","Dark","Steel"], 'half':["Poison","Fly","Psychic","Bug","Fairy"]}
damageDict['ground']: {'2x':["Fire","Electric","Poison","Rock","Steel"], 'half':["Grass","Bug"]}
damageDict['electric']: {'2x':["Water","Fly"], 'half':["Electric","Grass","Dragon"]}
# test case
attack_type = 'normal'
receiver_type = 'grass'
if receiver_type in damageDict[attack_type]['2x']:
print ("%s deals 2x damage!" % (attack_type)
elif receiver_type in damageDict[attack_type]['half']:
print ("%s deals 0.5x damage!" % (attack_type)
else:
print ("%s deals 1x damage!" % (attack_type)