Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何打印返回值?_Python_Python 3.x - Fatal编程技术网

Python 如何打印返回值?

Python 如何打印返回值?,python,python-3.x,Python,Python 3.x,** 如何打印返回索引1,索引2?我尝试了不同的方法,但没有打印出来。 ** 我想你要找的是一个main函数,你可以在其中使用你的函数你尝试了哪些不同的方法?编辑您的问题并包括这些尝试。请编辑您的问题以包括您尝试过的内容。如果您有问题,最好现在添加,或者保持沉默。 class Solution: def twoSum(self, nums, target): nums = [2,7,11,15] target = 9 hash_map =

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


我想你要找的是一个
main
函数,你可以在其中使用你的函数

你尝试了哪些不同的方法?编辑您的问题并包括这些尝试。请编辑您的问题以包括您尝试过的内容。如果您有问题,最好现在添加,或者保持沉默。
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]
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]))