473,788 Members | 2,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3276
>>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...@yaho o.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
1826
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 programming. Having some difficulty understand some items. For lists, I understand this: C= print C some But I can't seem to do this:
8
2216
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 the container or whether it also gets deleted in the process), I looked up the STL code. Erase certainly does not delete the memory associated with the element. However, it appears that the destructor on the element is invoked. I wonder why it has to be this way. In my opinion, this renders...
34
4176
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. I create an instance of a Class A, and "push_back" a copy of this into a vector V. This is repeated many times in an iterative process. Ok whenever I "push_back" a copy of Class A, I also want to assign a pointer contained in an exisiting instance of a Class B to this
9
1603
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;" onChange="Change()"> <option value="0"> &lt;ALL&gt <option value="1000012" >Food Sales <option value="1000014" >Dessert Sales
1
1571
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 have so at the moment of creation each of one I append them to one list. So I get list wchich consist of lists of objects. And now i want to get common objects of this list. How to do it? Grzegorz
6
10375
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: sorted(uniter(, , ])) must return . I tried the following implementations: from itertools import chain
3
34604
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 number of common elements is: 3 The number of common elements is: 3 I get
6
3073
by: John A Grandy | last post by:
InstrumentPropertyEntity string Name InstrumentEntity string Name List<InstrumentPropertyEntityInstrumentProperties For a List<InstrumentEntity, I want to determine the List<InstrumentPropertyEntityheld in common by all members.
6
1743
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 arrays. For example, I could have the three arrays: 1, 3, 2, 8, 5 3, 6, 1
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.