0

I have a list which contains string values taht represents calculations. From them I want to get a specific character, let's say plus and minus signs.

I tried using var1.find('a') != -1 or var.find('z') != -1 as suggested here Python: Using AND and OR with .FIND() method, but I'm still getting -1 when I try '-'.

Example:

lists = ["22 + 13", "4500 - 2"]
for list in lists :
  if (list.find('+') or (list.find('-')) != -1) :
    signpos =  list.find("+") or list.find("-")
    print(signpos)
  else:
    print('Error')

I get 3 aand -1 as results, where the desire output should be 3 and 5.

What I'm not seeing?

Le Paul
  • 67
  • 8
  • 1
    Don't use `list` as a variable name, you're replacing the built-in function with the same name. – Barmar Apr 07 '22 at 23:31
  • It's an example the original it's not named list – Le Paul Apr 07 '22 at 23:33
  • I dont think this line is doing what you think it does: `if (list.find('+') or (list.find('-')) != -1) :`. Seems to me, it checks if True or False is not equal to -1, which will always be true. It’s equivalent to `if True` from what im seeing – Anonymous Apr 07 '22 at 23:33
  • @Barmar there is no function named list. `list` is a class, not a function. – Anonymous Apr 07 '22 at 23:34
  • So... how do I use the find() with a logical properly? @Anonymous4045 – Le Paul Apr 07 '22 at 23:36
  • @Anonymous4045 Classes are used as functions, e.g. `my_list = list(something)` – Barmar Apr 07 '22 at 23:37
  • The line `list.find('+') or (list.find('-')) != -1` is **not** the same as `list.find('a') != -1 or list.find('z') != -1` – kenntnisse Apr 07 '22 at 23:39
  • @Barmar no lol, that’s called a cunstructer. It creates an instance of the list class. – Anonymous Apr 07 '22 at 23:39
  • @Anonymous4045 For this purpose, it doesn't really matter. It's a built-in that you shouldn't redefine. – Barmar Apr 07 '22 at 23:42
  • Also, why are you naming it `list` in `for list in lists:`? Kind of gets confusing when it's not actually a list, but a string. The `list.find(val)` returns an `int`, the position of the value. See [here](https://stackoverflow.com/questions/18195322/pythons-logical-operator-and) for what happens if you use logical operators with `int`s. – kenntnisse Apr 07 '22 at 23:44

2 Answers2

1

As others suggested, don't use list as a variable name. It is a type.

lists = ["22 + 13", "4500 - 2"]
for string in lists:
  signPos = None

  if "+" in string:
    signPos = string.find("+")
  elif "-" in string:
    signPos = string.find("-")
  else:
    print('Error')
    
  print(signPos)

# Output
# 3
# 5
Ajility
  • 526
  • 3
  • 19
0
signpos =  list.find("+") or list.find("-")

If the string does not have a plus sign, the first find() call will return -1 which is a non-false value, so the second find() will not be executed, and signpos will be assigned -1.

This probably isn't what you wanted.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • I’m curious, did you run that and it worked? Looks like it will just assign either True or False – Anonymous Apr 08 '22 at 01:29
  • @Anonymous4045 The expression `A or B` returns the value of A if it is truthy, otherwise it returns the value of B. It does not necessarily return true/false. – John Gordon Apr 08 '22 at 02:06