[英]Python code to loop through a list which has 2 variables per cycle
我試圖了解如何添加 2 個變量列表並讓 python 代碼循環通過它,就像循環一樣
例如
[[1, 9], [2, 9], [3, 7],[4. 6],[5, 9],[6, 9]]
在下面的示例中,它的12,9
將使用 12 填充 M 並且它將遍歷代碼 9 次,因此使長度 = 9 然后在完成后移動到下一次迭代 2,9 等
#This is how many iterations are needed
list = [1,2,3,4,5,6,7,8,9]
# Getting length of list
length = len(list)
i = 0
# Iterating using while loop
while i < length:
#This is the ID
m = '12'
我認為你正在嘗試做這樣的事情:
mylist = [[1, 9], [2, 9], [3, 7], [4, 6], [5, 9], [6, 9]]
for first, second in mylist:
M = first
for i in range(second):
print(f'The ID is {M}, iteration number {i}')
The ID is 1, iteration number 0
The ID is 1, iteration number 1
The ID is 1, iteration number 2
The ID is 1, iteration number 3
The ID is 1, iteration number 4
The ID is 1, iteration number 5
The ID is 1, iteration number 6
The ID is 1, iteration number 7
The ID is 1, iteration number 8
The ID is 2, iteration number 0
The ID is 2, iteration number 1
The ID is 2, iteration number 2
The ID is 2, iteration number 3
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.