简体   繁体   English

检查列表中是否存在值

[英]Check if a value exists on a list

I have a list: object = [x, y, type, name] 我有一个列表: object = [x, y, type, name]

I want to add it to an array, if it's not already added: 我想将其添加到数组中(如果尚未添加):

if not object in world[int(room)]:
   world[int(room)].append(object)

This worked very nice without the x and y coordinates. 如果没有x和y坐标,则效果很好。

What I want to do is to now compare the lists without x and y. 我想做的是现在比较没有x和y的列表。 Just by type and name. 仅按类型和名称即可。

Something like if not object[2:] in world[room][2:] if not object[2:] in world[room][2:]类的东西

Create a generator of lists made from the elements from index 2 onwards for each item in the world[int(room)] list: world[int(room)]列表中的每个项目创建一个从索引2开始的元素组成的lists generator

if not object[2:] in (i[2:] for i in world[int(room)]):
   world[int(room)].append(object)

Nb you should also not use object as it is an identifier that refers to a built-in type, so by using it as a variable name, you are overriding it. 注意,您也不应该使用object因为它是一个指向内置类型的标识符,因此通过将其用作变量名可以覆盖它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM