Python 中 raw_input() 可以填充的最大字符数

发布于 2024-12-21 04:25:58 字数 380 浏览 0 评论 0原文

对于 InterviewStreet 挑战,我们必须能够容纳从键盘输入的 10,000 个字符串,但是当我将 10k 长的单词复制/粘贴到本地测试中时,它会在 1000 个左右被截断。

Python 的官方限制是多少?有没有办法改变这一点?

谢谢大家

,这是挑战:

http://www.interviewstreet.com/recruit/challenges/solve/view/4e1491425cf10/4edb8abd7cacd

For an InterviewStreet challenge, we have to be able to accomodate for a 10,000 character String input from the keyboard, but when I copy/paste a 10k long word into my local testing, it cuts off at a thousand or so.

What's the official limit in Python? And is there a way to change this?

Thanks guys

Here's the challenge by-the-by:

http://www.interviewstreet.com/recruit/challenges/solve/view/4e1491425cf10/4edb8abd7cacd

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

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

发布评论

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

评论(3

九八野马 2024-12-28 04:25:58

您确定您的 10k 长单词不包含换行符吗?


raw_input([提示])

如果提示参数存在,则会将其写入标准输出,且不带尾随换行符。然后该函数从输入中读取一行,将其转换为字符串(去除尾随换行符),然后返回该字符串。当读取 EOF 时,会引发 EOFError。

...

如果加载了 readline 模块,则 raw_input() 将使用它来提供详细信息行编辑和历史记录功能。

raw_input 返回的缓冲区没有最大限制(在 python 中),并且当我测试了 stdin 的一些大长度输入时,我无法重现您的结果。我尝试在网上搜索有关此问题的信息,但没有找到任何可以帮助我回答您的问题的信息。

我的测试

  :/tmp% python -c 'print "A"*1000000' | python -c 'print len (raw_input ())';
  1000000
  :/tmp% python -c 'print "A"*210012300' | python -c 'print len (raw_input ())';
  210012300
  :/tmp% python -c 'print "A"*100+"\n"+"B"*100' | python -c 'print len (raw_input ())'; 
  100

Are you sure of the fact that your 10k long word doesn't contain newlines?


raw_input([prompt])

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised.

...

If the readline module was loaded, then raw_input() will use it to provide elaborate line editing and history features.

There is no maximum limit (in python) of the buffer returned by raw_input, and as I tested some big length of input to stdin I could not reproduce your result. I tried to search the web for information regarding this but came up with nothing that would help me answer your question.

my tests

  :/tmp% python -c 'print "A"*1000000' | python -c 'print len (raw_input ())';
  1000000
  :/tmp% python -c 'print "A"*210012300' | python -c 'print len (raw_input ())';
  210012300
  :/tmp% python -c 'print "A"*100+"\n"+"B"*100' | python -c 'print len (raw_input ())'; 
  100
花伊自在美 2024-12-28 04:25:58

我也有同样的经历,并且发现如果不导入 readline 模块,Python 就会限制 raw_input 的输入长度。一旦我导入了 readline 模块,它就解除了限制(或者至少将其显着提高到我使用的文本工作得很好的程度)。这是在我的 Mac 上使用 Python 2.7.15 进行的。此外,已确认至少可在 3.9.5 上运行。

I had this same experience, and found python limits the length of input to raw_input if you do not import the readline module. Once I imported the readline module, it lifted the limit (or at least raised it significantly enough to where the text I was using worked just fine). This was on my Mac with Python 2.7.15. Additionally, it’s been confirmed working on at least 3.9.5.

嗼ふ静 2024-12-28 04:25:58

我想这是挑战的一部分。 常见问题解答表明 raw_input() 可能不是最佳方法:

下面列出了最常见(可能是简单的)方法。 (...)

确实有Python 标准模块可以帮助处理系统输入/输出。

I guess this is part of the challenges. The faq suggest raw_input() might not be the optimal approach:

The most common (possibly naive) methods are listed below. (...)

There are indeed Python standard modules helping to handle system input/output.

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