python-使用if语句检查条目是否已在列表中 [英] python - use if statement to check if entry is already in a list

查看:218
本文介绍了python-使用if语句检查条目是否已在列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个浮动列表的列表,我想测试该列表中是否已经有一个值对(例如[2.0,1.1]).因此,我编写了一个简单的代码来检查这一点.据我了解我的代码,它应该总是写结果数组.我认为if陈述的表达方式不正确,或者至少没有按照我的意图去做.结果数组应类似于:[0,1,2].就像是自我检查".

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]

因此,总体思路是检查列表中是否已存在条目.如果是这种情况,我想找回存储该条目的索引.在我的情况下,一个条目是一个如下所示的float数组:[2.0,1.1].这个想法来自 this 问题.

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])

这篇关于python-使用if语句检查条目是否已在列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆