python中if in list_python-使用if语句检查条目是否已在列表中

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.

解决方案

Got it solved. I edited the result line now:

result[i] = array_a_list.index(array_a_list[i])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值