将十位整数附加到列表将一些条目与“L"连接起来. [英] Appending ten digit integer to list concatenates some entries with an "L"

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

问题描述

我写了一个小脚本来在 e 的数字中找到 n 位质数(与那个旧的 Google 广告有关):

导入数学# e 的前 251 位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("位数:"))打印 "Finding ", str(digits) + "-digit primes in the first", len(e_digits), "digits of e."数字 = []素数 = []# 根据可变数字创建数字列表对于范围内的 n(0,len(e_digits) - (digits - 1)):test_number = e_digits[n:n+digits]numbers.append(int(test_number))# 检查每个数字是否有小于其 sqrt 的除数,然后附加到列表质数对于 n 数字:n_sqrt = int(math.floor(math.sqrt(n)))div = []对于范围内的 i (2,n_sqrt+1):如果 n % i == 0:div.append(i)如果 div == []:素数.append(n)打印素数

但是,当我设置数字 = 10 时,会打印:

[7427466391L, 7413596629L, 6059563073L, 3490763233L, 2988075319L, 1573834187, 70215408918L] 19

除了第 6 个之外的所有列表条目都用L"连接,我不知道为什么.当我在 IDLE 和 CMD 中运行代码时会出现问题,但仅在使用此特定代码附加十位整数时才会出现.

在最后一个 for 循环的 if 语句中,如果我打印 n,或者在追加之前将 n 转换为字符串,则会打印正确的数字.但是,然后再次转换为整数会产生同样的问题.

digits = 11 也会出现这个问题,但digits

我终其一生都找不到错误(或弄清楚是否有错误,真的).对此的一些建议将不胜感激.

解决方案

您的代码运行良好,您看到的一切正常.这些是 Python 长整数.

打印列表时,列表的内容被打印为表示,与 repr() 函数 会给出.另一种方法是打印列表的各个元素.

然而,您无需担心长表示.这只是泄漏的 Python 整数的实现细节:

<预><代码>>>>12345678901234567890>>>类型(1234567890)<输入'int'>>>>1234567890123456789012345678901234567890L>>>类型(12345678901234567890)<输入'长'>

在这里,Python 解释器将表达式的结果打印为 repr() 表示 too.大于 sys.maxint 的整数会自动变成 long 整数.

引用文档:

<块引用>

普通整数(也称为整数)在 C 中使用 long 实现,这为它们提供至少 32 位的精度(sys.maxint 始终设置为当前平台的最大纯整数值,最小值为 -sys.maxint - 1).长整数具有无限精度.

<块引用>

未修饰的整数文字(包括二进制、十六进制和八进制数)产生纯整数,除非它们表示的值太大而无法表示为纯整数,在这种情况下它们会产生长整数.带有 'L''l' 后缀的整数文字产生长整数('L' 是首选,因为 1l> 看起来太像十一了!).

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

<块引用>

Python 完全支持混合算术:当一个二元算术运算符有不同数值类型的操作数时,narrower"类型的操作数被加宽到另一个类型的操作数,其中普通整数比长整数窄,比浮点数窄比复杂更窄.混合型数之间的比较使用相同的规则.

Python 3 完全消除了这种区别.

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.

解决方案

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.

这篇关于将十位整数附加到列表将一些条目与“L"连接起来.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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