【问题标题】:Python Printing on the same LinePython 在同一行打印
【发布时间】:2015-01-20 05:42:17
【问题描述】:

我遇到的问题是将 phone_sorter() 和 number_calls() 都打印在同一行上。例如,它将打印 phone_sorter 的两行,但 number_calls 将打印在它的正下方。我已经尝试过 end='' 方法,但它似乎不起作用。

customers=open('customers.txt','r')
calls=open('calls.txt.','r')

def main():
    print("+--------------+------------------------------+---+---------+--------+")
    print("| Phone number | Name                         | # |Duration | Due    |")
    print("+--------------+------------------------------+---+---------+--------+")
    print(phone_sorter(), number_calls())

def time(x):
    m, s = divmod(seconds, x)
    h, m = divmod(m, x)
    return "%d:%02d:%02d" % (h, m, s)

def phone_sorter():
    sorted_no={}
    for line in customers:
        rows=line.split(";")
        sorted_no[rows[1]]=rows[0]
    for value in sorted(sorted_no.values()):
            for key in sorted_no.keys():
                if sorted_no[key] == value:
                    print(sorted_no[key],key)                            
def number_calls():
    no_calls={}
    for line in calls:
        rows=line.split(";")
        if rows[1] not in no_calls:
            no_calls[rows[1]]=1
        else:
            no_calls[rows[1]]+=1
    s={}
    s=sorted(no_calls.keys())
    for key in s:
        print(no_calls[key]) 
main()

【问题讨论】:

  • 在Python2.7中,只需要以逗号结尾即可。 print 'output',
  • 这是python3,我已经试过逗号的方法了。
  • @Kirill 你在哪里使用end=''
  • 如果print('output', end="") 没有这样做,我不知道该告诉你什么。
  • 在 phone_sorter 和 number_calls 的最后一次打印中,它不起作用,所以我将其删除。

标签: python printing formatting


【解决方案1】:

您的关键问题是phone_sorternumber_calls 都进行自己的打印,并返回None。因此,打印它们的返回值是荒谬的,并且应该在它们完成所有自己的单独行打印之后以毫无意义的 None None 行结束。

更好的方法是将它们重组为 return,而不是 print,它们确定的字符串,然后才将这些字符串排列为print “编排”main 函数。

看起来他们每个人都会返回一个 list 的字符串(他们现在正在单独的行上打印),如果它们按相应的顺序排列,您可能希望 zip 准备打印.

但是您的代码有些不透明,因此很难判断两者的顺序是否确实对应。他们最好是,如果最终的印刷是有意义的......

补充:让我举例说明phone_sorter的一些细微改进和一个大变化...:

def phone_sorter():
    sorted_no={}
    for line in customers:
        rows=line.split(";")
        sorted_no[rows[1]]=rows[0]
    sorted_keys = sorted(sorted_no, key=sorted_no.get)
    results = [(sorted_no[k], k) for k in sorted_keys]
    return results

明白了吗?除了更好地进行计算之外,核心思想是将 list 放在一起并将其返回——main 的工作是适当地格式化和打印它,与返回的类似列表一致number_calls(看起来是平行的)。

def number_calls():
    no_calls=collections.Counter(
        line.split(';')[1] for line in calls)
    return [no_calls(k) for k in sorted(no_calls)]

现在这两个列表之间的关系对我来说并不明显,但是,假设它们是平行的,main 可以做例如:

nc = no_calls()
ps = phone_sorter()
for (duration, name), numcalls in zip(ps, nc):
    print(...however you want to format the fields here...)

你在main打印的那些标题并没有告诉我每个下面应该打印什么数据,以及打印应该如何格式化(宽度为 例如每个字段)。但是,main,只有main,应该是 非常熟悉这些表示问题并控制它们,而其他功能则处理适当地提取数据的“业务逻辑”。 “关注点分离”——编程中的一个大问题!

【讨论】:

  • 是的,它们都应该打印在同一行,一行电话号码、姓名、#等。我试过 return 但它只打印一个函数实例而不是完整的字典。
  • @Kirill,返回一个列表,正如我所提到的;然后在main 中循环遍历这些列表(可能是zipped),边走边打印(如果全部都需要在一行上,则很可能使用end=' ')。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-20
相关资源
最近更新 更多