小编Nil*_*esh的帖子

类比不适用于08和09

虽然在python中进行类型转换我遇到了错误.

int(01)
int(02)
int(03)
int(04)
int(05)
int(06)
int(07)
Run Code Online (Sandbox Code Playgroud)

最重要的是工作正常.

但是当我为轰鸣声做同样的事情时:

int(08)
Run Code Online (Sandbox Code Playgroud)

int(09)
Run Code Online (Sandbox Code Playgroud)

我收到了错误,即

SyntaxError: invalid token
Run Code Online (Sandbox Code Playgroud)

我知道,这种类型转换对于将int转换为int是不正确的.

但我只是想知道,当它适用于01至07时,为什么它不适用于08和09?

python python-2.7

4
推荐指数
1
解决办法
531
查看次数

对浮动字符串列表进行排序

我需要排序到以下列表:

b = ['0.10', '0.1', '0.11', '0.12', '0.2', '0.3']
Run Code Online (Sandbox Code Playgroud)

...这样输出将是:

['0.1', '0.2', '0.3', '0.10', '0.11', '0.12']
Run Code Online (Sandbox Code Playgroud)

它们看起来像float值,但这不是我想要的顺序 - 它们需要被视为版本号 - 即0.1不是0.10(第一个是零点1,第二个是零点10).当我float用作键时,我得到一个不同的顺序:

>>>b.sort(key=float)
>>>b 
['0.10', '0.1', '0.11', '0.12', '0.2', '0.3']
Run Code Online (Sandbox Code Playgroud)

我还想对以下列表进行排序:

['3.0.1', '3.0.10', '3.0.11', '3.0.12', '3.0.2', '3.0.3', 
 '3.0.4', '3.0.5', '3.0.6', '3.0.7', '3.0.8', '3.0.9', 
 '3.1.1', '3.1.10', '3.1.11', '3.1.12', '3.1.2', '3.1.3', 
 '3.1.4', '3.1.5', '3.1.6', '3.1.7', '3.1.8', '3.1.9']
Run Code Online (Sandbox Code Playgroud)

按此顺序:

['3.0.1', '3.0.2', '3.0.3', '3.0.4', '3.0.5', '3.0.6',
 '3.0.7', '3.0.8', '3.0.9', '3.0.10', '3.0.11', '3.0.12',
 '3.1.1', '3.1.2', '3.1.3', '3.1.4', '3.1.5', '3.1.6',
 '3.1.7', '3.1.8', '3.1.9', …
Run Code Online (Sandbox Code Playgroud)

python

-1
推荐指数
1
解决办法
111
查看次数

标签 统计

python ×2

python-2.7 ×1