1

Suppose i have a list in python which contains many words. Now I want to print elements in list on basis of a condition, which I have to take from input. For example, I want all elements which start with 'a' sometimes, or elements which end with 'l'.

So, I want an applicable method to execute this programme :

a=['','apple','ball','cat']
condition = input()
for i in a :
     if condition:
          print i

where condition is an expression and I need programme to parse it as a expression instead of string.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

4 Answers4

1

Because you handle strings, you can pass a regex as an input.

It gives you:

  1. One generic code - Don't handle ifs and specific conditions for each case
  2. Powerful solution for user - Provides many options to be used, almost no limitations for the user to query.

Do something like:

import re

a=['','apple','ball','cat']
regex = re.compile(input())

# filter only the strings in 'a' which match the given pattern
matches = filter(lambda x: regex.match(x), a)
for i in matches:
    print(i)

Some examples:

input: '^a[a-z]*$' will match only 'apple'

input: '^[a-z]*ll' will only match 'ball'

Aaron_ab
  • 3,450
  • 3
  • 28
  • 42
  • It is legit but I the list elements need not be strings. I may have integers and I might want all integers > 10 or less than 20. I just want a way to replace the variable 'condition' with whatever expression I need – Sriraj Vysyaraju Sep 30 '19 at 06:30
  • @SrirajVysyaraju - Check: https://stackoverflow.com/questions/31999444/interpreting-a-python-string-as-a-conditional-statement – Aaron_ab Oct 01 '19 at 05:31
1

Yes, you can! Use RegEx python library to compile the string and you are good to go.

import re
a = ['', 'apple','nike', 'cat']
expression = input()
r = re.compile(expression)
print(expression)
for ain in a:
    if(re.findall(r,ain)):
        print(ain)
1

It can be done in this way :

  1. Create a function which evaluate the condition, and return True or False if it pass or fail.

  2. In for loop use call this function with given input.

Code

def condition_function(argument, condition):
      # check condition for argument here

      # put code to check the condition true ore not
      # result is true or false indication argument satisfy the condition

      if result is True:
               return True
      else:
           return False


a=['','apple','ball','cat']
condition = input()
for i in a :
     if conditions_function(i, condition):
           print i
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
  • The `input()` method will return a string. You run `if condition`, without addressing the `argument`. If given input is not an empty string, it will always be `True` – Aaron_ab Sep 30 '19 at 11:17
0

You can use the str.startswith(letter) method :

a=['','apple','ball','cat']
condition = input()
for i in a :
     if i.startswith("a") or i.endswith("i"):
          print i

Also, what is "condition" input for?

Gugu72
  • 2,052
  • 13
  • 35