gpt4 book ai didi

python - 将无聊的事情自动化第 6 章几乎完成的表打印机

转载 作者:太空狗 更新时间:2023-10-30 01:39:01 26 4
gpt4 key购买 nike

在这个部分,他们希望我们创建这个表:

    apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose

必须右对齐,输入为tableData。这是我的代码:

tableData=[['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
listlens=[]
tour=0
lists={}
for m in tableData:
total=0
tour+=1
for n in m:
total+=len(n)
lists["list:",tour]=total
print("list",tour,total)

itemcount=list(lists.values())
sortedlen=(sorted(itemcount,reverse=True))
longest=sortedlen[0]

#print (lists['list:', 1])
#print (longest)


for m in range(len(tableData[0])):
for n in range(len(tableData)):
print (tableData[n][m],end=" ")
n+=1
print ("".rjust(lists['list:', 1],"-"))
m+=1

除了一件事,我几乎完成了,我不能让它右对齐。这个输出是我迄今为止最接近的输出。

apples Alice dogs ---------------------------
oranges Bob cats ---------------------------
cherries Carol moose ---------------------------
banana David goose ---------------------------

如果我将 rjust 放在内部 for 循环中,输出会大不相同:

apples-------------------------- Alice-------------------------- dogs-------------------------- 
oranges-------------------------- Bob-------------------------- cats--------------------------
cherries-------------------------- Carol-------------------------- moose--------------------------
banana-------------------------- David-------------------------- goose--------------------------

最佳答案

这里有一个替代方法,也许您可​​以将其应用到您自己的代码中。我首先使用 tableData 并将其整理到字典中,以便更容易使用。之后我找到了最长的字符列表。这让我们知道较短的列表应该走多远。最后,我打印出每个列表,根据与最长列表的不同,在较短列表前添加空格。

# orginal data
tableData=[['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]

# empty dictonary for sorting the data
newTable = {0:[], 1:[], 2:[], 3:[]}

# iterate through each list in tableData
for li in tableData:
for i in range(len(li)):
# put each item of tableData into newTable by index
newTable[i].append(li[i])

# determine the longest list by number of total characters
# for instance ['apples', 'Alice', 'dogs'] would be 15 characters
# we will start with longest being zero at the start
longest = 0
# iterate through newTable
# for example the first key:value will be 0:['apples', 'Alice', 'dogs']
# we only really care about the value (the list) in this case
for key, value in newTable.items():
# determine the total characters in each list
# so effectively len('applesAlicedogs') for the first list
length = len(''.join(value))
# if the length is the longest length so far,
# make that equal longest
if length > longest:
longest = length

# we will loop through the newTable one last time
# printing spaces infront of each list equal to the difference
# between the length of the longest list and length of the current list
# this way it's all nice and tidy to the right
for key, value in newTable.items():
print(' ' * (longest - len(''.join(value))) + ' '.join(value))

关于python - 将无聊的事情自动化第 6 章几乎完成的表打印机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34488115/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com