Home > Net >  Formatting tuples in al list
Formatting tuples in al list

Time:01-13

I used SQLite to get a query from a database. The intention is to rank the most downloaded albums in a top 10. This I have succeeded to do. 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      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

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

Screenshot Code

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)

Screenshot Output

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] 

CodePudding user response:

You need to print row by row, not column by column. Also, you can use enumerate to get a running index like so:

for i, infos in enumerate(cur.fetchall(), start=1):
    print(i, *infos, sep="\t")

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

CodePudding user response:

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))
  • Related