用Python實現檔案亂序(轉)

post0發表於2007-08-10
用Python實現檔案亂序(轉)[@more@]

# cat test.dat

1 --- I

2 --- L

3 --- O

4 --- V

5 --- E

6 --- Y

7 --- O

8 --- U

9 --- !

10 --- !

# cat r.py

import sys,random

lines = sys.stdin.readlines()

olines=[]

while lines:

olines.append(lines.pop(random.randrange(len(lines))))

sys.stdout.write( "".join(olines))

# cat test.dat | python r.py

6 --- Y

4 --- V

8 --- U

5 --- E

3 --- O

2 --- L

1 --- I

10 --- !

7 --- O

9 ---

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8225414/viewspace-940634/,如需轉載,請註明出處,否則將追究法律責任。

相關文章