473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to compare elements in two separate lists

Avatar19
Hi, I would like to compare elements in two seperate Lists for example:
Expand|Select|Wrap|Line Numbers
  1. List1=[['Primi ', ' Movida ', ' 5'], ['Primi ', ' CCHQ ', ' 40'], ['Primi ', ' Chicago ', ' 45']]
Expand|Select|Wrap|Line Numbers
  1. List2=[['Chicago ', ' The Doors ', ' 20'], ['CCHQ ', ' The Doors ', ' 10']]
What I understand is that the syntax should look like this:
Expand|Select|Wrap|Line Numbers
  1. List3
  2. for i in List2:
  3.     for j in List1:
  4.              if i[0]==j[1]
  5.                  List3.append(j)

What i specifically would like to do is that in List1. If List1[i][1] is found in List2[j][0] then i would like to pull out the List1 elements for which this is True.
Please help I am very confused!
Thanks
May 7 '10 #1

✓ answered by bvdet

Something is not right about the code Nonemsludo posted. A return statement out of the blue?

He was trying to show you a list comprehension. Here's my contribution:
Expand|Select|Wrap|Line Numbers
  1. List3 = [item for item in List1 if item[1].strip() in [item[0].strip() for item in List2]]
Apparently you have some extra spaces. The strip() method gets rid of the spaces for the comparison to work.

6 16881
Expand|Select|Wrap|Line Numbers
  1.  d=[i[1] for i in List1 if i[1]==j[0] for j in List2]
  2. return d 
  3.  f=[elem for elem in d if elem==elem in list1
May 7 '10 #2
Thanks for the reply, i did manage to figure it out eventually but I am interested in this syntax, I've tried it an it doesn't seem to work??
May 9 '10 #3
bvdet
2,851 Expert Mod 2GB
Something is not right about the code Nonemsludo posted. A return statement out of the blue?

He was trying to show you a list comprehension. Here's my contribution:
Expand|Select|Wrap|Line Numbers
  1. List3 = [item for item in List1 if item[1].strip() in [item[0].strip() for item in List2]]
Apparently you have some extra spaces. The strip() method gets rid of the spaces for the comparison to work.
May 10 '10 #4
Hey bv thanks for the help, but just one thing I noticed is that with using "item" as the iteration variable in both lists seems to confuse the output, but otherwise perfect solution, Thanks!!
May 10 '10 #5
bvdet
2,851 Expert Mod 2GB
I am thinking the inner list comprehension is executed first then the outer. I like using item as the variable name for list iteration.
May 10 '10 #6
Oh ok, I see it does work I just reversed the Lists and it gave the right result, is this how it was meant to be?

original:
Expand|Select|Wrap|Line Numbers
  1. List3 = [item for item in List1 if item[1].strip() in [item[0].strip() for item in List2]]
reversed:
Expand|Select|Wrap|Line Numbers
  1. List3=[item for item in List2 if item[0].strip() in[item[1].strip() for item in List1]]

Thanks for the help bv
Ava
May 10 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Peter Galfi | last post by:
I am trying to make a compare between two lists of numbers using the SequenceMatcher, however I would need somehow to override the method which is used by SequenceMatcher to determine if two elements...
1
by: Viswa | last post by:
PythonGurus, I would like to read the ascii data from a file with many columns into a list. I want the type to be float. data=open('dat1.dat','r') lines=data.readlines() print lines
6
by: Robin Siebler | last post by:
I have two directory trees that I want to compare and I'm trying to figure out what the best way of doing this would be. I am using walk to get a list of all of the files in each directory. I...
8
by: Nickolay Kolev | last post by:
Hi all, I have a list whose length is a multiple of 3. I want to get a list of tuples each of which has 3 consecutive elements from the original list, thus packing the list into smaller...
41
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and...
11
by: Robert Latest | last post by:
Hello, From a list of strings I want to delete all empty ones. This works: while '' in keywords: keywords.remove('') However, to a long-term C programmer this looks like an awkward way of...
5
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I have a Container that is an an Array List of Class Each ArrayList element can be the class or a another ArrayList of Class So there the ArrayList could look like Element 1 - Class...
2
by: Amanda Ahl | last post by:
How can I divide a file with emails divided with headers like this: From: jdakl@hotmail.com Sent: 7th aprill 2099 To: hjfdaso@hotmail.com email text ...into separate lists in one big list...
1
by: Johan Novak | last post by:
Hi! I'm learning python and I have a question about matix. ef __init__(self,colors=, forms=, numbers=, grades=): self.cardList = self.playerCardList = self.lista3...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.