简体   繁体   English

遍历两个列表

[英]Iterate over two lists

I want to iterate over 2 lists, and compare elements from the same list to see if higher or lower.我想遍历 2 个列表,并比较同一列表中的元素以查看更高或更低。 something like就像是

high = [5,7,8,4,2... 3] low = [16,4,8,1,48... 4]高 = [5,7,8,4,2...3] 低 = [16,4,8,1,48...4]

if number in high > than previous number, add it to the high_list if number in low < previous number, add it to the low_list如果高数>比前一个数,将其添加到高列表如果低数<前一个数,将其添加到低列表

output would be output 将是

high_list = [5,7,8] low_list = [16,4,1] high_list = [5,7,8] low_list = [16,4,1]

def iter_num (high,low):
    some_listH = []
    some_listL = []
    for H,L in zip(high,low):
        x = H +1
        if H > H[x]:
            H = H[x]
            some_listH.append(H)
        if L < L[x]:
            L = L[x]
            some_listL.append(L)
        return some_listH, some_listL

You have written:你写:

 H > H[x]

Either H is an element of a list, or it is a list. H 要么是列表的元素,要么是列表。 It is not both.两者都不是。

The two lists essentially have nothing to do with eachother.这两个列表本质上没有任何关系。 If they are not guaranteed to be the same length, I would not recommend handling them in the same loop.如果不能保证它们的长度相同,我不建议在同一个循环中处理它们。

This should do what you want for one list.这应该为一个列表做你想要的。 You can figure out from this how you want to handle the second list, put it into a function, etc.您可以从中找出您想如何处理第二个列表,将其放入 function 等。

list = [5,7,8,4,2,1,1,1,3]
some_listL = []

some_listL.append(list[0])
for x, y in zip(list, list[1:]):
    if y > x:
        some_listL.append(y)

print (some_listL)

To give credit where credit is due, I learned how to do this from here为了在应得的地方给予信任,我从这里学会了如何做到这一点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM