【问题标题】:Not able to understand Python3 enumerate()无法理解 Python3 enumerate()
【发布时间】:2020-08-24 07:50:34
【问题描述】:

问题:给定一个整数数组,返回两个数字的索引,使它们相加为特定目标。

您可以假设每个输入都只有一个解决方案,并且您不能两次使用相同的元素。

例子:

给定 nums = [2, 7, 11, 15],目标 = 9,

因为 nums[0] + nums[1] = 2 + 7 = 9, 返回 [0, 1]。

class Solution:
def twoSum(self, nums, target):

    lookup={}
    for cnt, num in enumerate (nums):
        if target-num in lookup:
            return lookup[target-num], cnt
        lookup[num]=cnt

我无法理解使用 for 循环后的步骤。我是 Python 新手,请帮助我。

【问题讨论】:

  • enumerate(items)items 中的每个项目返回(idx, item),并且idx0 开头,除非另有说明
  • if target-num in lookup: 这是什么意思?
  • if target-num in lookup 表示“如果字典 lookup 包含 target-num 作为键”

标签: python python-3.x


【解决方案1】:

让我通过解释代码的作用以及它如何解决问题来帮助您理解。

我们需要找到两个总和为 9 的数字,为了实现这一点,我们可以遍历数组中的每个数字,然后查看是否已经遇到一个等于目标数字减去当前数字的数字。如果我们还没有遇到这样的数字,我们存储当前数字及其对应的索引。

因为我们需要返回索引,所以我们希望能够查找数字-目标对并立即获取索引。该解决方案使用字典来存储数字(键)并将索引作为(值)返回。

我们遍历每个数字,如果我们之前已经遇到过目标数字,我们可以返回当前索引和目标数字的索引,如果我们没有遇到那个数字,我们只是存储当前数字及其索引。

枚举部分,简单地提供一个索引以及被迭代的数组的值,形式为(id, item)。

class Solution:
    def twoSum(self, nums, target):
        # Here a dictionary is created, which will store value, index as key, value pairs.
        lookup={}
        # For every number in the array, get the index (cnt) and number (num)
        for cnt, num in enumerate (nums):
            # If we find target-num, we know that num + target-num = target
            if target-num in lookup:
                # Hence we return the index of the target-num we stored in the dict, and the index of the current value (cnt)
                return lookup[target-num], cnt
            # Otherwise we store the current number as key with its index as value
            lookup[num]=cnt

【讨论】:

    【解决方案2】:

    enumerate() 方法向一个可迭代对象添加一个计数器,并以枚举对象的形式返回它。这个枚举对象可以直接在 for 循环中使用,也可以使用 list() 方法转换为元组列表。

    例如
    >>>list(enumerate("abc"))

    给予

    [(0, 'a'), (1, 'b'), (2, 'c')]
    

    为了便于理解,我正在评论您的程序。去看看,你一定会明白的。
    class Solution:
    def twoSum(self, nums, target):
        
        # lookup is a dictionary that stores the number and its index 
        # e.g. '{7:1}'
        #     number 7 at index 1 
        lookup={}
    
        # As explained above cnt and num will receive values one by one along with index.
        for cnt, num in enumerate (nums):
            
            # We look if the number required to be added into the 'num' is present in dictionary
            if target-num in lookup:
                # if value found in lookup then we return the current index along with the index of number found in lookup.
                return lookup[target-num], cnt
    
            # After every loop insert the current value and its index into the lookup dictionary.
            lookup[num]=cnt
    

    希望,我以您想要的方式回答了您的问题。请在下方评论,如果有什么未解决的问题,我一定会尝试回答。

    【讨论】:

      猜你喜欢
      • 2015-07-31
      • 2017-12-27
      • 1970-01-01
      • 2020-07-17
      • 1970-01-01
      • 2016-10-04
      • 2019-04-13
      • 1970-01-01
      • 2015-08-24
      相关资源
      最近更新 更多