无法将元组项与列表项匹配

时间:2022-07-21 19:45:24

I have a list (tags) of integers. I want to map the list items to the value items of a dictionary (classes) and get the corresponding dictionary keys as output.

我有一个整数列表(标签)。我想将列表项映射到字典(类)的值项,并获取相应的字典键作为输出。

I am using:

我在用:

h = classes.items()
for x in tags:
    for e in h:
        # print x, e,  # uncomment this line to make a diagnosis
        if x == e[1]:
            print e[0]
        else:
            print "No Match"

Classes is the dictionary. Tags is the list with items that I want to map with the classes. When I run this code, I am getting 2616 time No Match at the output.

类是字典。标签是包含我想要与类一起映射的项目的列表。当我运行此代码时,我在输出中得到2616次No Match。

2616 = 8 (no. of tuples)*327 (no. of items of tags list)

2 个解决方案

#1


2  

If I understand what you are trying to do maybe this will help

如果我明白你想要做什么,也许这会有所帮助

>>> tags
['0', '2', '1', '3', '4', '7', '2', '0', '1', '6', '3', '2', '8', '4', '1', '2', '0', '7', '5', '4', '1']
>>> classes
{'Tesla': 7, 'Nissan': 0, 'Honda': 5, 'Toyota': 6, 'Ford': 1, 'Mazda': 4, 'Ferrari': 2, 'Suzuki': 3}

tags is a list of strings, not integers - so let's convert it to a list of ints.

tags是一个字符串列表,而不是整数 - 所以让我们将它转​​换为一个int列表。

>>> tags = map(int, tags)

classes is a dictionary mapping car makes to ints, but we want to use the value as the lookup. We can invert the dictionary (swap keys and values)

classes是一个将汽车制作为int的字典,但我们希望将该值用作查找。我们可以反转字典(交换键和值)

>>> classes_inverse = {v: k for k, v in classes.items()}

Now this is what tags and classes_inverse look like

现在这就是tags和classes_inverse的样子

>>> tags
[0, 2, 1, 3, 4, 7, 2, 0, 1, 6, 3, 2, 8, 4, 1, 2, 0, 7, 5, 4, 1]
>>> classes_inverse
{0: 'Nissan', 1: 'Ford', 2: 'Ferrari', 3: 'Suzuki', 4: 'Mazda', 5: 'Honda', 6: 'Toyota', 7: 'Tesla'}

Now we can collect the values of the inverse dictionary for each item in the list.

现在我们可以为列表中的每个项目收集逆字典的值。

>>> [classes_inverse.get(t, "No Match") for t in tags]
['Nissan', 'Ferrari', 'Ford', 'Suzuki', 'Mazda', 'Tesla', 'Ferrari', 'Nissan', 'Ford', 'Toyota', 'Suzuki', 'Ferrari', 'No Match', 'Mazda', 'Ford', 'Ferrari', 'Nissan', 'Tesla', 'Honda', 'Mazda', 'Ford']

#2


0  

For each tag, you iterate through all the keys, and print whether it was a match or not, when you except to have at most one hit. For example, if you have 10 items, for each tag, you'll print 1 hit and 9 misses.

对于每个标记,您遍历所有键,并打印是否匹配,除非您最多只有一个匹配。例如,如果您有10个项目,则对于每个标记,您将打印1个匹配和9个未命中。

Since you want to store this data, the easiest way is to invert the dictionary map, i.e. make key -> value to value -> key. However, this assumes that all values are unique, which your example implies so.

由于您要存储此数据,最简单的方法是反转字典映射,即make key - > value to value - > key。但是,这假设所有值都是唯一的,您的示例意味着这一点。

def map_tags(tags, classes):
    tag_map = {value: key for key, value in classes.items()}
    return [tag_map.get(t, 'No match') for t in tags]

However, be careful. In your classes examples the values are integers, while the tags are strings. You want the two to match when making a map out of them. If the tags are intended to be strings, then change

但是要小心。在您的类示例中,值是整数,而标记是字符串。你希望两者在制作地图时匹配。如果标签是字符串,则更改

tag_map.get(t, 'No match')

to

tag_map.get(int(t), 'No match')

#1


2  

If I understand what you are trying to do maybe this will help

如果我明白你想要做什么,也许这会有所帮助

>>> tags
['0', '2', '1', '3', '4', '7', '2', '0', '1', '6', '3', '2', '8', '4', '1', '2', '0', '7', '5', '4', '1']
>>> classes
{'Tesla': 7, 'Nissan': 0, 'Honda': 5, 'Toyota': 6, 'Ford': 1, 'Mazda': 4, 'Ferrari': 2, 'Suzuki': 3}

tags is a list of strings, not integers - so let's convert it to a list of ints.

tags是一个字符串列表,而不是整数 - 所以让我们将它转​​换为一个int列表。

>>> tags = map(int, tags)

classes is a dictionary mapping car makes to ints, but we want to use the value as the lookup. We can invert the dictionary (swap keys and values)

classes是一个将汽车制作为int的字典,但我们希望将该值用作查找。我们可以反转字典(交换键和值)

>>> classes_inverse = {v: k for k, v in classes.items()}

Now this is what tags and classes_inverse look like

现在这就是tags和classes_inverse的样子

>>> tags
[0, 2, 1, 3, 4, 7, 2, 0, 1, 6, 3, 2, 8, 4, 1, 2, 0, 7, 5, 4, 1]
>>> classes_inverse
{0: 'Nissan', 1: 'Ford', 2: 'Ferrari', 3: 'Suzuki', 4: 'Mazda', 5: 'Honda', 6: 'Toyota', 7: 'Tesla'}

Now we can collect the values of the inverse dictionary for each item in the list.

现在我们可以为列表中的每个项目收集逆字典的值。

>>> [classes_inverse.get(t, "No Match") for t in tags]
['Nissan', 'Ferrari', 'Ford', 'Suzuki', 'Mazda', 'Tesla', 'Ferrari', 'Nissan', 'Ford', 'Toyota', 'Suzuki', 'Ferrari', 'No Match', 'Mazda', 'Ford', 'Ferrari', 'Nissan', 'Tesla', 'Honda', 'Mazda', 'Ford']

#2


0  

For each tag, you iterate through all the keys, and print whether it was a match or not, when you except to have at most one hit. For example, if you have 10 items, for each tag, you'll print 1 hit and 9 misses.

对于每个标记,您遍历所有键,并打印是否匹配,除非您最多只有一个匹配。例如,如果您有10个项目,则对于每个标记,您将打印1个匹配和9个未命中。

Since you want to store this data, the easiest way is to invert the dictionary map, i.e. make key -> value to value -> key. However, this assumes that all values are unique, which your example implies so.

由于您要存储此数据,最简单的方法是反转字典映射,即make key - > value to value - > key。但是,这假设所有值都是唯一的,您的示例意味着这一点。

def map_tags(tags, classes):
    tag_map = {value: key for key, value in classes.items()}
    return [tag_map.get(t, 'No match') for t in tags]

However, be careful. In your classes examples the values are integers, while the tags are strings. You want the two to match when making a map out of them. If the tags are intended to be strings, then change

但是要小心。在您的类示例中,值是整数,而标记是字符串。你希望两者在制作地图时匹配。如果标签是字符串,则更改

tag_map.get(t, 'No match')

to

tag_map.get(int(t), 'No match')