检查列表中是否存在值

时间:2017-12-29 12:42:22

标签: python python-2.7

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

我想将它添加到数组中,如果它尚未添加:

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

没有x和y坐标,这非常好。

我想要做的是现在比较没有x和y的列表。只是按类型和名称。

喜欢的东西 if not object[2:] in world[room][2:]

1 个答案:

答案 0 :(得分:2)

generator列表中的每个项目创建lists 2 world[int(room)]来自索引if not object[2:] in (i[2:] for i in world[int(room)]): world[int(room)].append(object) 的元素:

object

N.b。你也不应该使用Season: Winter 2017-18 Activity: Basketball - Boys JV *DATE: 02/13/2018 * - ( previously 02/06/2018 ) Event type: Game Home/Host: Clear Lake Opponent: Webster City *START TIME: 6:15PM CST* - ( previously 4:30PM CST ) Location: Clear Lake High School, 125 N. 20th Street, Clear Lake, IA ,因为它是一个引用内置类型的标识符,所以通过使用它作为变量名,你可以覆盖它。