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

发布于 2025-01-24 01:08:40 字数 1121 浏览 0 评论 0原文

所以我一直在学习python。它一直在杀我。已经大约两个月了。我有2个问题。 1。解决此问题不需要45分钟的问题是什么? 2-在获得能力之前,您必须练习多长时间?

分析数据集时,例如人类高度的数据或人类的数据集 权重,一个常见的步骤是调整数据。这种调整可能是 通过标准化为0到1之间的值或扔掉 异常值。

对于此程序,请在浮子列表中阅读,然后通过 将所有值除以最大值。用两个输出每个值 小数点之后的数字。按照一个空间跟随每个数字输出。

ex:如果输入为:

30.0 50.0 10.0 100.0 65.0

输出为:

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 

So I've been really struggling with learning python. It's been killing me. It's been about 2 months now. I have 2 questions. 1. What is a simpler way to solve this problem that doesn't take 45 minutes? And 2 - how long did you have to practice Python before you became competent?

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: Compare output
3 / 3

Input
30.0 50.0 10.0 100.0 65.0
Your output

2: Compare output
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: Compare output
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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无法回应 2025-01-31 01:08:40

您只需要逐步进行此操作即可。获取输入:

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()

You just need to do this step by step. Grab the input:

num = input()

Split it into words and convert them all to floats:

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

Now we have a list of floats. Find the largest:

maxval = max(vals)

Divide them all by that value:

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

Print them:

for i in vals:
    print( "{:.2f}".format(i), end=' ')
print()
给妤﹃绝世温柔 2025-01-31 01:08:40

一个简单的方法是:

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

A simpler way would be this:

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))

The code prints precisely: 0.30 0.50 0.10 1.00 0.65

╭⌒浅淡时光〆 2025-01-31 01:08:40

回答您的问题:

  1. 解决此问题的简单方法是什么?

步骤1:您可以做的是将数据作为“ float”,然后跳过第一个数字

,如果要求是一个

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

第2步,您使用“输入”的方式也可以使用“输入”的方式:现在计算最大值的最大值

maximum = max(data)

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

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

out = [e/maximum for e in data]
  1. 您在获得胜利之前必须练习多长时间?

好吧,没有固定的答案,专注于解决问题,您就可以了。另外,没有定义的时间可以量化您需要练习的“长期”。据我所知,只要您想进入这个领域,就可以练习!

Answering your questions:

  1. What is a simpler way to solve this problem?

Step 1: What you can do is get the data as "float" with skipping first number

Alternatively, your way of looping with "input" works too if the requirement is sending lines one by one

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

Step 2: Now calculate the maximum value

maximum = max(data)

Step 3: Finally, just divide each element in list by the maximum you found in step 2 using a list comprehension

You can learn about list comprehension here: https://www.w3schools.com/python/python_lists_comprehension.asp

out = [e/maximum for e in data]
  1. How long did you have to practice Python before you became competent?

Well, there's no fixed answer for that, focus on solving problems and you are good to go. Also, there's no defined time that can quantify how "long" you need to practice. As far as I believe, you practice as long as you want to be in this field!

我喜欢麦丽素 2025-01-31 01:08:40

解决此问题的最简单方法是首先将其转换为numpy数组,然后将整个数组除以最大数字,

import numpy

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

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

根据我的经验,我花了大约2-3个月的时间才能在Python获得中级水平,但这是因为我为我提供了一个系统的计划和良好的指导。但是我确信即使不花一分钱,您也可以升级。所以振作起来!

The simplest way to do this problem would be to first convert this into a numpy array and then divide the whole array by the largest number,

import numpy

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

And about the second question...all I can say is that don't worry too much. Learning anything is pretty difficult at first, everyone was once at the same stage as you are right now. Have a systematic plan that you follow each day, know what you should be studying right now, and what you should be studying after you are done with the current topic. Seek someone's guidance if you feel like it. Don't take too much stress because not everyone learns at the same rate. It's okay to take a little more time...all that matters is that you learnt something.

From my experience it took me about 2-3 months to get Intermediate level at Python but that was because I paid for a course which provided me a systematic plan and good mentorship. But I am damn sure that you can level up even without spending a penny. So cheer up!

迟到的我 2025-01-31 01:08:40

伪代码中的一种简单方法。如果您解决一个大问题,那么在代码之前,伪代码和计划确实会有所帮助。当我是一个初学者时,我曾经认为这都是伪代码的废话,而是直接进入编码。

首先获取输入

mylist = input()

创建一个新列表/输出

newlist = []

获得最大

maxval = max(mylist)

浏览旧列表,然后除以最大划分,并在每个步骤中添加到新列表中,

for i in mylist:

  x = i / maxval

  newlist.append(x)

以便您的第二个问题,编程需要很多耐心才能擅长。您必须继续努力争取问题,以发展技能。尽管当然拥有一个好的老师或资源可以使您免于许多不必要的挣扎。有些人看起来比其他人快,但是我看到的方式,他们有更多的经验以合乎逻辑的方式工作。

我只需要练习几个月才能掌握它,因为我首先学会了C ++。我建议先从低级语言开始,然后再进入高级语言,但这是一个完全不同的话题。是2022年,您可以自己学到任何东西,但是我很老式,建议您如果您真的想这样做并从中谋生,请上大学(我知道非常有争议的)。

A simpler way would in pseudocode. And pseudocode and planning before you code really does help if your tackling a big problem. When I was a beginner I used to think it's all crap to pseudocode and instead go straight into coding.

First get the input

mylist = input()

Create a new list/output

newlist = []

Get the max

maxval = max(mylist)

Go through the old list and divide by the max and at each step add to the new list

for i in mylist:

  x = i / maxval

  newlist.append(x)

As for your second question, programming takes a lot of patience to get good at. You must continually struggle and struggle through problems to develop the skills. Although of course having a good teacher or resources will save you from a lot of needless struggling. Some people seem faster than others but the way I see it, they just have more experience working their mind in a logical way.

I only had to practice Python for a few months to get a hang of it because I learned C++ first. And I would suggest starting with a low-level language before going to higher-level ones but this is a whole different topic. It's 2022 you can learn anything by yourself but I'm old-fashioned I'd recommend if you really want to do this and make a living out of it go to college (very debatable I know).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文