0

我有一个格式为 的元组列表((x,y),type),我想看看该列表是否包含我想要的“类型”,无论(x,y)位置如何。如果是这样,我想在包含所选“类型”的元组的索引之后插入一些东西。

我设置它的方式,列表将只包含每种类型中的一种,如果这有所不同的话。

编辑:

哇,对不起,我刚刚意识到使用字典会使这变得容易得多。我保证我在发帖之前确实考虑过这个问题。

4

3 回答 3

0

Something like this should work -

for t in l:
    if isinstance(t[1], type):
        l.insert(value, l.index(t))
于 2013-01-17T01:34:41.007 回答
0

Basically, to do this, I just set up each type as its own entry in a dictionary, with the position that it is at as the corresponding entry.

于 2013-01-17T01:34:56.077 回答
0

I would make a dictionary out of your list:

table = {type:(x, y) for x, y, type in tuples}

Now, you can look up a tuple just by doing table[your_type].

于 2013-01-17T01:38:10.097 回答