追加到列表十位整数,并置某些条目以" L" [英] Appending ten digit integer to list concatenates some entries with an "L"

查看:100
本文介绍了追加到列表十位整数,并置某些条目以" L"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个小脚本找到的电子的(相对于旧的谷歌广告)的数字n位的素数:

I wrote a small script to find n-digit primes in the digits of e (in relation to that old Google ad):

import math

# First 251 digits of e
e_digits = ("2"
            "7182818284 5904523536 0287471352 6624977572 4709369995"
            "9574966967 6277240766 3035354759 4571382178 5251664274"
            "2746639193 2003059921 8174135966 2904357290 0334295260"
            "5956307381 3232862794 3490763233 8298807531 9525101901"
            "1573834187 9307021540 8914993488 4167509244 7614606680")
e_digits = e_digits.replace(" ", "")

digits = int(raw_input("Number of digits: "))
print "Finding ", str(digits) + "-digit primes in the first", len(e_digits), "digits of e."

numbers = []
primes = []

# Creates list of numbers based on variable digits
for n in range(0,len(e_digits) - (digits - 1)):
    test_number = e_digits[n:n+digits]
    numbers.append(int(test_number))

# Checks each number for divisors smaller than its sqrt, then appends to list primes
for n in numbers:
    n_sqrt = int(math.floor(math.sqrt(n)))
    div = []
    for i in range(2,n_sqrt+1):
        if n % i == 0:
            div.append(i)
    if div == []:
        primes.append(n)

print primes

然而,当我设置数字= 10,这是印刷:

However, when I set digits = 10, this is printed:

[7427466391L, 7413596629L, 6059563073L, 3490763233L, 2988075319L, 1573834187, 7021540891L, 5408914993L]

之外的所有六个号码列表中的条目已连接在一起的L,我不知道为什么。当我运行在空闲的code,以及在CMD的问题就出现了,虽然使用这个特定的code追加十位整数,只有当。

All of the list entries except for number six has been concatenated with an "L", and I have no clue why. The problem arises when I run the code in IDLE as well as in CMD, though only when appending ten digit integers using this specific code.

在过去for循环的if语句,如果我打印n个正确的号码印,或者如果我转换n至字符串追加前。然而,然后转换为一个整数再次产生相同的问题。

In the if-statement in the last for loop, the correct numbers are printed if I print n, or if I convert n to a string before appending. However, then converting to a integer again creates the same problem.

的问题也会发生与位= 11,但不与数字及下; 10。

The problem also occurs with digits = 11, but not with digits < 10.

我不能为我的生活找出错误(或者找出是否有错误可言,真的)。这方面的一些建议将不胜AP preciated。

I cannot for the life of me find the error (or figure out if there is an error at all, really). Some advice on this would be greatly appreciated.

推荐答案

您code是工作得很好,你看到的是正常的。这些都是的文字重新presentations Python的长整数的。

Your code is working just fine and what you see is normal. Those are literal representations of Python long integers.

在打印列表中,的内容的清单打印为重presentations,同样的输出作为的 再版()函数会给。另一种方法是打印列表的各个元素来代替。

When printing a list, the contents of a list are printed as representations, the same output as the repr() function would give. The alternative is to print individual elements of the list instead.

您不必然而担心长期再presentation。这就是通过泄露的Python整数只是实现的细节:

You don't need to worry about the long representation, however. That is just an implementation detail of Python integers leaking through:

>>> 1234567890
1234567890
>>> type(1234567890)
<type 'int'>
>>> 12345678901234567890
12345678901234567890L
>>> type(12345678901234567890)
<type 'long'>

在这里,Python的跨preTER打印前pressions的结果,为再版()重新presentations 。整数比所有的sys.maxint 大自动成为整数。

Here, the Python interpreter prints the results of expressions as repr() representations too. Integers larger than sys.maxint automatically become long integers.

引用文档:

普通的整数(也只是称为整数)使用实施在C,这给他们带来precision(的至少32位所有的sys.maxint 始终设置为当前平台的最大平原整数值,最小值为 -sys.maxint - 1 )。长整型有无限的precision。

Plain integers (also just called integers) are implemented using long in C, which gives them at least 32 bits of precision (sys.maxint is always set to the maximum plain integer value for the current platform, the minimum value is -sys.maxint - 1). Long integers have unlimited precision.

朴实的整数文本(包括二进制,十六进制和八进制数)产生纯整数,除​​非他们表示值过大被重新psented作为一个普通的整数,在这种情况下,他们得到一个长整型$ P ​​$。整型常量与'L'L后缀产量长整型('L 是preferred因为1升看起来太多的十一!)。

Unadorned integer literals (including binary, hex, and octal numbers) yield plain integers unless the value they denote is too large to be represented as a plain integer, in which case they yield a long integer. Integer literals with an 'L' or 'l' suffix yield long integers ('L' is preferred because 1l looks too much like eleven!).

定期和长整数之间的比较和算术完全支持透明的:

Comparisons and arithmetic between regular and long integers is fully supported and transparent:

的Python充分支持混合算术:当一个二进制算术运算器具有不同的数字类型的操作数,与窄类型的操作数被加宽到,其他,其中纯整数比长整型窄比浮点窄的较复杂的窄。混合型的数字之间的比较使用相同的规则。

Python fully supports mixed arithmetic: when a binary arithmetic operator has operands of different numeric types, the operand with the "narrower" type is widened to that of the other, where plain integer is narrower than long integer is narrower than floating point is narrower than complex. Comparisons between numbers of mixed type use the same rule.

Python 3中完全删除的区别。

Python 3 removed the distinction altogether.

这篇关于追加到列表十位整数,并置某些条目以&QUOT; L&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆