简体   繁体   English

如何将结果打印为列表?

[英]How can I print my outcome as a list?

I've created a program where the output prints as, for example, 36, 6, 2, 1 . 我创建了一个程序,输出的输出例如为36, 6, 2, 1

I want it to print [36, 6, 2, 1] . 我希望它打印[36, 6, 2, 1]

This is my current code: 这是我当前的代码:

def collatz(n):

    print(n, end = ', ')

    while (n > 1):
        if n % 2 == 0:
            n=int(n**0.5)
        else:
            n=int(n**1.5)
        if n == 1:
            print(1)
        else:
            print(n, end = ', ')

I am not sure what to edit at this point as I've tried a lot messing with the print statements and I have seen in other posts where print(*n, ...) was used but I get the error: 我不确定在这一点上要编辑什么,因为我已经尝试了很多处理print语句的麻烦,并且在其他使用了print(*n, ...)帖子中也看到了print(*n, ...)但是出现错误:

TypeError: print() argument after * must be a sequence, not int.

Which I get why it wouldn't work so I'm lost at this point. 我明白为什么它不起作用,所以我现在迷路了。

To control how its printed, simply print the [ and ] separately. 要控制其打印方式,只需分别打印[] Don't confuse how its printed with how its stored or what data type it has. 不要混淆其打印方式存储方式或数据类型。

def collatz(n):

    print('[{}'.format(n), end=', ')

    while (n > 1):
        if n % 2 == 0:
            n=int(n**0.5)
        else:
            n=int(n**1.5)
        if n == 1:
            print(1, end=', ')
        else:
            print(n, end = ', ')
    print(']', end='')

The best way would be to create a list and append the values of n to the list. 最好的方法是创建一个列表并将n的值附加到列表中。 In this way you can take the advantage of the builtin functionality of str(list) which automatically adds [ and ] to the end while printing. 这样,您可以利用str(list)的内置功能,该功能在打印时自动将[]添加到末尾。

A sample code can be 示例代码可以是

def collatz(n):

    templist = [n]    
    while (n > 1):
        if n % 2 == 0:
            n=int(n**0.5)
        else:
            n=int(n**1.5)
        if n == 1:
            templist.append(1)
        else:
            templist.append(n)
    print(templist)

Now when you run collatz(36) you get [36, 6, 2, 1] 现在,当您运行collatz(36)您会得到[36, 6, 2, 1] collatz(36) [36, 6, 2, 1]

You can simplify your function considerably by getting rid of the if condition since 通过消除if条件,可以大大简化函数,因为

if n % 2 == 0:
  n=int(n**0.5)
else:
  n=int(n**1.5)

Is the same as: n % 2 + 0.5 等于: n % 2 + 0.5

And also using recursion rather than iteration. 并且也使用递归而不是迭代。 So your collatz function can simply implemented as this: 因此,您的collatz函数可以简单地实现为:

def collatz(n):
  if n <= 1:
    return [1]
  return [n] + collatz(int(n ** (n % 2 + 0.5)))

collatz(n) will now return an array which is fine, its better if functions have a single responsibility. collatz(n)现在将返回一个很好的数组,如果函数具有单一职责,则更好。 Now you can wrap it in a function which prints the array in a Python-like format: 现在,您可以将其包装在一个函数中,该函数以类似Python的格式打印数组:

def print_collatz(n):
  return str(collatz(n))

So you can simply call: 因此,您可以简单地致电:

print_collatz(36)
# [36, 6, 2, 1]

To return an array-formatted string: 要返回数组格式的字符串:

You can create a list and print its string representation: 您可以创建一个列表并打印其字符串表示形式:

def collatz(n):

    my_list = []

    my_list.append(n)

    while (n > 1):
        if n % 2 == 0:
            n=int(n**0.5)
        else:
            n=int(n**1.5)
        if n == 1:
            my_list.append(1)
        else:
            my_list.append(n)

    print(str(my_list))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM