如何使输出更具可读性?

How to make output more readable?

我写了下面的代码,它打印出所需数量的素数。

def prime_number(number1):
    for desired_num in range(1,int(number1)+1):
       if desired_num > 1:
          for i in range(2,desired_num):
              if (desired_num % i) == 0:
              break
          else:
              print(desired_num)
number2 = int(raw_input ("Enter any number :- "))
print "Prime numbers between 1 and", number2, "are :-"
prime_number(number2)    

我得到的输出类似于下面的输出,这对于包含很多数字的列表来说变得非常麻烦。

2   
3  
5  
7  

我想要的输出类似于下面的输出。

[2, 3, 5, 7]   

对于如何获得所需的输出,您有什么建议吗?

使用append到一个新列表:

import time
def prime_number(number1):
    l=[]
    for desired_num in range(1,int(number1)+1):
       if desired_num > 1:
          for i in range(2,desired_num):
              if (desired_num % i) == 0:
              break
          else:
              l.append(desired_num)
    print(l)
number2 = int(raw_input ("Enter any number :- "))
print "Prime numbers between 1 and", number2, "are :-"
start = time.time()
prime_number(number2)
print(time.time() - start)

一个简单的想法是将素数列表存储在 python 内置列表中,然后以您喜欢的格式简单地打印出来!就这么简单。

def prime_number(number1):
    primes = [] # An empty list here, that will be populated with primes!
    for desired_num in range(1,int(number1)+1):
       if desired_num > 1:
          for i in range(2,desired_num):
              if (desired_num % i) == 0:
                  break
          else:
              primes.append(desired_num)
    print "[" + ",".join(map(str, primes)) + "]" 

number2 = int(raw_input ("Enter any number :- "))
print "Prime numbers between 1 and", number2, "are :-"
primes = prime_number(number2)

打印有两种方式。将其存储在列表中然后打印或连续打印。

方法 1 Use list and then print it as comma separated:

import time
def prime_number(number1):
    output_list = list()
    for desired_num in range(1,int(number1)+1):
        if desired_num > 1:
           for i in range(2,desired_num):
               if (desired_num % i) == 0:
                   break
           else:
               output_list.append(desired_num)
    return output_list
number2 = int(raw_input ("Enter any number :- "))
print "Prime numbers between 1 and", number2, "are :-"
output_list = prime_number(number2)
print (",".join(output_list))

方法 2 Print the numbers in function itself serially using end:

import time
def prime_number(number1):
    for desired_num in range(1,int(number1)+1):
        if desired_num > 1:
           for i in range(2,desired_num):
               if (desired_num % i) == 0:
                   break
           else:
               print(desired_num,end = " ")
  number2 = int(raw_input ("Enter any number :- "))
  print "Prime numbers between 1 and", number2, "are :-"
  prime_number(number2)    

如果用数组存储这些素数,然后打印出来会更好。

import time

def prime_number(number1):
    primes = []
    for desired_num in range(1,int(number1)+1):
       if desired_num > 1:
          for i in range(2,desired_num):
              if (desired_num % i) == 0:
                  break
          else:
              primes.append(desire_num)
    return primes

number2 = int(raw_input ("Enter any number :- "))
print "Prime numbers between 1 and", number2, "are :-"
print(prime_number(number2)) 

为了实现您的目标(格式化输出),您必须对代码进行一些更改,换句话说,使其成为 "more pythonic":

import time
from pprint import pprint, pformat
from typing import Iterable


def prime_number(number1):
    """
    generator
    :rtype: Iterable[int]
    :type number1: int
    """
    for desired_num in range(1, int(number1) + 1):
        if desired_num > 1:
            for i in range(2, desired_num):
                if (desired_num % i) == 0:
                    break
            else:
                yield desired_num


number2 = int(raw_input("Enter any number :- "))
primes = pformat(list(prime_number(number2)))
print("Prime numbers between 1 and {} are :- {}".format(number2, primes))

我这里用的是 pipe: pformat(list(prime_number(...))) generator -> list [1, 2, 3] -> text '[1, 3, ]' . 然后我将文本和 number2 放入 格式化的 字符串,并发送给打印。

如果您是 python 的初学者并且不熟悉 generators 概念,您可以重写 prime_number 函数以使用简单的 .append 到列出而不是产生它,然后 return 整个列表。