472,331 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

common elements between list of lists and lists

Hello,

I am a beginner in python.
following program prints the second element in list of lists 4 for the
first elements in list 4 that are common with the elements in list 5
list4 = [['1', 'a'],['4', 'd'],['8', 'g']]
list5 = ['1', '2', '3']

for j in list4:
for k in list5:
if j[0] == k:
print j[1]

Result: a

I would like to do the same thing starting with following lists, where
the numbers in list 5 are without ''. Is there a way to convert
integers in a list to integers in '' ? This is based on a situation
where I want to find common numbers between a list and a list of lists
where the numbers in the list are without '' and the numbers in the
list of lists are with ''
list4 = [['1', 'a'],['4', 'd'],['8', 'g']]
list5 = [1, 2, 3]

This might be a stupid question, but anyway, thanks for your answer
It is not my first post on this site. In some way it is not possible
to react on the messages that I receive to thank the persons that
react. Anyway, thanks a lot
Jul 17 '08 #1
2 3204
>>list4 = [['1', 'a'],['4', 'd'],['8', 'g']]
>>list5 = [1, 2, 3]
set5 = set(list5)
[x for n, x in list4 if int(n) in set5]
['a']

Jul 17 '08 #2
On Jul 17, 4:30 pm, antar2 <desoth...@yahoo.comwrote:
Hello,

I am a beginner in python.
following program prints the second element in list of lists 4 for the
first elements in list 4 that are common with the elements in list 5

list4 = [['1', 'a'],['4', 'd'],['8', 'g']]
list5 = ['1', '2', '3']

for j in list4:
for k in list5:
if j[0] == k:
print j[1]

Result: a

I would like to do the same thing starting with following lists, where
the numbers in list 5 are without ''. Is there a way to convert
integers in a list to integers in '' ? This is based on a situation
where I want to find common numbers between a list and a list of lists
where the numbers in the list are without '' and the numbers in the
list of lists are with ''

list4 = [['1', 'a'],['4', 'd'],['8', 'g']]
list5 = [1, 2, 3]

This might be a stupid question, but anyway, thanks for your answer
It is not my first post on this site. In some way it is not possible
to react on the messages that I receive to thank the persons that
react. Anyway, thanks a lot
By "integer without ''" you mean integers not embraced by single
quotes, right?

Actually, '1' is a string, not an integer. If you want to normalize
the first elements of all the lists in list4, just use int() to
convert them.

That is:

list4 = [['1', 'a'],['4', 'd'],['8', 'g']]
list5 = ['1', '2', '3']

set5 = set(map(int, list5))
list4 = [[int(i[0]), i[1]] for i in list4]

for j in list4:
if j[0] in set5: print j[1]

You can have a try :)
Jul 17 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Matthew Sims | last post by:
Python Newbie here. This is my first time learning object-oriented programming and trying to break out of the usual Korn/Perl/PHP style of...
8
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it....
9
by: john woo | last post by:
Hi Please see the following simple script: <TD align="Left" width="30%" > <select name="DEPARTMENT" SIZE ="1" style="width:200px;"...
1
by: Grzegorz ¦lusarek | last post by:
Hi all. I my application i have situation when i have some lists and i must get from lists common elements. Exacly i don't know how many list I...
6
by: Tekkaman | last post by:
I have a list of lists and I want to define an iterator (let's call that uniter) over all unique elements, in any order. For example, calling: ...
3
JoeMac3313
by: JoeMac3313 | last post by:
My Assignment was to compare two lists and print out the number of elments that are same. It is supposed to look like this Week 7 Homework The...
6
by: John A Grandy | last post by:
InstrumentPropertyEntity string Name InstrumentEntity string Name List<InstrumentPropertyEntityInstrumentProperties For a...
6
by: Peter | last post by:
Hi I have a number of arrays of longs, from which I need to find a single array which only contains the values which appear in all the original...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

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.