Re: common elements between list of lists and lists



On Jul 17, 4:30 pm, antar2 <desoth...@xxxxxxxxx> wrote:
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 :)
.



Relevant Pages

  • Re: Iterating & Comparing String Data Sets Efficiently?
    ... function to traverse lists in parallel, you would get O) ... we found that first elements of the lists are equal, ... add first element to the sorted-intersection of the CDRs of the ...
    (comp.lang.lisp)
  • common elements between list of lists and lists
    ... 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 ... where I want to find common numbers between a list and a list of lists ...
    (comp.lang.python)
  • Re: Iterating & Comparing String Data Sets Efficiently?
    ... Victor Kryukov wrote: ... lists, that will lead to Ooperations. ... when you traverse both lists only *once* - e.g. if ... we found that first elements of the lists are equal, ...
    (comp.lang.lisp)
  • Re: What are my options for dictionary hashing?
    ... their more common words with the letter 'A,' their next-most common ... The first is your assumption of 10 levels for 1024 deep vocabularies, ... then the most frequently used words in each hash string ... lists are ordered randomly. ...
    (comp.lang.forth)
  • Re: [fw-wiz] Re: Best Practices
    ... >of them, it is in getting others to link to those lists, read and then ... >frugallity have set the common mindset to; don't foot the bill till the ... target. ... My initial target would be admin and security people in small ...
    (Firewall-Wizards)