无法打印出来以便它们对齐

时间:2017-10-07 15:25:44

标签: python python-3.x

我正在尝试打印出我所拥有的结果列表,但我希望它们能够对齐。他们目前看起来像:

       table        
word:          frequency:
i                   9
my                   2
to                   2
test                   2
it                   2
hate                   1
stupid                   1
accounting                   1
class                   1
because                   1
its                   1
from                   1
six                   1
seven                   1
pm                   1
how                   1
is                   1
this                   1
helping                   1
becuase                   1
im                   1
going                   1
do                   1
a                   1
little                   1
on                   1
freind                   1
ii                   1

我希望频率彼此对齐,因此它们不会以这种奇怪的锯齿形式出现。我试着在格式中添加东西,但它没有用。这就是我的代码:

import string
from collections import OrderedDict


f=open('mariah.txt','r')
a=f.read()  # read the text file like it would normal look ie no \n or anything
# print(a)
c=a.lower()  # convert everything in the textfile to lowercase
# print(c)
y=c.translate(str.maketrans('','',string.punctuation))  # get rid of any punctuation
# print(y)
words_in_novel=y.split()  # splitting every word apart. the default for split is on white-space characters. Why when i split like " " for the spacing is it then giving me \n?

#print(words_in_novel)

count={}

for word in words_in_novel:
    #print(word)
    if word in count:  # if the word from the word_in_novel is already in count then add a one to that counter
        count[word]+=1
    else:
        count[word]=1  # if the word is the first time in count set it to 1

print(count)
print("\n\n\n\n\n\n\n")
# this orderes the dictionary where its sorts them by the second term wehre t[1] refers to the term after the colon
# reverse so we are sorting from greatest to least values
g=(sorted(count.items(), key=lambda t: t[1], reverse=True))
# g=OrderedDict(sorted(count.items(), key=lambda t: t[1]))
print(g)
print("\n\n\n\n\n\n\n")
print("{:^20}".format("table"))
print("{}{:>20}".format("word:","frequency:"))
for i in g:
    # z=g[i]
    # print(i)
    # a=len(i[0])
    # print(a)
    # c=50+a
    # print(c)
    print("{}{:>20}".format(i[0],i[1]))

有谁知道如何让它们直线前进?

4 个答案:

答案 0 :(得分:2)

您需要调整第1列的宽度/宽度,而不是第2列 正确的方法:

...
print("{:<20}{}".format("word:","frequency:"))
for i in g:
    print("{:<20}{}".format(i[0],i[1]))

输出看起来像:

word:               frequency:
i                   9
my                  2
...
accounting          2
class               1
because             1
...

答案 1 :(得分:0)

好的,对于你的代码部分:

for i in g:
    r = " "*25
    #print("{}{:>20}".format(i[0],i[1]))
    r[:len(i[0])] = i[0]
    r = r[:22]+str(i[1])
    print(r)

它应该有用

答案 2 :(得分:0)

如果您发现频率大于一位数,您可以尝试这样的事情:

max_len = max(len(i[0]) for i in g)
format_str = "{{:<{}}}{{:>{}}}".format(max_len, 20 - max_len)
for i in g:
    print(format_str.format(i[0], i[1]))

答案 3 :(得分:0)

对齐词语

print("{:<10}{:>10}".format(i[0],i[1]))