0

I have a list of strings and I would like to verify some conditions on the strings. For example:

String_1: 'The price is 15 euros'. 
String_2: 'The price is 14 euros'. 
Condition: The price is > 14 --> OK 

How can I verify it? I'm actually doing like this:

if ('price is 13' in string): 
    print('ok')

and I'm writing all the valid cases. I would like to have just one condition.

rook
  • 5,880
  • 4
  • 39
  • 51
  • 5
    You need to clarify your question? The if statement conflicts with the Condition – Alan Kavanagh Mar 01 '19 at 13:48
  • What counts as a valid case? Do you expect to see input like "The price is 1.50 euros"? How about "The price is -99 euros"? How about "The price is 10000 euros"? How about "The price is 10,000 euros"? – Kevin Mar 01 '19 at 13:59

5 Answers5

1

You can list all of the integers in the string and use them in an if statement after.

str = "price is 16 euros"
for number in [int(s) for s in str.split() if s.isdigit()]:
    if (number > 14):
        print "ok"

If your string contains more than one number, you can select which one you want to use in the list.

Hoep it helps.

Maxouille
  • 2,729
  • 2
  • 19
  • 42
0

Is the string format always going to be the exact same? As in, it will always start with "The price is" and then have a positive integer, and then end with "euros'? If so, you can just split the string into words and index the integer, cast it into an int, and check if it's greater than 14.

if int(s.split()[3]) > 14:
    print('ok')

If the strings will not be consistent, you may want to consider a regex solution to get the numeral part of the sentence out.

Endyd
  • 1,249
  • 6
  • 12
0

You could use a regular expression to extract the number after "price is", and then convert the number in string format to int. And, finally to compare if it is greater than 14, for example:

import re
p = re.compile('price\sis\s\d\d*')
string1 = 'The price is 15 euros'
string2 = 'The price is 14 euros'

number = re.findall(p, string1)[0].split("price is ")

if int(number[1]) > 14:
    print('ok')

Output:

ok
Eduardo Soares
  • 992
  • 4
  • 14
0

You can just compare strings if they differ only by number and numbers have the same digits count. I.e.:

String_1 = 'The price is 15 euros'
String_2 = 'The price is 14 euros'
String_3 = 'The price is 37 EUR'

The will be naturally sorted as String_3 > String_1 > String_2

But will NOT work for:

String_4 = 'The price is 114 euros'

it has 3 digits instead of 2 and it will be String_4 < String_3 thus

So, the better, if you can extract number from the string, like following:

import re
def get_price(s):
    m = re.match("The price is ([0-9]*)", s)
    if m:
        return = int(m.group(1))
    return 0

Now you can compare prices as integer:

price = get_price(String_1)
if price > 14:
    print ("Okay!")

. . .

if get_price(String_1) > 14:
    print ("Okay!")

([0-9]*) - is the capturing group of the regular expression, all defined in the round parenthesis will be returned in group(1) method of the Python match object. You can extend this simple regular expression [0-9]* further for your needs.

If you have list of strings:

string_list = [String_1, String_2, String_3, String_4]

for s in string_list:
    if get_price(s) > 14:
        print ("'{}' is okay!".format(s))
rook
  • 5,880
  • 4
  • 39
  • 51
0

I suppose you have only ono value in your string. So we can do it with regex.

import re
String_1 = 'The price is 15 euros.'
if float(re.findall(r'\d+', String_1)[0]) > 14:
    print("OK")
Tao
  • 559
  • 4
  • 4