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.
我有一个列表my_list = [] 在这个列表中有一些 int,所以my_list = [1,2,3,4] 现在我想一个一个地处理每个数字,而不知道只有 4 个 int。列表中可能有 1000 件事情。我想过这样的事情:
my_list = []
my_list = [1,2,3,4]
i = len(self.my_list) while i > 0: print(my_list[i]) i -=1
但我收到了这个错误:IndexError: list index out of range
IndexError: list index out of range
您可以做的是遍历列表中的每个项目:
for i in my_list: print(i,i**2)
我通常使用这种方法:
my_list = [1,2,3,4] i=0 while(i < len(my_list)): print(my_list[i]) i+=1
当您可以控制索引时。