将数字转换为整数列表

发布于 2024-07-17 03:13:00 字数 192 浏览 6 评论 0原文

如何编写下面的magic函数?

>>> num = 123
>>> lst = magic(num)
>>>
>>> print lst, type(lst)
[1, 2, 3], <type 'list'>

How do I write the magic function below?

>>> num = 123
>>> lst = magic(num)
>>>
>>> print lst, type(lst)
[1, 2, 3], <type 'list'>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

記憶穿過時間隧道 2024-07-24 03:13:00

你是这个意思?

num = 1234
lst = [int(i) for i in str(num)]

You mean this?

num = 1234
lst = [int(i) for i in str(num)]
勿挽旧人 2024-07-24 03:13:00
a = 123456
b = str(a)
c = []

for digit in b:
    c.append (int(digit))

print c
a = 123456
b = str(a)
c = []

for digit in b:
    c.append (int(digit))

print c
与他有关 2024-07-24 03:13:00

你可以这样做:

>>> num = 123
>>> lst = map(int, str(num))
>>> lst, type(lst)
([1, 2, 3], <type 'list'>)

You could do this:

>>> num = 123
>>> lst = map(int, str(num))
>>> lst, type(lst)
([1, 2, 3], <type 'list'>)
终难愈 2024-07-24 03:13:00
magic = lambda num: map(int, str(num))

然后就做

magic(12345) 

magic(someInt) #or whatever
magic = lambda num: map(int, str(num))

then just do

magic(12345) 

or

magic(someInt) #or whatever
迷爱 2024-07-24 03:13:00
>>> from collections import deque
>>> def magic(num):
        digits = deque()
        while True:
            num,r = divmod(num,10)
            digits.appendleft(r)
            if num == 0:
                break
        return list(digits)

>>> magic(123)
[1, 2, 3]

根据我的计时,即使对于较小的示例,此解决方案也比字符串方法 (magic2) 快得多。

>>> def magic2(num):
        return [int(i) for i in str(num)]

时间:

magic

>>> timeit.timeit(setup='from __main__ import magic', stmt='magic(123)')
1.3874572762508706
>>> timeit.timeit(setup='from __main__ import magic', stmt='magic(999999999)')
3.2624468999981673

magic2

>>> timeit.timeit(setup='from __main__ import magic2', stmt='magic2(123)')
3.693756106896217    
>>> timeit.timeit(setup='from __main__ import magic2', stmt='magic2(999999999)')
10.485281719412114
>>> from collections import deque
>>> def magic(num):
        digits = deque()
        while True:
            num,r = divmod(num,10)
            digits.appendleft(r)
            if num == 0:
                break
        return list(digits)

>>> magic(123)
[1, 2, 3]

According to my timings, this solution is considerably faster than the string method (magic2), even for smaller examples.

>>> def magic2(num):
        return [int(i) for i in str(num)]

Timings:

magic

>>> timeit.timeit(setup='from __main__ import magic', stmt='magic(123)')
1.3874572762508706
>>> timeit.timeit(setup='from __main__ import magic', stmt='magic(999999999)')
3.2624468999981673

magic2

>>> timeit.timeit(setup='from __main__ import magic2', stmt='magic2(123)')
3.693756106896217    
>>> timeit.timeit(setup='from __main__ import magic2', stmt='magic2(999999999)')
10.485281719412114
慵挽 2024-07-24 03:13:00

不要使用单词list作为变量名! 它是Python内置数据类型的名称。

另外,请澄清你的问题。 如果您正在寻找一种创建单成员列表的方法,请执行以下操作:

a = 123
my_list = [a]

以及“pythonizing”Cannonade 的答案:

a = 123
my_list = [int(d) for d in str(a)]

Don't use the word list as variable name! It is a name of python built in data type.

Also, please clarify your question. If you are looking for a way to create a one-member list, do the following:

a = 123
my_list = [a]

and "pythonizing" Cannonade's answer:

a = 123
my_list = [int(d) for d in str(a)]
猫弦 2024-07-24 03:13:00
num = map(int, list(str(num)))
num = map(int, list(str(num)))
不忘初心 2024-07-24 03:13:00

如果它被命名为魔法,为什么不直接使用魔法:

def magic(x):
    if x < 10:
        return [x]
    else:
        return magic(x//10) + [x%10]

If it is named as magic, why not just use magic:

def magic(x):
    if x < 10:
        return [x]
    else:
        return magic(x//10) + [x%10]
甜警司 2024-07-24 03:13:00

对于 python 3.x:

num = 1234
lst = list(map(int, str(num)))

for python 3.x:

num = 1234
lst = list(map(int, str(num)))
从来不烧饼 2024-07-24 03:13:00

你可以试试这个:

def convert_to_list(number):
    return list(map(lambda x: int(x), str(number)))

convert_to_list(1245)

You can try this:

def convert_to_list(number):
    return list(map(lambda x: int(x), str(number)))

convert_to_list(1245)
羁客 2024-07-24 03:13:00

只需使用:

a= str (num)
lst = list(a)

Just use :

a= str (num)
lst = list(a)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文