Home > python > Compare two python lists and look for conatains

Compare two python lists and look for conatains

March 13Hits:1
listA = ['ab', 'bv', 'cd'] listB = ['ab_cd', 'AB_vd', 'ab_ud', 'bV_db', 'bv_de', 'cd_scd'] 

and I have a if condition like

for x in listA :   if x in listB:     print "Have"   else:     print "dun have" 

But it is always looking for the exact values, but what i need to achieve is a conatains type of search in the list. (Ex: ab actually in listB ('ab_cd', 'AB_vd', 'ab_ud'). Any idea guys? Thanks in advance


You can save some hassle with the any function. It returns True if any element of the argument iterable is True, False otherwise. The and version of this is called all.

listA = ['ab', 'bv', 'cd', 'qsz']
listB = ['ab_cd', 'AB_vd', 'ab_ud', 'bV_db', 'bv_de', 'cd_scd']

for want in listA:
    if any(want in target for target in listB):
        print "Have", want, "in some listB item"
        print want, "... we don' got dat one"


Have ab in some listB item
Have bv in some listB item
Have cd in some listB item
qsz ... we don' got dat one

You can do this:

l = ["ListB does have {}".format(a)
      for a in listA for b in listB if a.lower() in b.lower()]

for el in l:
    print el

Notice the use of .lower() so as to match all possible combinations. (aA-Aa-AA-aa)

Tags:python, list, django

Related Articles

  • Compare two python lists and look for conatainsMarch 13

    listA = ['ab', 'bv', 'cd'] listB = ['ab_cd', 'AB_vd', 'ab_ud', 'bV_db', 'bv_de', 'cd_scd'] and I have a if condition like for x in listA : if x in listB: print "Have" else: print "dun have" But it is always looking for the exact values

  • Mathematica vs Python - how does it compare to Python's scientific computing suite?June 16

    Python's family of packages for scientific computing has matured rapidly. I can pretty much replicate all of Mathematica's functionalities, but with production level and open-source code using the following: Numpy, Scipy, Sklearn for math and algorit

  • Output arrays in format that can be compared with Python numpyDecember 3

    Is there a way to convert an array in Mathematica such that its output format resembles that of Python's numpy arrays? I want to go from: {{0.1875, -0.0625, 0.0625, -0.0625, -0.1875, 0.0625, -0.0625, 0.0625, 0, 0, -0.125, 0.0625, 0, 0}, {-0.0625, 0.1

  • Comparing and updating Python scripts on local machine with latest version on server using shell scriptsMarch 17

    I'm got a cubieboard machine running cubian.There's a Python service that runs on start-up that interfaces between the various devices connected to the machine (UART, USB etc) and the web browser. This machine will eventually cloned into other machin

  • Newbie, deciding Python or Erlang January 6

    I'm a Administrator (unix, Linux and some windows apps such as Exchange) by experience and have never worked on any programming language besides C# and scripting on Bash and lately on powershell. I'm starting out as a service provider and using multi

  • If we can do functional programming with Python, do we need a specific functional programming language? February 2

    Using generators and lambda, we can do functional programming with Python. You can also achieve the same thing with Ruby. So the question is: why do we need specific functional programming languages such as Erlang, Haskell, and Scheme? Is there anyth

  • Perl or Python, better suited for Unix system automation?March 9

    I have been using bash for most of my system administration tasks. I also know a bit of perl. Should I learn Python or Perl is better for system automation. So far from my experience learning perl has been easy. --------------Solutions------------- S

  • Wanting to start development for Ubuntu (and most Linux distributions in general), which is suitable? Python or Vala? April 25

    I'm a computer science graduate, and learned languages like C, C++, C# and Java. Though I have more experience with Java. I want to develop for Linux and I'd prefer to develop for GNOME, I'm confused between choosing one from the Python and Vala. Aft

  • Failing to import python modules only on specific userOctober 16

    I've been trying to figure out this for ages. When I run certain python programs they would crash saying that certain module can't be imported, while it's actually installed and should be importable. For example: pip3 would crash with ImportError: ca

  • Is there an equivalent to emacs' rx macro in python?January 15

    The rx macro in emacs (see http://www.emacswiki.org/emacs/rx and http://doc.endlessparentheses.com/Fun/rx) makes it possible to specify regular expressions in a modular and readable way (at least you don't have to take care about quoting issues). For

  • database-driven requests: python or node.js? January 25

    I'm designing and developing a back end that will perform several queries involving big amount of data and several joins for each request. DB turns out to be the bottleneck when it comes to performances but all I can do is optimizing the coming reque

  • Converting Python GDALDataset to C++ GDALDatasetJanuary 26

    I'm currently writing a C++ extension to python to speed up a raster data viewer which is running extremely slowly with our large datasets. I've managed to get a small speedup by writing a C++ extension, but after some investigation the slowest part

  • How to compare pip versioning with debian versioning?January 27

    For example if we have a package with version like "1.0.0", then it changes versioning to "0.0.10" epoch will be added. So if we compare it in package way "1:0.0.10" > "1.0.0" there is true. But pip does not has

  • Equivalent urllib.parse.quote() in python 2.7February 2

    What is the equivalent of urllib.parse.quote It's urllib.urlencode()? Thanks --------------Solutions------------- I think you are looking for urllib.pathname2url. Compare: Python 3, urllib.parse.quote: >>> urllib.parse.quote('abc def/foo?bar=baz'

  • how to export firefox 'about:config' into plain text file?July 19

    so, how can I export the WHOLE 'about:config' from firefox into plain text file. Screenshot would be good enough although text file is preferred. My favorite firefox extention sceengrab cannot handle it. Please note that I am on Mac and I cannot find

  • Why isn't Lisp more widespread?March 19

    I am starting to learn Scheme by the SICP videos, and I would like to move to Common Lisp next. The language seems very interesting, and most of the people writings books on it advocate that it has unequaled expressive power. CL seems to have a decen

  • 40 million tweets from 200k usersJanuary 10

    mysql> select count(username) from users where no_tweets > 50; +-----------------+ | count(username) | +-----------------+ | 282366 | +-----------------+ 1 row in set (5.41 sec) mysql> select sum(no_tweets) from users where no_tweets > 50; +--

  • Why aren't there native Javascript interpreters for Windows/Mac/Linux?

    Why aren't there native Javascript interpreters for Windows/Mac/Linux?August 27

    It seems to me it would be very useful to use Javascript for general server side scripting tasks as it has more or less the same features as Perl and Python. But AFAIK there are no generally available Javascript interpreters for the major machine arc

  • Advice on selecting programming languages to concentrate on? (2nd year IT security student) October 1

    I'm in the process of considering which programming languages I should devote the majority of my coding studies to. I'm a 2nd year CS student, majoring in IT security. What I want to do/work with: Intelligence gathering Relational databases Virus des

  • Simplify complex hash-table manipulations in Common LispJanuary 8

    I'm trying to write a simple triplestore in Common Lisp that will store triples in the form subject-predicate-object. The code is inspired by the book "Programming the Semantic Web". (defvar triplestore-graph (make-hash-table)) (defun triplestor

Copyright (C) 2018 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.626 s.