如果字符串在另一个列表中,则调用该列表中的下一个值 (Python 3)
If string is in another list, call on the next value in said list (Python 3)
假设我在列表 list1 中有一个值 callfunc 我想调用列表中的下一个值,但仅如果 callfunc 在 list2 中(它是)。我有这个代码:
for instr in list1:
if instr in list2:
eval(instr(list1[list1.index(instr)+1]))
我收到此错误消息:
eval(instr(list1[list1.index(instr)+1]))
TypeError: 'str' object is not callable
我试图将输出作为函数,而不是字符串:
callfunc(75)
这将在脚本中在 75 上调用 callfunc。
列表 1 和 2:
list1 = [callfunc, 75]
list2 = [callfunc]
我不知道从这里去哪里。将不胜感激。
callfunc 是一个 string.
尝试:
eval(instr+"("+list1[list1.index(instr)+1]+")")
这将创建您想要的调用字符串。
然后阅读 this 个问题及其答案。
我不太明白你为什么在这里使用eval
。如果你想调用该函数,只需这样做:
for ifunc in list1:
if ifunc in list2:
d = ifunc(list1[list1.index(ifunc)+1])
print(d)
假设我在列表 list1 中有一个值 callfunc 我想调用列表中的下一个值,但仅如果 callfunc 在 list2 中(它是)。我有这个代码:
for instr in list1:
if instr in list2:
eval(instr(list1[list1.index(instr)+1]))
我收到此错误消息:
eval(instr(list1[list1.index(instr)+1]))
TypeError: 'str' object is not callable
我试图将输出作为函数,而不是字符串:
callfunc(75)
这将在脚本中在 75 上调用 callfunc。
列表 1 和 2:
list1 = [callfunc, 75]
list2 = [callfunc]
我不知道从这里去哪里。将不胜感激。
callfunc 是一个 string.
尝试:
eval(instr+"("+list1[list1.index(instr)+1]+")")
这将创建您想要的调用字符串。
然后阅读 this 个问题及其答案。
我不太明白你为什么在这里使用eval
。如果你想调用该函数,只需这样做:
for ifunc in list1:
if ifunc in list2:
d = ifunc(list1[list1.index(ifunc)+1])
print(d)