Home > Software design >  Print nested list in a particular way
Print nested list in a particular way

Time:12-03

I have this list:

top_list = [['Recent news', '52', '15.1'], ['Godmorning', '5', '1.5'], ['Sports news', '47', '13.7'], ['Report with weather', '34', '9.9'], ['The angel and the lawless', '33', '9.6'], ['Thundercats', '3', '0.9'], ["Mother's legacy", '3', '0.9'], ['UR: Summer evenings with Europe of the Times', '3', '0.9'], ['Tip', '20', '5.8'], ['Florida Straits', '2', '0.6']]

It contains sublists with ['Program name', 'viewers', 'percentage']

I want to print it like this:

------------------ Top-10-list ----------------
1. Recent news, 52 viewers, (15.1%)
2. Godmorning,  5 viewers, (1.5%) 
3. Sports news, 47 viewers, (13.7%) 
4. Report with weather, 34 viewers, (9.9%) 
5. The angel and the lawless, 33 viewers, (9.6%)
6. Thundercats, 3 viewers, (0.9%)
7. Mother's legacy, 3 viewers, (0.9%) 
8. UR: Summer evenings with Europe of the Times 3 viewers, (0.9%)
9. Tip, 20 viewers, (5.8%) 
10. Florida Straits, 2 viewers, (0.6%)

I have tried this so far, but I don't know how to combat the problem. Is there a more efficient way/ some way to print out the list with programs?


index_top_ten = 1                                       #index for list (starts at 1)
    print('')
    print("------------------ Top-10-list ----------------")   #header
    line =''
    for sublist in top_list:
        line  = str(index_top_ten)  '.' ' '              #add index and dot

        for index in sublist
            
            if index[1]=='.' or index[2]=='.' or index[3]=='.':    #separate the percentage
                line  = '('   convert   '%'   ')'  ' '
    
            else:
                line  = str(index)   ' '
        index_top_ten  = 1    

This is other one is somewhat working but i don't know how to add the percentage icon:

[print(*x) for x in top_list][0] # one-line print

All help is much appreciated!

CodePudding user response:

To format the list of programs as shown in your example, you can use a for loop to iterate over the elements in the top_list array, and print each program's name, viewer count, and percentage in the desired format.

Here is an example of how you could implement this:

top_list = [['Recent news', '52', '15.1'], ['Godmorning', '5', '1.5'], ['Sports news', '47', '13.7'], ['Report with weather', '34', '9.9'], ['The angel and the lawless', '33', '9.6'], ['Thundercats', '3', '0.9'], ["Mother's legacy", '3', '0.9'], ['UR: Summer evenings with Europe of the Times', '3', '0.9'], ['Tip', '20', '5.8'], ['Florida Straits', '2', '0.6']]

# Print the header
print("------------------ Top-10-list ----------------")

# Iterate over the elements in the top_list array
for (i, program) in enumerate(top_list):
  # Extract the program name, viewer count, and percentage
  name = program[0]
  viewers = program[1]
  percentage = program[2]

  # Print the program's name, viewer count, and percentage in the desired format
  print(f"{i   1}. {name}, {viewers} viewers, ({percentage}%)")

This code will print the list of programs in the format shown in your example.

Note that the enumerate() function is used to iterate over the elements in the top_list array, and to obtain the index of each element in the array (which is used to print the program's ranking in the top-10 list). The f-strings feature is used to format the string that is printed for each program, which makes the code more concise and readable.

  • Related