Python 3.0 - is this true?

Duncan Grisby duncan-news at grisby.org
Wed Nov 19 12:33:32 CET 2008


In article <4919E931.5030704 at v.loewis.de>,
Martin v. Löwis <martin at v.loewis.de> wrote:

>> The sorting is in a performance-critical part of the system, so the
>> overhead of evaluating a key function is not insignificant.
>
>Can you easily produce an example? It doesn't have to be real data,
>but should have the structure (typewise) of the real data. I would
>like to perform some measurements. For example, I could imagine that
>
>l = []
>for i in range(1000):
>  x = random.randint(0,100)
>  if x < 4: l.append(None)
>  else: l.append(x)
>
>might adequately model your problem.

Sorry for the delay in replying. Yes, that's not far off. Most of the
time the lists contain strings, though. A better approximation might
be to read lines from a file and randomly replace them with Nones:

l = []
for line in open("bigfile.txt"):
    x = random.randint(0,100)
    if x < 4: l.append(None)
    else: l.append(line)

And maybe once in a while you end up with something not dissimilar to:

l = []
for line in open("bigfile.txt"):
    x = random.randint(0,100)
    if x < 4: l.append(None)
    elif x < 5: l.append([line,line])
    else: l.append(line)

In that kind of case it doesn't really matter what happens to list
items in the sort order, but it's important it doesn't fail to sort
the ones that are strings.

Cheers,

Duncan.

-- 
 -- Duncan Grisby         --
  -- duncan at grisby.org     --
   -- http://www.grisby.org --



More information about the Python-list mailing list