简体   繁体   English

如何使用while循环从列表中的每个整数中减去最小的数字?

[英]How do I subtract the smallest number from each integer in a list using a while loop?

input: 输入:

5
30
50
10
70
65

5 is how many numbers follow after. 5是紧随其后的数字。

My code: 我的代码:

n = int(input())
list = []
i = 0
while len(list) < n:
    integer = int(input())
    list.append(integer)
    i = i + 1
    minList = min(list)
    integers = list[i - 1] - minList
    print(integers)

I'm suppose to subtract the smallest number from the 5 integers. 我想从5个整数中减去最小的数字。

The correct output: 20 40 0 60 55 正确的输出:20 40 0 60 55

My output: 0 20 0 60 55 我的输出:0 20 0 60 55

I understand why my output is wrong since the smallest number is 20 until 10 is inputed, but I don't know how to correct it. 我知道为什么输出错误是因为最小的数字是20直到输入10,但是我不知道如何纠正它。 I tried different ways, but none of them work. 我尝试了不同的方法,但是没有一个起作用。 How do I subtract the smallest number from each integer? 如何从每个整数中减去最小的数字?

Get the inputs to a list. 将输入获取到列表。 Take minimum of this list using min() and then subtract minimum value from each of the list elements: 使用min()获取此列表的min() ,然后从每个列表元素中减去最小值:

n = int(input())                  # Read number of numbers
lst = []

for _ in range(n):
    lst.append(int(input()))      # Append to list

min_value = min(lst)              # Take the minimum number
final_lst = [abs(x-min_value) for x in lst]  # Subtract minimum from each number

Collect all the input first, then find the minimum value and print the numbers. 首先收集所有输入,然后找到最小值并打印数字。

n = int(input())
numbers = []
while len(numbers) < n:
    integer = int(input())
    numbers.append(integer)
smallest = min(numbers)
for number in numbers:
    print number - smallest
size = int(input())
lst = [int(input()) for _ in range(size)]
m = min(lst)
res = [abs(n - m) for n in lst]

This looks like a default puzzle for sites like Hackerrank ... take all the inputs , then perform your operations on all inputs. 对于像Hackerrank这样的网站来说,这似乎是一个默认难题……接受所有输入,然后对所有输入执行操作。 Do not meddle with data while still gathering inputs (unless it makes sense to do so). 仍在收集输入的同时不要混入数据(除非这样做是有道理的)。

A nice way of getting all the data is: 获取所有数据的一种好方法是:

n = int(input())    # get how many inputs follow

# get n inputs, perform the int() conversion, store as list.
data = list(map(int, (input().strip() for _ in range(n))))

# calculate the min value
min_value = min(data)

# print all reduced values in order each on one line
print( *[x-min_value for x in data], sep = "\n")
# or comma seperated:
print( *[x-min_value for x in data], sep = ",")

Output: 输出:

# print with sep="\n"
20
40
0
60
55

# print with sep=","
20,40,0,60,55

Read the doku for map() , int() , min() and look at Built in functions : do not use those as names for your variables, neither use list , dict , set , tuple . 阅读map()int()min()的doku并查看内置函数 :不要将那些用作变量的名称,也不要使用listdictsettuple

Try this: 尝试这个:

l = list()
for _ in range(int(input())):
    l.append(int(input()))
xmin = min(l)
print(*[x - xmin for x in l])

Output: 输出:

C:\Users\Documents>py test.py
5
30
50
10
70
65
20 40 0 60 55

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM