【问题标题】:How can I print the return value? [duplicate]如何打印返回值? [复制]
【发布时间】:2017-09-14 12:47:48
【问题描述】:

** 如何打印返回 index1,index2?我尝试了不同的方法,但没有打印出来。 **

class Solution:
    def twoSum(self, nums, target):
        nums = [2,7,11,15]
        target = 9
        hash_map = {}
        for index, value in enumerate(nums):
            hash_map[value] = index
        for index1, value in enumerate(nums):
            if target - value in hash_map:
                index2 = hash_map[target - value]
                if index1 != index2:
                    return [index1,index2]

【问题讨论】:

  • 您尝试了哪些不同的方法?编辑您的问题并包括这些尝试。
  • 请编辑您的问题以包含您尝试过的Minimal, Complete, and Verifiable Example
  • 如果您有问题,最好现在添加或保持沉默。

标签: python python-3.x


【解决方案1】:
class Solution:
    def twoSum(self, nums, target):
        nums = [2,7,11,15]
        target = 9
        hash_map = {}
        for index, value in enumerate(nums):
            hash_map[value] = index
        for index1, value in enumerate(nums):
            if target - value in hash_map:
                index2 = hash_map[target - value]
                if index1 != index2:
                    return [index1,index2]

if __name__ == '__main__':
    print(Solution().twoSum(9, [2,7,11,15]))

我认为您正在寻找的是一个 main 函数,您可以在其中使用您的函数

【讨论】:

    猜你喜欢
    • 2018-08-28
    • 2019-03-28
    • 1970-01-01
    • 2012-09-29
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多