Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我想打印与列表中指定的相同的整数值_Python_Python 2.7 - Fatal编程技术网

Python 我想打印与列表中指定的相同的整数值

Python 我想打印与列表中指定的相同的整数值,python,python-2.7,Python,Python 2.7,输出为: the_count = [001, 002, 003, 004, 005] for number in the_count: print(number) 1 2 3 4 5 我的预期产出是: the_count = [001, 002, 003, 004, 005] for number in the_count: print(number) 1 2 3 4 5 或者看看zfill或rjust 或者看看zfill或rjust,试试以下方法: print('{0:

输出为:

the_count = [001, 002, 003, 004, 005]
for number in the_count:
    print(number)
1
2
3
4
5
我的预期产出是:

the_count = [001, 002, 003, 004, 005]
for number in the_count:
    print(number)
1
2
3
4
5
或者看看zfill或rjust

或者看看zfill或rjust,试试以下方法:

print('{0:03d}'.format(number))
试试这个:

print('{0:03d}'.format(number))

您应该将数字存储为字符串,同时将bcoz定义为整数001对我们来说与1相同,这也是python意外存储的内容。然而,在Python中,带前导“0”的数字表示八进制数字基8。所以它会一直工作到007年

the_count = [001, 002, 003, 004, 005]
for number in the_count:
    print(str(number).zfill(3))
但一旦你超过八进制极限,它就会失败。八进制数字是0、1、2、3、4、5、6、7、10、11等等

>>> a = 001
>>> a
1
>>> a = '001'
>>> a
'001'
>>> the_count = ['001', '002', '003', '004', '005']
>>> for i in the_count:
    print i
001
002
003
004
005

您应该将数字存储为字符串,同时将bcoz定义为整数001对我们来说与1相同,这也是python意外存储的内容。然而,在Python中,带前导“0”的数字表示八进制数字基8。所以它会一直工作到007年

the_count = [001, 002, 003, 004, 005]
for number in the_count:
    print(str(number).zfill(3))
但一旦你超过八进制极限,它就会失败。八进制数字是0、1、2、3、4、5、6、7、10、11等等

>>> a = 001
>>> a
1
>>> a = '001'
>>> a
'001'
>>> the_count = ['001', '002', '003', '004', '005']
>>> for i in the_count:
    print i
001
002
003
004
005

您是否打算在八进制第一行的列表中指定整数值?因为这就是您在Python2和Python3中所做的,所以列表值是语法错误。Python2认为任何以0开头的多位数整数文字都是以八进制格式给出的数值,请参阅Python2文档一节中的octinteger

如果这是您的目标,那么您可以将每个值格式化为一个带前导零的3位八进制数,如下所示:

>>> a = 008
SyntaxError: invalid token
在Python 3中,八进制文字的规则略有不同,第一行必须更改为:

the_count = [001, 002, 003, 004, 005]
for number in the_count:
    print(format(number, '03o'))

您是否打算在八进制第一行的列表中指定整数值?因为这就是您在Python2和Python3中所做的,所以列表值是语法错误。Python2认为任何以0开头的多位数整数文字都是以八进制格式给出的数值,请参阅Python2文档一节中的octinteger

如果这是您的目标,那么您可以将每个值格式化为一个带前导零的3位八进制数,如下所示:

>>> a = 008
SyntaxError: invalid token
在Python 3中,八进制文字的规则略有不同,第一行必须更改为:

the_count = [001, 002, 003, 004, 005]
for number in the_count:
    print(format(number, '03o'))

试着让它上升到009,看看会发生什么。:>>x=int1>>>NewStringVariable=strx.zfill3>>>打印NewStringVariable 001尝试将其上升到009,看看会发生什么情况。:>>x=int1>>>NewStringVariable=strx.zfill3>>打印NewStringVariable 001Form查询iam在POList:logger.InfoPonMember中获取PONumber的值,如PONLIST=00100200304005006在log中获取0在logI中获取0我并不真正了解Python日志记录包,如果没有必要将PONumber保留为整数类型,我会继续在您记录PONumber时将其转换为字符串,并执行以下操作:对于PONumber,在POList:logger.infostrPONumber.ZFILL3表单查询中,iam将获得如下值:EPOLIST=001002003004005006POList中的PONumber:logger.InfoPonMember在log中iam在logI中获得0我不太了解pythons日志包,如果没有必要将PONumber保留为整数类型,我会继续将其转换为字符串,当您记录它并执行以下操作时:对于PONumber in POList:logger.infostrPONumber.zfill3Iam从查询中获取值,如001002003,我想将其转换为“001”,“002”,“003”,查询中的值是否为单个字符串,如001002003?如果没有,具体是什么?提示:请尝试printreprquery查找。从查询中可以获取单个值。\u count=001005008006004\u count=001005008006004在Python 2中是无效语句,因为008是一个语法错误,因为8不是有效的八进制数字。printreprthe_count显示什么?我从查询中获取值,如001002003,我想转换为'001','002','003',查询中的值是否为单个字符串,如001002003?如果没有,具体是什么?提示:请尝试printreprquery查找。从查询中可以获取单个值。\u count=001005008006004\u count=001005008006004在Python 2中是无效语句,因为008是一个语法错误,因为8不是有效的八进制数字。PrintRepr\u计数显示什么?