#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Exampe use of the bisect module. """ #end_pymotw_header import bisect import random # Use a constant see to ensure that we see # the same pseudo-random numbers each time # we run the loop. random.seed(1) # Generate 20 random numbers and # insert them into a list in sorted # order. l = [] for i in range(1, 20): r = random.randint(1, 100) position = bisect.bisect(l, r) bisect.insort(l, r) print '%2d %2d' % (r, position), l