[英]How to create a table using a list of lists
我正在嘗試編寫一個有 2 行的文件,第一行是數字,第二行是字母。 例如,我試圖用字母表來做到這一點。
list1=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
list2=list1+list1
abcList = [[],[]]
for i in range(len(list2)):
i+=1
if i % 5 == 0:
if i>=10:
abcList[0].append(str(i) + ' ')
else:
abcList[0].append(str(i) + ' ')
elif i<=1:
abcList[0].append(str(i) + ' ')
else:
abcList[0].append(' ')
for i,v in enumerate(list2):
i+=1
if i > 10:
abcList[1].append(' '+v+' ')
else:
abcList[1].append(v+' ')
print(''.join(abcList[0]))
print(''.join(abcList[1]))
with open('file.txt','w') as file:
file.write(''.join(abcList[0]))
file.write('\n')
file.write(''.join(abcList[1]))
上述設置的問題是它非常“hacky”(我不知道它是否正確)。 它“有效”,但實際上只是修改了 2 個列表以確保它們正確堆疊在一起。 問題是如果您的列表變得太長,那么文本會環繞,並堆疊在自身而不是數字上。 我正在尋找適用於任何大小列表的不那么“hacky”的東西(嘗試在沒有外部庫的情況下執行此操作,所以我不想使用 pandas 或 numpy)。
編輯: output 將是:
1 5 10
A B C D E F G H I J...etc.
編輯 2: 只是想補充一下,到目前為止,我已經做到了這一點,但我只能制作列,而不是行。
list1=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
list2=list1*2
abcList = [[],[]]
for i in range(len(list2)):
i+=1
if i % 5 == 0:
if i>=5:
abcList[0].append(str(i))
elif i<=1:
abcList[0].append(str(i))
else:
abcList[0].append('')
for i,letter in enumerate(list2):
abcList[1].append(letter)
for number, letters in zip(*abcList):
print(number.ljust(5), letters)
但是,這不再有包裝問題,並且數字與字母完美對齊。 現在唯一要做的就是讓它們從列到行。
上面的 Output 是:
1 A
B
C
D
5 E
F
G
H
I
10 J
我的意思是,你可以這樣做:
file_contents = """...""" # The file contents. I not the best at file manipulation
def parser(document): # This function will return a nested list
temp = str(document).split('\n')
return [[line] for line in temp] # List comprehension
parsed = parser(file_contents)
# And then do what you want with that
您預期的 output 有點不一致,因為在第一個中,您有 1、6、11、16 1, 6, 11, 16...
而在第二個中:1、5、10、15 1, 5, 10, 15....
所以我有幾個可能的解決方案:
print(''.join([' ' if n%5 else str(n+1).ljust(2) for n in range(len(list2))]))
print(''.join([c.ljust(2) for c in list2]))
Output:
1 6 11 16 21 26 31 36 41 46 51
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
print(''.join([' ' if n%5 else str(n).ljust(2) for n in range(len(list2))]))
print(''.join([c.ljust(2) for c in list2]))
Output:
0 5 10 15 20 25 30 35 40 45 50
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
print(''.join(['1 ']+[' ' if n%5 else str(n).ljust(2) for n in range(len(list2))][1:]))
print(''.join([c.ljust(2) for c in list2]))
Output:
1 5 10 15 20 25 30 35 40 45 50
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
如果您想保持可變寬度字符串對齊,您可以使用寬度等於 position 中單個項目寬度的最大值的字符串格式。 (順便說一下,這個例子適用於多於任何數量的列表。)
list1 = ["", "5", "", "10", "", "4"]
list2 = ["A", "B", "C", "D", "EE", "F"]
lists = [list1, list2]
widths = [max(map(len, t)) for t in zip(*lists)]
for lst in lists:
line = " ".join("{val:{width}s}".format(val=val, width=width)
for val, width in zip(lst, widths))
print(line)
給出:
5 10 4
A B C D EE F
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.