Home
Sign Up
Sign In
V2EX
›
问与答
python 中 list 转换为 int
supman
·
Apr 12, 2015
· 5222 views
This topic created in 4247 days ago, the information mentioned may be changed or developed.
情况是这样的, 有一个list 里面只有一个 整数, 想把它转化为int类型, 网上有一个例子, 用的是
int_list = [int(i) for i in yourList]
我试了下, 这样确实能把数字取出来, 但类型竟然编程了 unicode了...
大家有啥方法不?
int
yourlist
unicode
5 replies
•
2015-04-12 17:56:15 +08:00
PeterD
Apr 12, 2015
1
x = ls[0] if ls else None
hitsmaxft
Apr 12, 2015 via iPhone
2
你直接拿出来不就行了
supman
OP
Apr 12, 2015
3
@
PeterD
@
hitsmaxft
[u'177'] 是这样的....数字前面还有个 u..., 这个是通过正则匹配后的结果, 类型确实是list, 但是直接取不出来...
PeterD
Apr 12, 2015
1
4
>>> ls = [u'12']
>>> x = int(ls[0]) if ls else None
>>> x
12
hitsmaxft
Apr 12, 2015 via iPhone
5
@
supman
你说的这个不是整形,是内容为数字的unicode字符串。取出来之后的进行数值计算。把字符串中数字计算出来。
About
·
Help
·
Advertise
·
Blog
·
API
·
FAQ
·
Privacy
·
Solana
·
1084 Online
Highest 6679
·
Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 33ms ·
UTC 23:34
·
PVG 07:34
·
LAX 15:34
·
JFK 18:34
♥ Do have faith in what you're doing.
❯