[英]Why do Python lists in my computer don't hold more than 693 numbers?
I was solving the Day 0 of 10 Days of Statistics problems in HackerRank the other day.前几天我正在解决 HackerRank 中 10 天统计问题的第 0天。 My solution was pretty straightforward and it worked with almost all the test cases.我的解决方案非常简单,几乎适用于所有测试用例。 The one my code fails in has 2500 numbers as input.我的代码失败的那个有 2500 个数字作为输入。 I placed a few prints to see how this is happening and discovered my list for the numbers holds only the 693 values of all.我放了几张照片来看看这是怎么发生的,发现我的数字列表只包含所有的 693 个值。
Here is the part to find median:这是查找中位数的部分:
number_of_items = int(input().strip())
inputs = list(map(int, input().split(' ')))
inputs.sort()
if (number_of_items % 2) == 0:
n = number_of_items // 2 - 1
median = (inputs[n] + inputs[n+1])/2
else:
n = number_of_items // 2
median = inputs[n-1]
print(median)
Here is the full code: https://gist.github.com/mnzr/5a6f6c1c49d4dc0dbb940ed3ecba79ff这是完整的代码: https : //gist.github.com/mnzr/5a6f6c1c49d4dc0dbb940ed3ecba79ff
I have tried this code on an online editor and it worked, with an exception: the mode was way off from the actual number.我已经在在线编辑器上尝试过这段代码,它工作正常,但有一个例外:模式与实际数字相差甚远。
I thought Python lists can hold large amount of numbers!我认为 Python 列表可以容纳大量数字! Why doesn't it work on my PC?为什么它在我的电脑上不起作用?
Edit: I have had a friend solve the problem on his own and relay me the results.编辑:我有一个朋友自己解决了问题并将结果转告给我。 Then told him to run my code and tell me what he sees.然后告诉他运行我的代码并告诉我他看到了什么。 Here is his code: https://gist.github.com/sz-ashik440/192bc22b18da0292832e65997a6787a7 Here is what happened: 1: His code worked, he said.这是他的代码: https : //gist.github.com/sz-ashik440/192bc22b18da0292832e65997a6787a7这是发生的事情: 1:他说,他的代码有效。 I ran it and it worked on my PC too!我运行了它,它也可以在我的 PC 上运行! For the first time that is.这是第一次。 2: I gave him my code and run it myself again. 2:我给了他我的代码,然后自己再次运行。 I saw there were only 693 elements, he reported the same.我看到只有 693 个元素,他报告了相同的内容。 3: And perhaps the most surprising thing is, his own version has gave the same out of index error and an array with the size of 693! 3:也许最令人惊讶的是,他自己的版本给出了同样的索引错误和大小为693的数组! My friend's own code on his own PC now gives wrong answers.我朋友在他自己的 PC 上自己的代码现在给出了错误的答案。
Here is my system config:这是我的系统配置:
My friend is using Python 3.4.3 on Ubuntu 14.04.我的朋友在 Ubuntu 14.04 上使用 Python 3.4.3。
I don't know why you are experiencing this limitation when running your code on your local machine, however, I strongly suspect that it is due to your input data - perhaps a stray new line character as suggested by Martijn Pieters .我不知道为什么您在本地机器上运行代码时会遇到此限制,但是,我强烈怀疑这是由于您的输入数据 - 可能是Martijn Pieters建议的一个新行字符。
The problem that you have with HackerRank is due to your mode calculation:您对 HackerRank 的问题是由于您的模式计算:
# mode
occurances = {n: inputs.count(n) for n in inputs}
mode = min(occurances)
print(mode)
This always selects the lowest value from the input, not the lowest of the most frequently occurring values.这总是从输入中选择最低值,而不是最常出现的值中的最低值。 Here is one way to do that:这是一种方法:
from collections import Counter
c = Counter(inputs)
max_count = c.most_common(1)[0][1]
print(sorted([x for x in c if c[x] == max_count])[0])
Note that there is also another problem, you need to round the mean and median values to 1 decimal place, but this error is not exposed by HackerRank's test data.注意还有一个问题,你需要将平均值和中值四舍五入到小数点后一位,但是这个错误并没有被HackerRank的测试数据暴露出来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.