简体   繁体   English

为什么我的计算机中的 Python 列表不包含超过 693 个数字?

[英]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:这是我的系统配置:

  • Intel Core i5, 5th gen英特尔酷睿 i5,第 5 代
  • 8GB of RAM, 1600 MHz bus speed 8GB 内存,1600 MHz 总线速度
  • Ubuntu 16.04.1 Ubuntu 16.04.1
  • Python 3.5.2蟒蛇 3.5.2

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.

相关问题 为什么python列表可以容纳比numpy数组更多的数据? - Why can python lists hold more data than numpy arrays? 计算机无法存储大约 15 位以上的数字。 有没有办法在 Python 中计算这些总和? - Computer cannot store numbers with more than approximately 15 digits. Is there a way to do these sums in Python? roslibpy 消息不包含多个键值对 - roslibpy Messages don't hold more than one keyvalue pair Python 2 如何比较字符串和整数? 为什么列表比数字大,元组比列表大? - How does Python 2 compare string and int? Why do lists compare as greater than numbers, and tuples greater than lists? 我的程序运行良好,但我不明白为什么? 与函数和嵌套列表有关 - My program works well but I don't understand why? Something to do with functions and nested lists 如何在 Python 中使用嵌套列表遍历列表/为什么我的代码不起作用? - How do I iterate through lists with nested lists in Python / why doesn't my code work? 为什么列表具有__reverse __()特殊方法,而Python中却没有元组? - Why do lists have a __reverse__() special method but tuples don't in Python? 为什么 python 列表在函数作用域外持久存在,而整数则不然? - Why do python lists persist outside function scope while integers don't? 为什么复制的词典指向同一目录但列表却没有? - Why do copied dictionaries point to the same directory but lists don't? Python/Mpmath:为什么我没有得到大数除法的任何小数点,而是得到较小的数 - Python/Mpmath: Why don't I get any decimal points for large number division, but do for smaller numbers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM