简体   繁体   中英

Python comparing individual lists of lists elements

I'm trying to compare elements of lists of lists to find which values are larger/equal to each other.

x = [[8, 12.5], [1.5, 12.6], [35, 137], [3.8, 145], [48, 1.8], [15, 67]]
y = [[0, 14], [6.4, 224], [8.5, 123], [6.5, 26.1], [4.1, 57], [58, 61]]

if x[i,0] > y[i,0]:
   #do this
elif x[i,0] < y[i,0]:
   #do that
elif x[i,0] == y[i,0]:
   #do other 

When I try to compare the elements, I get an error message that says:

TypeError: list indices must be integers, not tuple

Is there a way to compare tuple/list elements? I see all kinds of posts that find common elements or comparing whole lists, but not comparing elements. I'm using python 2.7.3

Almost. You get a list with the first index and then you can get an element out of that list with the second:

if x[i][0] > y[i][0]:

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