Mine værktøjer
Du er her: Forside diverse filer til download mm. arraysort.py
Handlinger tilknyttet webside

arraysort.py

af adminSidst opdateret 09/07 2007 01:00

http://www.eksperten.dk/spm/786765

Klik her for at hente fil

Størrelse: 1.3 kB - Filtype text/python-source

Indhold af fil-indholdsobjekt

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""Sort a 2 dimensional array"""
import random
from pprint import pprint

def printTabel( Tabel, Fra , Til, Markeret ):


    Mark = { True : '<td bgcolor="FFFFCC">', False:'<td>' }

    print '<table border="1" cellspacing="0">'
    for raekke in Tabel[ Fra: Til ]:
        print "<tr>"
        for colData in raekke:
            print Mark.get( Markeret == colData ), colData, '</td>'
        print "</tr>"
    print "</table>"

def DoubleSort( Tabel, column, Reverse=False ):
    def colCompare( x, y ):
        return cmp( y[ column ], x[ column ] )
    Tabel.sort( cmp=colCompare )

if __name__ == "__main__":
    print "Content-type: text/html\n"
    print "<html>"
    print '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'
    print 'Se og hent kildekoden <a href="http://www.databassen.dk:8090/bauerdata/python-program-eksempler/arraysort-source">her</a>'
    print "<pre># Opret en dummy to dimensionel array med tilfældige tal."

    Rnd = range( -50, 50 )
    ToDim = [ [ y for y in [ random.choice( Rnd ) for x in  range(5) ] ] for z in range(10) ]

    print "Før sortering"
    pprint( ToDim )
    
    # Sorter tabellen
    DoubleSort( ToDim, 3 , Reverse=True )
    print "Efter sortering"
    pprint( ToDim )
    print "</pre>"
    printTabel( ToDim, 0, 3, 40 )
    print "</html>"
« august 2018 »
søn man tir ons tor fre lør
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Chat Rooms
Dyrk motion

Trim ballerup

 

Powered by Plone, the Open Source Content Management System