简体   繁体   中英

pretty print 2d matrix and call a sorting function at the same time

L = [['kevin', 8.5, 17.1, 5.9, 15.0, 18], ['arsene', 7.1, 4.4, 15.0, 5.6, 18], ['toufik', 1.1, 2.2, 13.4, 3.1, 20], ['lubin', 16.3, 14.8, 13.1, 5.6, 20], ['yannis', 18.8, 2.4, 12.0, 8.0, 18], ['aurelie', 3.6, 18.8, 8.2, 18.2, 18], ['luna', 14.6, 11.5, 15.2, 18.5, 19], ['sophie', 7.4, 2.1, 18.1, 2.9, 19], ['shadene', 17.9, 7.1, 16.7, 2.5, 19], ['anna', 9.7, 12.8, 10.6, 6.9, 20]]


def triNom(L):
'''sorts names alphabetically'''
n = len(L)
for i in range(n):
    for j in range (n - i - 1):
        if L[j] > L[j + 1]:
            L[j], L[j + 1] = L[j + 1], L[j]
return L

print('\n'.join(['\t'.join([str(cell) for cell in row]) for row in L]))

Output :

kevin   8.5     17.1    5.9     15.0    18
arsene  7.1     4.4     15.0    5.6     18
toufik  1.1     2.2     13.4    3.1     20
lubin   16.3    14.8    13.1    5.6     20
yannis  18.8    2.4     12.0    8.0     18
aurelie 3.6     18.8    8.2     18.2    18
luna    14.6    11.5    15.2    18.5    19
sophie  7.4     2.1     18.1    2.9     19
shadene 17.9    7.1     16.7    2.5     19
anna    9.7     12.8    10.6    6.9     20

How can I make a pretty print like this and call my function at the same time so that the output is pretty and sorted? It's my first time coding something like this I can't figure it out.

Your problem is with the sorting function, the pretty print is working correctly. Here is one way to do the first, without re-inventing the wheel, using native python functions.

First you need to convert L from being a 2D array into a dictionary of the following format.

L2 = {'kevin': [8.5, 17.1, 5.9, 15.0, 18], 'arsene': [7.1, 4.4, 15.0, 5.6, 18] }

This will make it easier to access the name which we are interested in and then we sort alphabetically by using sorted(list(L2)) .

To convert to a dictionary of the above format you can simply do

L2: dict = {}

for item in L: # In pseudo-code L2[name] = nums
    L2[item[0]] = [i for i in item[1:len(item)]] # item[0] is name and 1:len(item) the rest of the data (the numbers)  

Then we can short L2, by converting it to a list, and then looping throught the sorted list and recreating the first L list of lists in order now.

L = [] # Set this to empty because we are re-transfering the data
SORTED_L2 = sorted(list(L2)) # sort the list of L2 (only the names)
for name in SORTED_L2:
    numbers = L2[name] 
    L.append([name, *numbers]) # * is for unpacking

And then finally by calling print('\n'.join(['\t'.join([str(cell) for cell in row]) for row in L])) you can pretty print them. The output

anna    9.7     12.8    10.6    6.9     20
arsene  7.1     4.4     15.0    5.6     18
aurelie 3.6     18.8    8.2     18.2    18
kevin   8.5     17.1    5.9     15.0    18
lubin   16.3    14.8    13.1    5.6     20
luna    14.6    11.5    15.2    18.5    19
shadene 17.9    7.1     16.7    2.5     19
sophie  7.4     2.1     18.1    2.9     19
toufik  1.1     2.2     13.4    3.1     20
yannis  18.8    2.4     12.0    8.0     18

You can now wrap it all in one function like follows

def sortL(L):
    # Convert to dictionary for easy access
    L2: dict = {}
    for item in L:
        L2[item[0]] = [i for i in item[1:len(item)]]
    
    SORTED_L2 = sorted(list(L2)) # Sort the names
    L = [] 
    for item in SORTED_L2:
        L.append([item, *L2[item]])
    return L

def SortAndPrettyPrint(L):
    L = sortL(L)
    print('\n'.join(['\t'.join([str(cell) for cell in row]) for row in L]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM