-1

I'm coming from C++.

Intuitively, in C++, here is what I'd do:

int someArray[n];
for (int i = 1 ; i < n ; i++){
   someArray[i] = !(i%2);
}

where i%2 == 0 when i is even and i%2 == 1 when i is odd.

The ! then makes that evens are evaluated as 1 and odds as 0, so someArray would end up with 0 in its odd indices and 1 in its even indices.

This is an example to illustrate what I'd like to do.

Is there a way to do this in Python without the usage of control flow, simply within the mathematical expression itself? not evaluates a boolean, and not a 1 or a 0.

I ask because I am setting the attribute of something +-1 based on whether a certain count is even or odd, and it is nested inside multiple other specifiers.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 2
    It would really help to show and example and desired output. You say you want to **add** 1, but your code looks like it assigns 1. Maybe helpful to consider that `abs(i%2 - 1)` is the opposite of `i%2`? You can also treat `(not (i%2))` as an integer in python. – Mark Jan 15 '23 at 03:28
  • Do you consider `1 if i % 2 == 0 else 0` to be usage of control flow? – Brian61354270 Jan 15 '23 at 03:33
  • > `not` evaluates a boolean. This is right. But you could cast this boolean into an integer using `ìnt()`. Or you go the way with `abs()` as Mark said. – TimbowSix Jan 15 '23 at 03:34
  • 1
    slightly OT, but if n is not a compile-time constant, then that is not valid c++ – Neil Butterworth Jan 15 '23 at 03:35
  • Your C++ code does not assign a value to `someArray[0]`, which leaves the value uninitialized. This is probably not what you want. – kotatsuyaki Jan 15 '23 at 03:40
  • @TimbowSix No need to cast, Python booleans are subtypes of integers and can be used like them. – Klaus D. Jan 15 '23 at 03:40
  • The underlying question here could be cast as: "what's the operator that turns an integer `1` into `0` and vice versa?" That operator is **bitwise** negation, spelled `~` in Python. Or it could be cast as: "how do I treat a boolean value as an integer?" in which case there is not really a question, because Python implements `bool` as a subtype of `int`. There isn't a **specific, clear** question here, but rather than try to extract one (I don't think it will make a lot of sense) I have provided some reference links to canonical questions on the topic that I think should solve the problem. – Karl Knechtel Jan 15 '23 at 03:51
  • The negation of `0` is `-1` , so that is not wholly accurate @KarlKnechtel . I believe I was looking for the `int` cast. – user20896951 Jan 15 '23 at 04:02
  • Sorry, need more caffeine. `x ^ 1` will do the trick. – Karl Knechtel Jan 15 '23 at 04:08

2 Answers2

1

You can achieve this using list comprehension in python.

n = 10
some_array = [1 if not i%2 else -1 for i in range(n)]

In python not operator is used to negate the truthiness of value, not to negate numeric value.

Kiran Kandel
  • 284
  • 1
  • 9
  • 2
    It's really not worth answering unclear answers here. It doesn't help future users when the question is unclear. For example, I (and presumably future readers) have no idea why you are using `-1` here given the OP's code that could never be `-1`. – Mark Jan 15 '23 at 03:37
1

You could explicitly cast the bool to int after adding not to the remainder calculation like so:

n = 10 #example length

#list initialization 
some_list = [0]*n

for i in range(n):
    some_list[i] = int(not i%2)

You can also make the list a boolean list and use map as well.

Here's a link to more details on this topic

Ping34
  • 206
  • 1
  • 10