User:Statcompute/BlogEntry: 2007 April 22 01:29:05 EDT

From sasCommunity

Jump to: navigation, search
##################################################
# KEEP THE 1ST RECORD IN LIST BY THE SORT INDEX  #
##################################################

from random import *

List = []
seed(1)
for i in xrange(10):
  List.append([randint(1, 3), 'ROW' + str(i)])

print "ORIGINAL LIST"
print "+" + 15 * "-" + "+"
for row in List:
  print "|" + str(row[0]).center(4) + "|" + row[1].center(10) + "|"
print "+" + 15 * "-" + "+"
"""
ORIGINAL LIST
+---------------+
| 1  |   ROW0   |
| 3  |   ROW1   |
| 3  |   ROW2   |
| 1  |   ROW3   |
| 2  |   ROW4   |
| 2  |   ROW5   |
| 2  |   ROW6   |
| 3  |   ROW7   |
| 1  |   ROW8   |
| 1  |   ROW9   |
+---------------+
"""

SortList = []
for i in xrange(len(List)):
  SortList.append([List[i][0], i, List[i]])
SortList.sort()

KeepList = []
for i in set(x[0] for x in SortList):
  index = [i, min(x[1] for x in SortList if x[0] == i)]
  KeepList.extend(row[2] for row in SortList if row[:2] == index)

print "RECORDS LEFT"
print "+" + 15 * "-" + "+"
for row in KeepList:
  print "|" + str(row[0]).center(4) + "|" + row[1].center(10) + "|"
print "+" + 15 * "-" + "+"
"""
RECORDS LEFT
+---------------+
| 1  |   ROW0   |
| 2  |   ROW4   |
| 3  |   ROW1   |
+---------------+
"""
Personal tools