Appending ten digit integer to list concatenates s

2019-07-29 11:04发布

问题:

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

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

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

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.

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.

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

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.

回答1:

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

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.

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

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

Quoting the documentation:

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.

and

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 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 removed the distinction altogether.