0

I have something similar to the following code:

list1 = [(13,120), (25, 254), (25, 125), (189, 11), ....] 
list2 = [ list(el) for el in list1]
arr = np.array(list2)

Let's say list1 has a length of 100 and hence, arr has a shape (100, 2)

tuple_list = some tuple list where tuple size is 2 i.e [(5, 56), (254, 23), (22, 22), ...]
count_list = 0
count_arr = 0
for tup in tuple_list:
    if tup in list1:
        count_list += 1
    if list(tup) in arr:
        count_arr += 1

I expected count_list and count_arr to be equal but they are showing different values. So my question is how does in work for list and ndarray?

paul-shuvo
  • 1,874
  • 4
  • 33
  • 37
  • https://stackoverflow.com/questions/18320624/how-does-contains-work-for-ndarrays – user2357112 Jul 28 '20 at 22:17
  • 1
    The array does not contain tuples; the tuples were used to initialize *rows*, not *elements*, of the array. – chepner Jul 28 '20 at 22:27
  • Show the different results; don't just say they are different! – hpaulj Jul 28 '20 at 22:40
  • Yes, `in` can be quite different when applied an `ndarray`; as a general idea it isn't a good idea to try it. `arr` does not contain lists or tuples, even when its shape is (100,2). `in`, depending on the objects, may use equality. Look at `[5,56]==arr`. The result is (100,2), not (100,) – hpaulj Jul 28 '20 at 23:07

0 Answers0