Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-使用带有自定义变量命名的for循环_Python_For Loop - Fatal编程技术网

Python-使用带有自定义变量命名的for循环

Python-使用带有自定义变量命名的for循环,python,for-loop,Python,For Loop,我试图用Python完成一项非常简单的任务,但似乎无法完成或找到一种方法来完成。我正在尝试一个for循环,其中我的变量被设置为另一个具有自定义编号的变量。这有点难以解释,但我的代码可以更好地解释: aha_aha1 = "1" aha_aha2 = "wouh" aha_aha3 = "yes !" test = "my man !" for i in range (0, 4): if aha_aha{i} = "yes !": test[i] == aha_aha{i}.for

我试图用Python完成一项非常简单的任务,但似乎无法完成或找到一种方法来完成。我正在尝试一个for循环,其中我的变量被设置为另一个具有自定义编号的变量。这有点难以解释,但我的代码可以更好地解释:

aha_aha1 = "1"
aha_aha2 = "wouh"
aha_aha3 = "yes !"
test = "my man !"

for i in range (0, 4):
  if aha_aha{i} = "yes !":
     test[i] == aha_aha{i}.format(i)

我希望test[I]只有在满足某些条件时才是aha_-aha[I],但我上面的代码显然不起作用。有什么帮助吗?

如果你真的想要那些“啊哈”:

d = {"aha_aha1" : "1", "aha_aha2" : "wouh", "aha_aha3" : "yes !"}
test = "my man !"

for i in range (0, 4):
  temp = "aha_aha"+str(i)
  if d.get(temp) == "yes !":
    #Do something

事实上,我不明白你在做什么,但至少从这里开始:

aha_aha_list = ["1", "wouh", "yes !"]
test = "my man !"

for aha_aha in aha_aha_list:
    if aha_aha is "yes !":
        test = aha_aha

print(test)
输出:

yes !
你想这样吗?在python中str类型无法更改,所以你应该将str更改为list,然后将chat list更改为str。 d={'aha_aha1':'1','aha_aha2':'wouh','aha_aha3':'yes!' 测试=我的人! 对于0,4范围内的i: 如果d.getaha_aha{i}.formati=i,则无==yes!: 测试=列表测试 test[i]=d.getaha_aha{i}.formati=i 测试=.jointest
打印测试我的是!一你不能给变量命名啊哈啊哈1,啊哈啊哈2。。然后根据名称中的数字选择它们,即python中不存在aha_aha{i}。你必须做一个列表并通过indexOh访问它们,这太可惜了。谢谢,我会的!使用字典。我从外部源获取变量,这些变量都是类似于这样的东西,比如1,2等等。如果你知道变量将以某种已知的固定格式到达,这应该会有所帮助