Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 打印内容以便对齐时遇到问题_Python_Python 3.x - Fatal编程技术网

Python 打印内容以便对齐时遇到问题

Python 打印内容以便对齐时遇到问题,python,python-3.x,Python,Python 3.x,我正试图打印出我的结果列表,但我希望它们是一致的。它们目前看起来像: table word: frequency: i 9 my 2 to 2 test 2 it 2 hate 1 stupid 1

我正试图打印出我的结果列表,但我希望它们是一致的。它们目前看起来像:

       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]))

有人知道如何让他们走直线吗

您需要调整第一列的宽度/对齐,而不是第二列。
正确的方法:

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

您需要调整第一列的宽度/对齐,而不是第二列。
正确的方法:

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

好的,对于代码部分:

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)

它应该可以工作

好的,对于代码部分:

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)

如果您发现频率大于一个位数,您可以尝试以下方法:

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]))
max\u len=max(len(i[0])表示i in g)
format_str=“{{:{}}}”。format(max_len,20-max_len)
对于i in g:
打印(格式\u str.format(i[0],i[1]))

如果您发现频率大于一位数,您可以尝试以下方法:

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]))
max\u len=max(len(i[0])表示i in g)
format_str=“{{:{}}}”。format(max_len,20-max_len)
对于i in g:
打印(格式\u str.format(i[0],i[1]))
将单词也对齐

print("{:<10}{:>10}".format(i[0],i[1]))
print(“{:10}”。格式(i[0],i[1]))
将单词也对齐

print("{:<10}{:>10}".format(i[0],i[1]))
print(“{:10}”。格式(i[0],i[1]))

您需要单词列具有固定宽度您需要单词列具有固定宽度哇,谢谢!为什么在第一个元素上这样做会起作用?@Brit,不客气。specificator
{:它是从行的开头而不是单词的结尾扩展的吗?哇,谢谢!当你在第一个元素上这样做时,它为什么起作用?@Brit,不客气。specificator
{:它是从行的开头而不是单词的结尾扩展的吗?