0

I have a list of list of float lists and I want to test if a value pair (e.g. [2.0, 1.1]) is already in this list. Therefor I wrote a simple code to check this. As far as I understand my code it should always write the result array. I think the if statement is not correct formulated or is at least not doing what I intended to do. The result array should be look like: [0, 1, 2]. It is like a 'self-check'.

import numpy as np
array_a = np.asarray([[2.0, 1.1], [3.3, 4.4], [2.5, 3.0]])
array_a_list = array_a.tolist()
result = np.zeros(np.size(array_a_list, axis=0))
for i in range(np.size(array_a_list, axis=0)):
    print(i)
    if array_a_list[i] in array_a_list: # Shouldn't this be always true? At least that's what I expect it to be.
        result[i] = array_a_list.index(i) # here I'm expecting to get the index back, where the entry is stored
test = array_a_list[i]

So the overall idea is to check if an entry is already in a list. If that's the case I want to get back the index at which the entry is stored. In my case an entry is an array of float which is looking like the following: [2.0, 1.1]. The idea came from this question.

riyansh.legend
  • 117
  • 1
  • 13
  • What's your question? Please [edit] to clarify. If you're looking for debugging help, you need to provide a [mre]. To start, `result` is not defined. See [ask] for more advice. – wjandrea Feb 07 '20 at 23:21
  • 2
    It's also not clear what you're trying to do since your code doesn't check any pairs, and `array_a_list[i] in array_a_list` is a tautology. But FWIW you don't need `== True` at all. – wjandrea Feb 07 '20 at 23:26
  • @wjandrea "don't need" makes it sound superfluous. Instead of harmful, which it is. – Kelly Bundy Feb 07 '20 at 23:27
  • @HeapOverflow Harmful in what way? – wjandrea Feb 07 '20 at 23:29
  • 1
    @wjandrea Well it makes the whole expression false. Try for example `3 in [3] == True` and you'll get the result `False`. – Kelly Bundy Feb 07 '20 at 23:30
  • @HeapOverflow Oh wow I didn't even notice that. I was going by the PEP 8 rule, *"Don't compare boolean values to True or False using `==`."* How does that work though? I thought it woud be parsed as `(3 in [3]) == True` since `in` and `==` have the same precedence. I also tried `3 in ([3] == True)` but that raised a `TypeError`. Should I ask a separate question? – wjandrea Feb 07 '20 at 23:39
  • 1
    @wjandrea It's just chained comparisons. It means `(3 in [3]) and ([3] == True)` (parentheses not necessary, I just added them for extra clarity). Just like `0 <= i < n` means `0 <= i and i < n`. See here: https://docs.python.org/3/reference/expressions.html#comparisons – Kelly Bundy Feb 07 '20 at 23:41
  • @HeapOverflow Wow that's super counter-intuitive. I've posted [a new question](https://stackoverflow.com/q/60122659/4518341) to share it with the community. If you post that as an answer, I'll accept it! :) – wjandrea Feb 08 '20 at 00:01

1 Answers1

0

Got it solved. I edited the result line now:

result[i] = array_a_list.index(array_a_list[i])
riyansh.legend
  • 117
  • 1
  • 13
  • 1
    Isn't that the same as `result[i] = i`? **Edit** I guess it will be different if the value of `array_a_list[i]` occurs in `array_a_list` before `i`, but I don't see how that's useful. – wjandrea Feb 08 '20 at 00:20
  • In this simplified example it is the same. And you're right, in my complete code I will modify the `array_a_list`, so it will not necessarily be the same there. – riyansh.legend Feb 08 '20 at 00:25