Python 3.0 - is this true?

Terry Reedy tjreedy at udel.edu
Wed Nov 19 17:49:00 CET 2008


Duncan Grisby wrote:


> 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)

So use '' or '\0' instead of None for null lines.  Or replace None for 
the sort.

> 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.

Replace the sublists with a coded string, such as '\0'+line.
If sorting is time (and space) critical, you want a straight string sort 
without key *or* cmp function.

tjr




More information about the Python-list mailing list