Hi, I would like to compare elements in two seperate Lists for example: - List1=[['Primi ', ' Movida ', ' 5'], ['Primi ', ' CCHQ ', ' 40'], ['Primi ', ' Chicago ', ' 45']]
- List2=[['Chicago ', ' The Doors ', ' 20'], ['CCHQ ', ' The Doors ', ' 10']]
What I understand is that the syntax should look like this: - List3
-
for i in List2:
-
for j in List1:
-
if i[0]==j[1]
-
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
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: - 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 - d=[i[1] for i in List1 if i[1]==j[0] for j in List2]
-
return d
-
f=[elem for elem in d if elem==elem in list1
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??
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: - 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.
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!!
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.
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: - List3 = [item for item in List1 if item[1].strip() in [item[0].strip() for item in List2]]
reversed: - List3=[item for item in List2 if item[0].strip() in[item[1].strip() for item in List1]]
Thanks for the help bv
Ava
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
| |