python3 求助 整形→序列
萌新,求助大佬。在python3 里面,怎样把 整形 转换成 序列?
我想要的效果如下:
1234 → 这样的,
应该怎么编写呢?
{:5_111:} {:5_111:} {:5_111:} a=1234
b=str(a)
lis=[]
for each in b:
lis.append(each)
生成的是["1","2","3","4"]
再转成int,好像有点笨 函数写法:
def f(x):
lst=[]
while x:
lst.append(x%10)
x //= 10
lst.reverse()
return lst
列表推导:
a= BngThea 发表于 2018-2-6 10:03
函数写法:
好厉害啊!不明觉厉,回家试一试,先谢谢了! checkily 发表于 2018-2-6 09:52
生成的是["1","2","3","4"]
再转成int,好像有点笨
对的,我一开始也用了这个,生成的就是有引号的,再转成int的话,是不是直接int() 这样就可以了? 是的,就是比较啰嗦。用函数吧。 #-*- coding:utf-8 -*-
# Python 2.7
a=input('the number you want type in:')
b=len(str(a))
c=
print c 活用各种方法,一行代码就行:
list(map(lambda x: int(x), str(1234))) 太阳花田 发表于 2018-2-6 22:49
活用各种方法,一行代码就行:
好厉害啊~ 学习了,谢谢!
那我想再问下,怎么把序列变回整形呢? ouyunfu 发表于 2018-2-6 22:18
#-*- coding:utf-8 -*-
# Python 2.7
a=input('the number you want type in:')
谢谢~ 可惜不能直接试,因为我用的python3……残念 元宝 发表于 2018-2-7 00:06
谢谢~ 可惜不能直接试,因为我用的python3……残念
#-*- coding:utf-8 -*-
# Python 3
a=eval(input('the number you want type in:'))
b=len(str(a))
c=
print (c) 元宝 发表于 2018-2-7 00:04
好厉害啊~ 学习了,谢谢!
那我想再问下,怎么把序列变回整形呢?
int((str()).replace(',', '').replace(' ', ''))
一开始往复杂了想,发现没什么特别的号办法
简单点想发现更简单!{:10_266:}
页:
[1]