Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在研究 python 程序,我想要这样的东西:
for i in range(1,n+1): var(a+str(i)) = input() #do something
所以变量是a1,a2,a3等。这可以做到吗,怎么做?
你不想那样做,相信我。你想用字典
vars = {} for i in range(1,n+1): vars[i] = input() #do something
或者,由于在您的特殊情况下所有数字都是连续的,因此列表:
vars = [None] # initialize vars[0] with None for i in range(n): vars.append(input()) #do something
现在您可以访问您的变量vars[2],例如vars[5]等。
vars[2]
vars[5]