python - 返回负数的数字列表

标签 python

我尝试通过将数字转换为链接列表来将数字转换为其数字列表。它对于正数工作正常,但对于负数不起作用: 我尝试这样做:

def number_to_list(number):
    head,tail = None,None
    for x in str(number):
        if not x.isdigit():
           a1 = x
           continue
        map(lambda:x,x*a1)
        node = Node(int(x))
        if head:
           tail.next = node
        else:
           head = node
        tail = node
    return head

当我通过使用 head 作为参数传递到的库来调用函数 head = number_to_list(-120) 以及稍后的 listutils.from_linked_list(head) 时,它应该返回 [-1,-2,0] 但它正在返回 []。

最佳答案

根据您对问题的描述,很难猜测您想要什么。也许你可以详细说明一下上下文,否则我们几乎只能猜测。无论如何,我们开始吧:

x = -120
[-int(d) if x < 0 else int(d) for d in str(abs(x))]
# [-1, -2, 0]

关于python - 返回负数的数字列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21531575/

相关文章:

python - tensorflow 错误: Could not create cudnn handle: CUDNN_STATUS_NOT_INITIALIZED

python - 当父进程中的属性更改时,更新子进程中的对象属性

python - 在 Django 1.6 中获取用户配置文件的新方法是什么?

python - 使用 python 修复列缩进

python - 在Python中修改递归函数之外的列表

python - 过滤 Blaze 表中的日期

python - 如何将字典的键转换为单列的 pandas 数据框?

python - 如何在Tensorflow中绘制精度曲线

python - 将文件复制到文件夹

python - SQLAlchemy导入错误: import _mysql