[英]Formatting tuples in al list
I used SQLite to get a query from a database.我使用 SQLite 从数据库中获取查询。 The intention is to rank the most downloaded albums in a top 10. This I have succeeded to do.
目的是将下载次数最多的专辑排在前 10 名。我已经成功做到了这一点。 The result gives me list of tuples.
结果给了我元组列表。 Each tuple is composed of Artist name, Album title and Number of downloads.
每个元组由艺术家姓名、专辑名称和下载次数组成。 I want to format this tuple with a ranking from 1 to 10 and tabs in between, like this:
我想用从 1 到 10 的排名和介于两者之间的制表符来格式化这个元组,如下所示:
1 Artist name Album title Number of downloads
I just can't seem to figure out how tot change the formatting.我似乎无法弄清楚如何更改格式。 I keep comming back to the standard tuple format (Artist name, Album title, Number of downloads).
我一直回到标准的元组格式(艺术家姓名、专辑名称、下载次数)。 I can use the zip function to add the ranking from 1 tot 10, but the tuple stays as is
我可以使用 zip function 将排名从 1 添加到 10,但元组保持原样
import sqlite3
db = sqlite3.connect("C:\\Users\marlo\Downloads\programs\chinook.db")
cur = db.cursor()
cur.execute("""SELECT artists.name, Title, COUNT(*) as count
FROM invoice_items
JOIN tracks ON invoice_items.TrackId = tracks.TrackId
JOIN albums ON tracks.AlbumId = albums.AlbumId
JOIN artists ON albums.ArtistId = artists.ArtistID
GROUP BY Title
ORDER BY count DESC
LIMIT 10""")
Album_ranking = cur.fetchall()
print(*Album_ranking, sep = "\n")
Output: Output:
('Chico Buarque', 'Minha Historia', 27)
('Lenny Kravitz', 'Greatest Hits', 26)
('Eric Clapton', 'Unplugged', 25)
('Titãs', 'Acústico', 22)
('Kiss', 'Greatest Kiss', 20)
('Caetano Veloso', 'Prenda Minha', 19)
('The Who', 'My Generation - The Very Best Of The Who', 19)
('Creedence Clearwater Revival', 'Chronicle, Vol. 2', 19)
('Green Day', 'International Superhits', 18)
('Creedence Clearwater Revival', 'Chronicle, Vol. 1', 18)
I'm a beginner so I feel like everything I tried thusfar is hardly worth mentioning.我是初学者,所以我觉得到目前为止我尝试过的一切都不值得一提。 I tried to seperate all the elements by using List comprehension, but that just puts all the elements underneath eacht other like this:
我试图通过使用列表理解来分离所有元素,但这只是将所有元素放在彼此之下,如下所示:
# formatting
Rank = [*range(1, 11, 1)]
Artist = [ranking[0] for ranking in Album_ranking]
Album = [ranking[1] for ranking in Album_ranking]
Frequency = [ranking[2] for ranking in Album_ranking]
I am not sure if i understood correct but maybe sorted is rigth here:我不确定我是否理解正确但也许排序在这里是正确的:
albums = [('Chico Buarque', 'Minha Historia', 27),
('Lenny Kravitz', 'Greatest Hits', 26),
('Eric Clapton', 'Unplugged', 25),
('Titãs', 'Acústico', 22),
('Kiss', 'Greatest Kiss', 20),
('Caetano Veloso', 'Prenda Minha', 19),
('The Who', 'My Generation - The Very Best Of The Who', 19),
('Creedence Clearwater Revival', 'Chronicle, Vol. 2', 19),
('Green Day', 'International Superhits', 18),
('Creedence Clearwater Revival', 'Chronicle, Vol. 1', 18)
def func(element):
return element[2]
print(sorted(albums, key=func, reverse=True))
You need to print row by row, not column by column.您需要逐行打印,而不是逐列打印。 Also, you can use
enumerate
to get a running index like so:此外,您可以使用
enumerate
来获取运行索引,如下所示:
for i, infos in enumerate(cur.fetchall(), start=1):
print(i, *infos, sep="\t")
Output: Output:
1 Chico Buarque Minha Historia 27
2 Lenny Kravitz Greatest Hits 26
3 Eric Clapton Unplugged 25
4 Titãs Acústico 22
5 Kiss Greatest Kiss 20
6 Caetano Veloso Prenda Minha 19
7 The Who My Generation - The Very Best Of The Who 19
8 Creedence Clearwater Revival Chronicle, Vol. 2 19
9 Green Day International Superhits 18
10 Creedence Clearwater Revival Chronicle, Vol. 1 18
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.