我花了 45 分钟来回答这个 python 列表问题。什么是更简单的方法?

It took me 45 minutes to answer this python list question. What is a simpler method?

所以我一直在努力学习 python。它一直在杀我。现在已经快2个月了。我有两个问题。 1. 有什么更简单的方法可以解决这道不用 45 分钟的问题?以及 2 - 在你变得有能力之前,你需要练习多长时间 Python?

When analyzing data sets, such as data for human heights or for human weights, a common step is to adjust the data. This adjustment can be done by normalizing to values between 0 and 1, or throwing away outliers.

For this program, read in a list of floats and adjust their values by dividing all values by the largest value. Output each value with two digits after the decimal point. Follow each number output by a space.

Ex: If the input is:

30.0 50.0 10.0 100.0 65.0

the output is:

0.30 0.50 0.10 1.00 0.65

''' Type your code here. '''
num = input()
makeList = num.split()
biggest = float(makeList[0])
for index, value in enumerate(makeList):
    if float(value) > float(biggest):
        biggest = value
lastString = ""
for i in makeList:
    answer = ("{:.2f}".format(float(i)/float(biggest)))
    lastString += answer + " "
print(lastString)

1:比较输出 3 / 3

Input
30.0 50.0 10.0 100.0 65.0
Your output

2:比较输出 4 / 4

Input
5.0 10.0 15.0 20.0 25.0 30.0 35.0
Your output
0.14 0.29 0.43 0.57 0.71 0.86 1.00 

3:比较输出 3 / 3

Input
99.9 52.5 12.6 200.0
Your output
0.50 0.26 0.06 1.00 
0.30 0.50 0.10 1.00 0.65 

你只需要一步一步来。抓取输入:

num = input()

将其拆分为单词并将它们全部转换为浮点数:

vals = [float(k) for k in num.split()]

现在我们有了一个浮动列表。找出最大的:

maxval = max(vals)

将它们全部除以该值:

for i in range(len(vals)):
    vals[i] /= maxval

打印它们:

for i in vals:
    print( "{:.2f}".format(i), end=' ')
print()

一个更简单的方法是这样的:

inpt = "30.0 50.0 10.0 100.0 65.0" # take the input together
inpt = [float(val) for val in inpt.split()] # turn the input into a list of floats
large = max(inpt) # find the largest element

# print each element divided by large and specify 2 decimal places as the format
print(*(f"{(val/large):.2f}" for val in inpt))

代码精确打印:0.30 0.50 0.10 1.00 0.65

回答您的问题:

  1. 解决这个问题更简单的方法是什么?

第 1 步:您可以将数据设为“浮动”并跳过第一个数字

或者,如果要求逐行发送,您使用“输入”循环的方式也有效

data = list(map(float, text.strip().split('\n')))[1:]

第 2 步:现在计算最大值

maximum = max(data)

第 3 步:最后,使用列表理解将列表中的每个元素除以您在第 2 步中找到的最大值

您可以在此处了解列表理解:https://www.w3schools.com/python/python_lists_comprehension.asp

out = [e/maximum for e in data]
  1. 你练习了多长时间Python才变得能干?

好吧,没有固定的答案,专注于解决问题,你就可以开始了。此外,没有明确的时间可以量化您需要练习多长时间。据我所知,只要你想在这个领域,你就练习!

做这道题最简单的方法是先把它转换成一个numpy数组,然后把整个数组除以最大的数,

import numpy

test = np.array([30.0, 50.0, 10.0, 100.0, 65.0])
print(test/test.max())

关于第二个问题……我只能说不用太担心。学任何东西一开始都很难,每个人都曾经和你现在处于同一个阶段。有一个你每天遵循的系统计划,知道你现在应该学习什么,以及在完成当前主题后你应该学习什么。如果您愿意,请寻求他人的指导。不要承受太大的压力,因为并非每个人都以相同的速度学习。多花点时间没关系...重要的是你学到了一些东西。

根据我的经验,我花了大约 2-3 个月的时间才达到 Python 的中级水平,但那是因为我支付了课程费用,该课程为我提供了系统的计划和良好的指导。但是我敢肯定,即使不花一分钱,您也可以升级。加油!

一种更简单的方法是使用伪代码。如果您要解决一个大问题,伪代码和编码之前的计划确实会有所帮助。当我还是初学者的时候,我曾经认为伪代码都是废话,而是直接开始编码。

首先获取输入

mylist = input()

新建一个list/output

newlist = []

获取最大值

maxval = max(mylist)

遍历旧列表并除以最大值,并在每一步添加到新列表

for i in mylist:

  x = i / maxval

  newlist.append(x)

关于你的第二个问题,编程需要很大的耐心才能熟练掌握。你必须通过问题不断地奋斗和奋斗来发展技能。当然,拥有一位好老师或资源将使您免于许多不必要的挣扎。有些人看起来比其他人更快,但在我看来,他们只是更有经验地以逻辑方式思考。

我只需要练习 Python 几个月就掌握了它,因为我先学了 C++。我建议先使用 low-level 语言,然后再使用 higher-level 语言,但这是一个完全不同的话题。现在是 2022 年,你可以自学任何东西,但我 old-fashioned 如果你真的想这样做并以此为生,我建议你去上大学(我知道这是值得商榷的)。