2017-03-28 168 views
0

同一指数的eliments更大,我有两个列表:蟒蛇,检查是否在列表1的所有元素都比列表2

x = [50,25,30] 
y = [25,30,50] 

我的新节目,我怎么能确定,如果x [0]> = y [0],x [1]> = x [1],带有循环或其他函数?

我想避免简单:

x[0] >= y[0] 
x[1] >= y[1] 
x[2] >= y[2] 

因为这些清单可以追加。

回答

2

我分别改名为你的列表,以list1list2

result = all(x >= y for x, y in zip(list1, list2)) 

这里all(iterable)检查iterable所有元素是否'truthy'

0

我的溶剂将基于每个清单的长度相同的事实。

if len(x) == len(y): 
    test_container = 0 #will store the number of trues 
    for i in range(len(x)): 
     if x[i] > y[i]: 
      test_container += 1 
    if test_container == len(x): 
     print("All elements in x are bigger than the correspondent in y") 
    else: 
     print("False") 
0
result = True 
for xi, yi in zip(x, y): 
    if xi < yi: 
     result = False 
     break 

结果将包含答案。

如果你想这样做,作为一个功能:

def compare(x, y): 
    for xi, yi in zip(x, y): 
     if xi < yi: 
      return False 
    return True 
相关问题