简体   繁体   中英

how to correlate two lists in python?

I have two lists with values stored in sub-lists. I need to compare each element of each sub-list in list one with specific values and if the conditions are satisfied I need to extract the corresponding values from the sub-lists of second list.

I used the code below. However, I have problem when the code gives the values of second list. Actually, it prints all the first elements of all sub-lists in the list 2 instead of giving the first element of the sub-list related to the sub-list in the first list.

list1 = [[6.47, 1.34, 1.35],[5.41, 1.4, 1.39],[1.64, 2.43, 1.63]]
list2 = [[5820.0, 7870.0, 7600.0, 7590.0],[4660.0, 7770.0, 7480.0, 
7540.0],[4340.0, 9000.0, 5690.0, 50.0]]
for S in list1:
     if 2.43 <= (S[0]) or 1.01 > (S[0]):
      print(S[0])
      print('Single Axle')
      for W in list2:
           result = W[0]
           print(result)

My goal is to get results in this way:

6.47
single axle
5820
5.41
single axle
4660

But the answer I get is:

6.47
Single Axle
5820.0
4660.0
4340.0
5.41
Single Axle
5820.0
4660.0
4340.0

You can try with this:

list1 = [[6.47, 1.34, 1.35],[5.41, 1.4, 1.39],[1.64, 2.43, 1.63]]
list2 = [[5820.0, 7870.0, 7600.0, 7590.0],[4660.0, 7770.0, 7480.0, 
7540.0],[4340.0, 9000.0, 5690.0, 50.0]]

for i, v in enumerate(list1):
     if 2.43 <= (v[0]) or 1.01 > (v[0]):
        print(v[0])
        print('Single Axle')
        print(list2[i][0])

The result is:

6.47
Single Axle
5820.0
5.41
Single Axle
4660.0

Your issue is the:

for W in list2:
    result = W[0]
    print(result)

This is iterating over list2 , and printing the first item of every sublist. Instead, you only want to select the corresponding sublist.

list1 = [[6.47, 1.34, 1.35],[5.41, 1.4, 1.39],[1.64, 2.43, 1.63]]
list2 = [[5820.0, 7870.0, 7600.0, 7590.0],[4660.0, 7770.0, 7480.0, 
7540.0],[4340.0, 9000.0, 5690.0, 50.0]]
for Sindex, S in enumerate(list1):
    if 2.43 <= (S[0]) or 1.01 > (S[0]):
        print(S[0])
        print('Single Axle')
        print(list2[Sindex][0])

The enumerate() allows concise index retrieval, then that index is used to reference list2 .

For every sub_list in list1 you are looping over all sub_lists in list2 . By using the zip() function you are iterating simultaneously over both lists and you can then directly extract the critical elements.

list1 = [
    [6.47, 1.34, 1.35],
    [5.41, 1.4, 1.39],
    [1.64, 2.43, 1.63],
    ]
list2 = [
    [5820.0, 7870.0, 7600.0, 7590.0],
    [4660.0, 7770.0, 7480.0, 7540.0],
    [4340.0, 9000.0, 5690.0, 50.0],
    ]

for list1_group, list2_group in zip(list1, list2):
    critical_element = list1_group[0]
    if critical_element < 1.01 or critical_element >= 2.43:
        print(critical_element)
        print('Single Axle')
        print(list2_group[0])

Gives output

6.47
Single Axle
5820.0
5.41
Single Axle
4660.0

I'm not exactly sure what you want from the code that was posted. You've said that you want to check each element of the sublist, but your code will only look at the first element of each sublist.

This will get the indexes of each value in the sublists in list1 which are less than 1.01 or greater than or equal to 2.43 and print them out with the corresponding values at the same indexes in list2 :

list1 = [[6.47, 1.34, 1.35],[5.41, 1.4, 1.39],[1.64, 2.43, 1.63]]
list2 = [
   [5820.0, 7870.0, 7600.0, 7590.0],
   [4660.0, 7770.0, 7480.0, 7540.0],
   [4340.0, 9000.0, 5690.0, 50.0],
]

indexes = [
    (i, j)
    for i, sub_list in enumerate(list1)
    for j, num in enumerate(sub_list)
    if num < 1.01 or num >= 2.43
]

for i in indexes:
    print(list1[i[0]][i[1]])
    print('Single Axle')
    print(list2[i[0]][i[1]])

This will give you the following output:

6.47
Single Axle
5820.0
5.41
Single Axle
4660.0
2.43
Single Axle
9000.0

Although I think it would be neater to print the related data out on a single line:

for i in indexes:
    print(list1[i[0]][i[1]], 'Single Axle', list2[i[0]][i[1]])

Output:

6.47 Single Axle 5820.0
5.41 Single Axle 4660.0
2.43 Single Axle 9000.0

EDIT : From the latest comment, there are different conditions that need to be evaluated and produce different outputs? If this is the case, this might be easier using for loops.

for i, sub_list in enumerate(list1):
    for j, num in enumerate(sub_list):
        if num < 1.01 or num >= 2.43:
            print(num, 'Single Axle', list2[i][j])
        elif # some other condition:
            print(num, 'Different condition', list2[i][j])

You can use elif to keep adding new conditions if they are mutually exclusive. If the conditions overlap, use separate if statements instead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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