python用input输入整数列表_Python - 将用户输入整数拆分为列表,其中每个条目为2位数...

1586010002-jmsa.png

So I'm trying to split an arbitrarily long user input integer into a list where each entry is 2 digits, and if the number has an odd amount of integers put the only single digit as the first digit. (Where I will then proceed to put a zero in front of it)

I know that putting user integer input into a list looks like:

userintegerlist = [int(i) for i in str(user_input)]

print userintegerlist

And my input (say it's 45346) will look like [4,5,3,4,6]. But I want it to look like: [4,53,46]. Or if input is 68482238, it will be: [68,48,22,38].

Is this possible? All the code is in Python by the way.

解决方案

You can do it with string methods fairly easily, as other answers have already shown. I direct you to the related grouper recipe in itertools.

I want to mention that it may be more efficient to do it with maths:

>>> n = 45346

>>> output = []

>>> while n:

... output.append(n % 100)

... n //= 100

...

>>> output = output[::-1]

>>> print output

[4, 53, 46]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值