Python 2 中整数值尾随的“L”(后缀)是什么意思?
- 2025-02-11 09:51:00
- admin 原创
- 75
问题描述:
我编写了一些代码来从 SQL 数据库中查询一行、zip
带有列名的值,从而生成这个字典:
{'estimated': '',
'suffix': '',
'typeofread': 'g',
'acct_no': 901001000L,
'counter': 0,
'time_billed': datetime.datetime(2012, 5, 1, 9, 5, 33),
'date_read': datetime.datetime(2012, 3, 13, 23, 19, 45),
'reading': 3018L,
'meter_num': '26174200'}
例如,请注意 的值'reading'
写为3018L
,而不仅仅是3018
。为什么?
我发现我可以L
通过使用 转换值来删除int
,但我想了解它的含义。
解决方案 1:
l
因为在 Python 3 之前的 Python 版本中,长整数文字以或后缀表示L
。在 Python 3 中,int
s 和long
s 已合并为,其功能与以前int
非常相似。long
请注意,从技术上讲,Python(2)int
相当于 C 的long
,而 Python 的long
更像是BigNumber
具有无限精度的 -type 东西(现在 Python 3 的类型就是这种情况int
)。
http://docs.python.org/library/stdtypes.html#numeric-types-int-float-long-complex
解决方案 2:
L
是针对long
数据类型的。
例如,
age = 24 # int
bankBalance = 20000005L # long
解决方案 3:
因为它们不是真正的整数,所以它们是“长整数”。
https://docs.python.org/2/library/stdtypes.html#typesnumeric
请注意,这仅适用于 Python 2。在 Python 3 中,int
和long
被统一为 Python 2 所称的long
,即具有无限精度的整数值。
不过,它们通常不会带来太多麻烦
>>> a=1
>>> b=long(1)
>>> a
1
>>> b
1L
>>> a==b
True
关于此问题的其他 stackoverflow 问题:Python 如何管理 int 和 long?
相关推荐
热门文章
项目管理软件有哪些?
热门标签
曾咪二维码
扫码咨询,免费领取项目管理大礼包!
云禅道AD