【问题标题】:Getting a list printed 2 times in python, don't know why?在 python 中打印 2 次列表,不知道为什么?
【发布时间】:2022-06-12 21:10:10
【问题描述】:

我尝试了下面的代码,但它打印了 2 次列表,一次输出不正确,第二次输出正确。找不到代码有什么问题,感谢任何帮助。

代码:

nums = [2,7,15,11]
target = 18
# Output should be a list that shows indices of numbers that add up to target.

Output = []
for i in range(len(nums)):
    t = target - nums[i]
    if t in nums:
        u = nums.index(t)
        Output.append(u)
Output.sort()
print(Output)

【问题讨论】:

  • 这段代码中有一个print

标签: python python-3.x


猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-29
  • 1970-01-01
  • 2020-02-03
  • 2017-10-18
  • 2015-08-15
  • 2015-09-09
  • 1970-01-01
相关资源
最近更新 更多