Python 中的哈希映射/字典

发布于 2025-01-17 19:20:39 字数 744 浏览 0 评论 0原文

遇到了此leet代码。

prevMap = {} #Value:Index
    for i,n in enmuerate(nums):
        diff = target - n
        if diff in prevMap:
            return (prevMap[diff],i)
        prevMap[n] = i                  

这是使用hashaps的著名两个总问题的摘录。

我是Python的hashmaps的新手,因此没有意识到您可以使用“ In”函数直接从字典/哈希图中查找值。但是,我不明白的是最后两行。 第二行试图返回hashmap中值的索引。从我过去所知道的,这不是应该与hashmap [index] = value相反吗?相反的工作也可以在hashmap [value] = index上?

至于最后一行,我们正在尝试将list nums放入哈希图。在这种情况下,由于我们试图将值n映射到hashmap的索引i中,因此是否应该是prevMap [i] = n

谢谢。

hashmap [index] =值?相反的工作是否也可以使用hashmap [value] =索引?

至于最后一行,我们正在尝试将NUM列表放入哈希图。在这种情况下,由于我们试图将值n映射到hashmap的索引i中,因此是否应该被prevmap [i] = n。

谢谢。

Came across this leet code.

prevMap = {} #Value:Index
    for i,n in enmuerate(nums):
        diff = target - n
        if diff in prevMap:
            return (prevMap[diff],i)
        prevMap[n] = i                  

This is an extract of the famous Two Sum problem using hashmaps.

I'm new to hashmaps for Python, and hence did not realize that you could look up values directly from the dictionary/hashmap using the 'in' function. However, what I do not understand is the last 2 lines.
The second last line is trying to return the index of the value in the hashmap. From what I know in the past, isn't it supposed to be the opposite whereby hashMap[index] = value? Does the opposite work too whereby hashMap[value] = index?

As for the last line, we are trying to put the list nums into the hashmap. In this case, shouldn't it be prevMap[i] = n, since we are trying to map the value n into index i of the hashmap?

Thank you.

hashMap[index] = value? Does the opposite work too whereby hashMap[value] = index?

As for the last line, we are trying to put the list nums into the hashmap. In this case, shouldn't it be prevMap[i] = n, since we are trying to map the value n into index i of the hashmap?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆凄美了谁 2025-01-24 19:20:39

你把事情搞混了。在倒数第二行中,您将返回一个 元组列表中索引的 > 存储在 字典 中作为两个和和i,它又是列表中的索引 - 而不是两个和的第二个被加数的字典
在最后一行中,您将索引存储在列表中的特定值。

我相信您混淆了列表/字典的工作方式。您无法通过索引访问字典。仅仅因为语法与 [] 相同并不意味着访问值的方式相同。

l = ["val1", "val2", "val3", "val4"]
d = {
    "key1": "val1",
    "key2": "val2"
}

d_with_keys_defined_as_index = {
    0: "val1",
    1: "val2"
}

# Access first element of list
print(l[0])
# KeyError, a dictionary cannot be accessed by index (except index is explicitly defined as a key)
print(d[0])
# access value by key
print(d["key1"])
# now 0 is explicitly defined as a key, so we can access it using that key. This is still not an access by index though
print(d_with_keys_defined_as_index[0])

You are mixing things up. In the second to last line you are returning a tuple of the index within the list stored in the dictionary for one of the values that is part of the two sum and i which is again the index within the list - not the dictionary for the second summand of the two sum.
In the last line you store the index within the list for a specific value.

I believe you are mixing up how lists/ dictionaries work. You cannot access dictionaries by index. Just because the syntax is the same with [] does not mean the the way to access values is the same.

l = ["val1", "val2", "val3", "val4"]
d = {
    "key1": "val1",
    "key2": "val2"
}

d_with_keys_defined_as_index = {
    0: "val1",
    1: "val2"
}

# Access first element of list
print(l[0])
# KeyError, a dictionary cannot be accessed by index (except index is explicitly defined as a key)
print(d[0])
# access value by key
print(d["key1"])
# now 0 is explicitly defined as a key, so we can access it using that key. This is still not an access by index though
print(d_with_keys_defined_as_index[0])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文