dyao
dyao

Reputation: 1011

String contain specific characters

If I have the following:

name="paula"

list=["l", "p"]

for x in list:
    print name.count(x)

>>>
1
1

Which If I understand correctly, is taking the string: "paula" and confirming that it has one "l" and one "p" character.

But what if I wanted to do this:

#**if name has both Letters, l and p, then it is a mouse**

name="paula"

list_1=["l"]
list_2=["p"]
list_3=["l", "p"]

for y in list_3:
    if name.count(y):

   print "%s contains the letters %s and %s. %s is a mouse." % (name,w,x,name)  

#assume w and x are already defined


>>>

paula contains the letters l and p. paula is a mouse.
paula contains the letters l and p. paula is a mouse.

Obviously, this doesn't work. I realized that in this code, it would check the string "paula" for the letter "l". And then it runs again to see if it contains "p". Thus two outputs instead of one.

I believe the for loop probably needs to be discarded.

Any help is appreciated guys!

@ Two-Bit Alchemist- Reason for 3 lists:

#cat, dog, mouse
#if name contains the letter "L", then it is a cat
#if name contains the Letter "P", then it is a dog
#if Name has both Letters, L and P, then it is a mouse
#if Name has no L or P, then loop the question


name="lily"
name_2="patrick"
name_3="paula"

list_1=["l"]
list_2=["p"]
list_3=["l", "p"]


for w in list_1:
    if name.count(w):
        print "%s contains the letter %s. %s is a cat." %(name,w,name)

for x in list_2:
    if name_2.count(x):
        print "%s contains the letter %s. %s is a dog." %(name_2,x,name_2)

for y in list_3:
    if name_3.count(y):
        print "%s contains the letters %s and %s. %s is a mouse." %(name_3,w,x,name_3)

Upvotes: 0

Views: 70

Answers (3)

A.J. Uppal
A.J. Uppal

Reputation: 19264

Use any() to check if the contents of the list exist in the string:

name = "paula"
list_1 = ["l", "p"]
if any(word in name for word in list_1):
    print 'paula is a mouse'

Upvotes: 1

Michael0x2a
Michael0x2a

Reputation: 64068

If I'm understanding correctly, you want to check whether a list of strings can all be found inside another string?

If so, you could do something like this:

list3 = ["l", "p"]
name = "paula"
if all(char in name for char in list3):
    print "{0} is a mouse.".format(name)

Essentially, how the code works is that it uses a generator combined with the 'all' operator.

The segment of code char in name for char in list3 will iterate through each item in list3 and will report a true or false value whether or not the character exists inside the name. This syntax is called a generator comprehension, which is pretty similar to list comprehensions, which you can learn more about here.

In a nutshell, char in name for char in list3 is doing something similar to the following:

temp = []
for char in list3:
    temp.append(char in name)
print temp

>>> [True, True]

Next, the all function is a builtin that takes a list of bools, and returns True only when every element in the list is also True.

Taken all together, the expression all(char in name for char in list3) will evaluate to True only when every character in the list can be found inside name.

Upvotes: 3

dmcauslan
dmcauslan

Reputation: 398

I think your best bet is to use sets.

name = 'paula'
seek = 'pl'

if set(seek).issubset(set(name)):
    print "{0} contains the letters '{1}'. {0} is a mouse.".format(name, seek)

>>> paula contains the letters 'pl'. paula is a mouse.

Upvotes: 3

Related Questions