为什么在 Python 的 Map 和 Str 中不可能有前导零 [英] Why leading zero not possible in Python's Map and Str

查看:65
本文介绍了为什么在 Python 的 Map 和 Str 中不可能有前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将数字转换为序列时不能在数字开头使用零的原因是什么?

代码示例

map(int,str(08978789787))

这给出了语法错误.

我想将前导数字为零的数字转换为序列.如何将这样的数字转换为序列?

解决方案

发生这种情况是因为前导零表示您正在编写一个八进制数,而八进制数中不能有 9 或 8.见:

<预><代码>>>>一 = 0123>>>一种83>>>一 = 010>>>一种8

你可以这样做:

<预><代码>>>>地图(整数,'08978789787')[0, 8, 9, 7, 8, 7, 8, 9, 7, 8, 7]

What is the reason that you cannot use zero at the beginning of a number when converting the number to a sequence?

Code example

map(int,str(08978789787))

which gives Syntax error.

I would like to convert numbers which leading digit is zero to a sequence. How can you convert such a number to a sequence?

解决方案

This happens because the leading zero means you are writing an octal number and you can't have 9 or 8 in an octal number. See:

>>> a = 0123
>>> a
83
>>> a = 010
>>> a
8

You can just do:

>>> map(int, '08978789787')
[0, 8, 9, 7, 8, 7, 8, 9, 7, 8, 7]

这篇关于为什么在 Python 的 Map 和 Str 中不可能有前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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