user2423678
user2423678

Reputation: 43

Searching a list for a value in string python

trying to search this list

a = ['1 is the population', '1 isnt the population', '2 is the population']

what i want to do if this is achievable is search the list for the value 1. and if the value exists print the string.

What i want to get for the output is the whole string if the number exists. The output i want to get if the value 1 exists print the string. I.e

1 is the population 
2 isnt the population 

The above is what i want from the output but I dont know how to get it. Is it possible to search the list and its string for the value 1 and if the value of 1 appears get the string output

Upvotes: 0

Views: 1040

Answers (6)

Preet Kukreti
Preet Kukreti

Reputation: 8617

def f(x):
    for i in a:
        if i.strip().startswith(str(x)):
            print i
        else:
            print '%s isnt the population' % (x)

f(1) # or f("1")

This is more accurate/restrictive than doing a "1" in x style check, esp if your sentence has a non-semantic '1' char anywhere else in the string. For example, what if you have a string "2 is the 1st in the population"

You have two semantically contradictory values in the input array:

a = ['1 is the population', '1 isnt the population', ... ]

is this intentional?

Upvotes: 0

Sylvain Leroux
Sylvain Leroux

Reputation: 52040

If I understand it well, you wish to see all the entry starting with a given number ... but renumbered?

# The original list
>>> a = ['1 is the population', '1 isnt the population', '2 is the population']

# split each string at the first space in anew list
>>> s = [s.split(' ',1) for s in a]
>>> s
[['1', 'is the population'], ['1', 'isnt the population'], ['2', 'is the population']]

# keep only whose num items == '1'
>>> r = [tail for num, tail in s if num == '1']
>>> r
['is the population', 'isnt the population']

# display with renumbering starting at 1
>>> for n,s in enumerate(r,1):
...   print(n,s)
... 
1 is the population
2 isnt the population

If you (or your teacher?) like one liners here is a shortcut:

>>> lst = enumerate((tail for num, tail in (s.split(' ',1) for s in a) if num == '1'),1)
>>> for n,s in lst:
...   print(n,s)
... 
1 is the population
2 isnt the population

Upvotes: 0

gertvdijk
gertvdijk

Reputation: 24884

Use list comprehension and in to check if the string contains the "1" character, e.g.:

print [i for i in a if "1" in i]

In case you don't like the way Python prints lists and you like each match on a separate line, you can wrap it like "\n".join(list):

print "\n".join([i for i in a if "1" in i])

Upvotes: 0

luke14free
luke14free

Reputation: 2539

Python has a find method which is really handy. Outputs -1 if not found or an int with the position of the first occurrency. This way you can search for strings longer than 1 char.

print [i for i in a if i.find("1") != -1]

Upvotes: 0

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251126

You should use regex here:

in will return True for such strings as well.

>>> '1' in '21 is the population'
True

Code:

>>> a = ['1 is the population', '1 isnt the population', '2 is the population']
>>> import re
>>> for item in a:
...     if re.search(r'\b1\b',item):
...         print item
...         
1 is the population
1 isnt the population

Upvotes: 1

user1907906
user1907906

Reputation:

for i in a:
    if "1" in i:
        print(i)

Upvotes: 3

Related Questions