我尝试通过将数字转换为链接列表来将数字转换为其数字列表。它对于正数工作正常,但对于负数不起作用: 我尝试这样做:
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/