这是我的代码
this_item = "1234"
this_is_a_list = []
for item in this_is_a_list:
if this_item == item:
print("this item is already in the list")
else:
this_is_a_list.append(str(this_item))
print("I want to create a new variable with the name of this_item")
我要执行的操作是检查this_item是否在this_is_a_list中,如果是,请执行类似打印“此项目已在列表中”的操作,但是如果该项目不在this_is_a_list中,则我要创建一个名为this_item等于的新变量,我希望该变量等于0。仅就上下文而言,this_item可能会发生很大变化。
答案 0 :(得分:1)
改为使用字典:
myDict = {}
this_item = "1234"
this_is_a_list = []
for item in this_is_a_list:
if this_item == item:
print("this item is already in the list")
else:
#this_is_a_list.append(str(this_item))
#print("I want to create a new variable with the name of this_item")
myDict[str(this_item)] = 0
稍后您可以检索以下值:
print(myDict['1234'])