0

Supposed that hi has hi[0]~hi[200] this range , and I want to proceed if statement only if hi[0~200] are include 1. If one of hi list has rather than 1 (ex) hi[5]==2), if statement should not proceed, but else statement runs. What should I do..? if hi[0]==1 and hi[1]==1 and ... etc is this the only way?

if hi[]==1:
    i =1
else:
    i=2
  • You're looking for the `any` or `all` keywords: `if all(h == 1 for h in hi):`... – Scott Mermelstein May 09 '17 at 15:23
  • http://stackoverflow.com/questions/19389490/how-do-pythons-any-and-all-functions-work <-- this is what I should have set as the dupe. – Scott Mermelstein May 09 '17 at 15:25
  • 2
    @ScottMermelstein But you don't need `all()` for this necessarily, like `set(hi) == {1}` – Chris_Rands May 09 '17 at 15:27
  • 1
    @Chris_Rands that works, but I think it creates a set (and hashes) where an iteration would be faster. You're not reusing the `set`. – Jean-François Fabre May 09 '17 at 15:30
  • 2
    @Chris_Rands From the Zen of Python [Pep 20](https://www.python.org/dev/peps/pep-0020/): `There should be one-- and preferably only one --obvious way to do it.` Which sounds more like "the one way": using sets in a valid but not intuitive way, or using a python keyword? Also, OP specifically requests short circuiting behavior, which isn't in the sets. – Scott Mermelstein May 09 '17 at 15:32

3 Answers3

2

You want to check if all the values in a given range are equal to 1, you need all:

hi = [1]*10

print(all(x == 1 for x in hi[0:7]))

returns True because all elements from 0 to 6 (inclusive) are 1. Now:

hi[5] = 5

print(all(x == 1 for x in hi[0:7]))

returns False because not all elements from 0 to 6 are 1

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
-1

Just loop through it like so:

for i, el in enumerate(hi):
    if el != 1:  # go to the next iteration without doing anything else
        continue
    else:
        # your code goes here

EDIT: based on OP's comment on @Andre's answer:

i = 1 if all(x == 1 for x in hi) else 0
Ma0
  • 15,057
  • 4
  • 35
  • 65
-1

If I understand your question correctly you want to check if all items within a range of the array have the value 1?

You can do this by checking if any item is equal to 1 using any and a generator expression:

if any(item != 1 for item in hi[start:end]):
    print("Error")
else:
    # Ok
André Laszlo
  • 15,169
  • 3
  • 63
  • 81
  • 1
    You could also use early stopping with a generator: `if any(hi[start:end], lambda item: item != 1)` – Arya McCarthy May 09 '17 at 15:23
  • what I want to do is, when the value of all items in hi is 1 , I want to put value 1 in i – Coding Machine May 09 '17 at 15:25
  • This doesn't work, because [`any()`](https://docs.python.org/3/library/functions.html#any) doesn't take a key/function argument. You "generally" use it with generator expressions: `any(x != 1 for x in hi[start:end])` – Ben Hoyt May 09 '17 at 15:48
  • @BenHoyt oops just copy/pasted the suggestion from arya, rolling back. – André Laszlo May 09 '17 at 15:49