我真的嘗試過到處尋找解決問題的方法,但沒有成功找到任何東西。如果其他人已經問過這個問題,我很抱歉。到問題上。
我有兩個串列,其中包含要相互比較的值。我嘗試了以下選項。
list1 = [1,3,5,7,9]
list2 = [200,2]
x = 0
n = 0
y = 0
while x <= 9:
if list1[y] >= list2[n]:
print('TRUE')
x = x 1
y = y 1
if y > 4:
y = 0
n = n 1
else:
print('FALSE')
x = x 1
y = y 1
if y > 4:
y = 0
n = n 1
唯一的問題是,我需要遍歷值串列,而不是變數。
因此,我希望代碼看起來像這樣:
x = 0
n = [0,1]
y = [0,3]
z = len(n) len(y) - 1
while x <= z:
if list1[y] >= list2[n]:
print('TRUE')
x = x 1
else:
print('FALSE')
x = x 1
其中 n 和 y 是我要比較的數字的索引值。
這對我不起作用,我真的不知道該怎么做。
編輯:我不認為我必須通過文本解釋所有內容,因為我包含了兩組代碼。第一組代碼有效,它準確地顯示了我正在嘗試做的事情。第二個是我想要它做的。
Broken down further I want to know if list1[0]>=list2[0], followed by list1[3]>=list2[0], followed by list1[0]>=list2[1], followed by list1[3]>=list2[1]. The outputs that I expect are in the code I provided.
Apologies if I wasn't clear before. I need to call specific index positions that I will have in a separate list. This is the problem that I tried to outline in the second code.
uj5u.com熱心網友回復:
我想現在得到你想要做的事情。
首先,有兩個“原始”資料串列:
list1 = [1,3,5,7,9]
list2 = [200,2]
然后,有兩組“興趣指數”:
y = [0, 3] # indices of interest for list1
n = [0, 1] # indices of interest for list2
我認為以下可以實作您想要的:
product = [(index1, index2) for index2 in n for index1 in y] #cartesian product
for index1, index2 in product:
if list1[index1] >= list2[index2]:
print("True")
else:
print("False")
或者,如果不需要笛卡爾積,只需在嵌套回圈中執行:
for index2 in n:
for index1 in y:
if list1[index1] >= list2[index2]:
print("True")
else:
print("False")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/427804.html
標籤:python list variables while-loop
