簡體   English   中英

多輸入列表歸一化

[英]Multi-Input List Normalizing

我在學習 python 的過程中一直在學習“循環”部分。 我真的被困在最后一個 zybooks 實驗室中,老實說,我不知道從哪里開始,並且可以使用帶有解釋的解決方案,因為我真的很難理解這一部分。 下面的問題,在此先感謝。

在分析數據集(例如人體身高或體重數據)時,一個常見的步驟是調整數據。 這種調整可以通過標准化為 0 到 1 之間的值或丟棄異常值來完成。

對於此程序,通過將所有值除以最大值來調整值。 輸入以 integer 開頭,指示后面的浮點值數。

Output 每個小數點后兩位的浮點數,可以實現如下:

print(f'{your_value:.2f}')

例如:如果輸入是:

5
30.0
50.0
10.0
100.0
65.0

output 是:

0.30
0.50
0.10
1.00
0.65

5表示列表中有5個浮點數,分別為30.0、50.0、10.0、100.0、65.0。 100.0 是列表中的最大值,因此每個值都除以 100.0。

如說明中所述,您應該使用循環。

假設以下輸入:

text = '''5
30.0
50.0
10.0
100.0
65.0'''
# get the data as floats (skipping first number)
# you could also use a loop with `input`s if your coding server sends lines one by one
data = list(map(float, text.strip().split('\n')))[1:]

# calculate the maximum value
maximum = max(data)

# divide each element in list by maxium using a list comprehension (a compact kind of loop)
out = [e/maximum for e in data]

輸出: [0.3, 0.5, 0.1, 1.0, 0.65]

nums=[]
num_nums=int(input())

for i in range(num_nums):
    val=float(input())
    nums.append(val)

max=max(nums)

for i in range(num_nums):
    print(f'{(nums[i]/max):.2f}')

這是我想出的解決方案,它有效。 它使用我目前擁有的工具。

size = int(input())
inputList = []
for i in range(size):
    inputList.append(float(input()))
maxValue = None
for x in inputList:
    if maxValue==None or maxValue<x:
        maxValue = x
for x in inputList:
    print("%.2f"%(x/maxValue))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM