Comparing list values by index in Python

17,647

Solution 1

Convert your list2 to a dictionary that encodes the position given an item:

dic2 = dict((item,i) for i,item in enumerate(list2))

Now you can test for something being in the list by using x in dic2 and y in dic2 and use dic2[x] to get it's index in the list.

Edit: It goes against my better instincts, but here's the complete code. The first part is using what I showed above, turning a simple list into a lookup for the index. Next comes the standard if unintuitive way of initializing a 2D list. This is followed by your loops, using the ever handy enumerate function to assign an index to each name in the list.

Names = ['John', 'James', 'Barry', 'Greg', 'Jenny']
Results1 = ['James', 'Barry', 'Jenny', 'Greg', 'John']
Results2 = ['Barry', 'Jenny', 'Greg', 'James', 'John']

Order1 = dict((name,order) for order,name in enumerate(Results1))
Order2 = dict((name,order) for order,name in enumerate(Results2))

score = [[0]*len(Names) for y in range(len(Names))]

for i,name1 in enumerate(Names):
    for j,name2 in enumerate(Names):
        if name1 in Order1 and name2 in Order1 and Order1[name1] > Order1[name2] and name1 in Order2 and name2 in Order2 and Order2[name1] > Order2[name2]:
            score[i][j] = 1

Solution 2

lis1=[1,2,3,4,5,6,7,8]
num1=lis1[1]
num2=lis1[4]
lis2=[11,12,13,14,2,7,5,34]
if num1 in lis2 and num2 in lis2:
    if lis2.index(num1)>lis2.index(num2):
        #do something here
    else:
        #do something else

Solution 3

IF I understand what you are trying to do, here is an approach:

score={}

Names = ["John", "James", "Barry", "Greg", "Jenny"]
Results1 = ["James", "Barry", "Jenny", "Greg", "John"]
Results2 = ["Barry", "Jenny", "Greg", "James", "John"]

r1dict={name:i for i,name in enumerate(Results1)}
r2dict={name:i for i,name in enumerate(Results2)}

for i, ni in enumerate(Names):
    for j, nj in enumerate(Names):
        if r1dict[ni] > r2dict[nj]:
            score[(i,j)]=1

print(score)  

Prints:

{(0, 1): 1, (3, 2): 1, (4, 4): 1, (3, 3): 1, (2, 2): 1, 
 (4, 2): 1, (0, 3): 1, (0, 4): 1, (3, 4): 1, (0, 2): 1}
Share:
17,647
adohertyd
Author by

adohertyd

Updated on June 04, 2022

Comments

  • adohertyd
    adohertyd almost 2 years

    I need to see if 2 items from a list appears in another list, and if they do, compare the items by their position in the other list. Example in pseudo code:

    j=0
    for x in mylist #loop through the list
        i=0
        for y in mylist #loop through the list again to compare items
            if index of mylist[j] > index of mylist[i] in list1 and list2:
                score[i][j] = 1 #writes the score to a 2d array(numpy) called score
                i=i+1
            else: 
                score[i][j]=0
                i=i+1
    j=j+1
    

    Sample Narrative Description:

    Names = [John, James, Barry, Greg, Jenny]
    Results1 = [James, Barry, Jenny, Greg, John]
    Results2 = [Barry, Jenny, Greg, James, John]
    
    loop through Names for i
        loop through Names for j
            if (index value for john) > (index value for james) in results1 and 
               (index value for john) > (index value for james) results2:
                score[i][j] = 1
    

    Can someone please point me in the right direction? I've been looking at numerous list, array and .index tutorials but nothing seems to answer my question

  • adohertyd
    adohertyd almost 12 years
    Ashwini, would you mind redoing that using the narrative description I gave? I'm still not sure exactly what you did there.
  • adohertyd
    adohertyd almost 12 years
    Mark, could you give me an example of that based on the narrative description that I added above?
  • adohertyd
    adohertyd almost 12 years
    Thank you! It's very similar to Mark's answer below. The only difference is score is a 2d matrix (created using numpy). It appears that I have no choice but to create a dictionary from a list and then convert to a 2d array. How would this affect the program computationally, especially if the names list is large?
  • dawg
    dawg almost 12 years
    @adohertyd: If your numpy 2d array is already allocated with proper bounds, you can use a[i][j] in the same way that I am using the tuple. The array bounds must be in range however. (Python arrays do not autovivify)
  • abarnert
    abarnert almost 12 years
    +1 for directly mapping the pseudocode into Python in an obvious way. But it might be worth explaining that using an appropriate data structure (as the other answers do) allows both simpler and more efficient code, so you generally don't actually want this code (or the original pseudocode, for that matter).
  • Mark Ransom
    Mark Ransom almost 12 years
    @adohertyd, your example in the edited question is still very unclear.
  • adohertyd
    adohertyd almost 12 years
    The names list is a list of athletes. Results1 and Results2 are the respective positions of each athlete in 2 races. What I want to do is check if e.g. john beat james in both races and repeat this comparison for every pair of names. If so, I want to write the score 1 to score; this is for probability calculations later.
  • adohertyd
    adohertyd almost 12 years
    Thanks Mark that's a great answer. Don't worry about posting the whole code this isn't a homework assignment or anything it's part of a project that I'm working on and I'm exploring different things. Thanks for the help!