Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8198945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:04:14+00:00 2026-06-07T06:04:14+00:00

I need to see if 2 items from a list appears in another list,

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:04:15+00:00Added an answer on June 7, 2026 at 6:04 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to copy a sub set of items from one list to another.
I'm outputting a list of items from an array in Ruby. I need to
i need to see a folder (svn) from file system in eclipse (is there
I have two Ruby arrays, and I need to see if they have any
I need to list (and later update) all the items present in the table
I need to be able to store a large list of ordered items in
i need some help with a menu. See the following menu: menu The code
I am using knockout.js and upshot.js to get a list of items from API
I am retrieving a list of items from a URL and displaying them in
I need to compare two lists where each list contains about 60,000 objects. what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.