|
|
|
|
 用python来打乱排序后的文件 - sunnyflurry [ 2005-10-27 18:40 | 158 byte(s)]
 Re: 用python来打乱排序后的文件 - flingfly [ 2005-10-28 14:45 | 46 byte(s)]
 Re: 用python来打乱排序后的文件 - flingfly [ 2005-10-27 20:41 | 1,340 byte(s)]
 Re: 用python来打乱排序后的文件 - panhudie [ 2005-10-28 01:08 | 209 byte(s)]
 Re: 用python来打乱排序后的文件 - passworld [ 2005-10-28 13:03 | 91 byte(s)]
 Re: 用python来打乱排序后的文件 - flingfly [ 2005-10-28 14:55 | 1,102 byte(s)]
 Re: 用python来打乱排序后的文件 - passworld [ 2005-10-28 22:39 | 236 byte(s)]
 Re: 用python来打乱排序后的文件 - flingfly [ 2005-10-29 10:02 | 1,515 byte(s)]
 Re: 用python来打乱排序后的文件 - passworld [ 2005-10-29 11:40 | 54 byte(s)]
 Re: 用python来打乱排序后的文件 - flingfly [ 2005-10-29 11:46 | 53 byte(s)]
 Re: 用python来打乱排序后的文件 - passworld [ 2005-10-29 12:43 | 93 byte(s)]
 Re: 用python来打乱排序后的文件 - sunnyflurry [ 2005-10-28 15:17 | 1,195 byte(s)]
 Re: 用python来打乱排序后的文件 - flingfly [ 2005-10-28 18:03 | 1,993 byte(s)]
 Re: 用python来打乱排序后的文件 - sunnyflurry [ 2005-10-28 18:37 | 66 byte(s)]
 Re: 用python来打乱排序后的文件 - flingfly [ 2005-10-28 20:06 | 26 byte(s)]
 Re: 用python来打乱排序后的文件 - sunnyflurry [ 2005-10-28 09:17 | 16 byte(s)]
|
|
|
|
[Original]
[Print]
[Top]
|
我有一个文本文件,里面记录的是排序后的手机号,一行一个
现在要打乱这个排序,不知道用python实现起来是不是很容易?
新学python,大家给个思路:)
|
|
----
keep it simple & stupid
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
不知道怎么描述,就自己写了个程序,你可以参考。
大概:
(1)读取原文件到list
(2)取list中随机位置元素写入文件。
#coding:gb2312
import sys
import random
def usage():
print "usage:program srcfilename dstfilename"
global filename
filename = ""
try:
filename = sys.argv[1]
except:
usage()
raise()
#open the phonebook file
f = open(filename, 'r')
phonebook = [];
#read all number from file
for line in f.readlines( ):
phonebook.append(line)
f.close()
#write to file randomly
try:
filename = sys.argv[2]
except:
usage()
raise()
f = open(filename, 'w')
cnt = len(phonebook)
while (cnt > 0):
try:
pos = random.randrange(0, cnt - 1)
except: #if the cnt = 1, then raise the error
pos = 0
print 'pos' , pos
f.write(phonebook[pos])
del phonebook[pos]
cnt = len(phonebook)
f.close()
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
file = 'file.txt'
newfile = 'newfile.txt'
open(newfile, 'w').writelines (list (set (open(file).readlines())))
可以用set来hash里手机号码, 因为hash可以把顺序打乱了.
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
使用set每次生成的文件内容一致。
使用shuffle,每次内容都不一样。
一个问题请问:python里面文件不close没有问题吗?
呵呵,俺是个写C的。
根据上面两位的建议,我讲程序改如下:
#coding:gb2312
import sys
import random
def usage():
print "usage:program srcfilename dstfilename"
global filename
filename = ""
try:
filename = sys.argv[1]
except:
usage()
raise()
#open the phonebook file
f = open(filename, 'r')
phonebook = f.readlines()
print phonebook
f.close()
#write to file randomly
try:
filename = sys.argv[2]
except:
usage()
raise()
f = open(filename, 'w')
random.shuffle(phonebook)
f.writelines(phonebook)
f.close()
|
|
|
----
(1)多回答问题。 (2)多提出问题。
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
我用radom方法以后,执行了一上午,程序没执行完,好郁闷,因为文件很大,一共1000万条记录,刚刚取消了,想其他办法,同事说用sorted方法按后4位排序输出也可以
可是我看了sort的说明,半天没明白哪些参数的意义
The sort() method takes optional arguments for controlling the comparisons.
cmp specifies a custom comparison function of two arguments (list items) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: "cmp=lambda x,y: cmp(x.lower(), y.lower())"
key specifies a function of one argument that is used to extract a comparison key from each list element: "key=str.lower"
reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.
In general, the key and reverse conversion processes are much faster than specifying an equivalent cmp function. This is because cmp is called multiple times for each list element while key and reverse touch each element only once.
哪位能举个例子说明下cmp参数怎么设置?
|
|
|
----
keep it simple & stupid
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
好像不要那么久啊??
我的机器:
XP, intel 2.8G , 1G memory
测试结果:
好像只要30秒左右
//生成测试文件代码 b.y,执行时间:大概30秒
import os
import sys
import time
now = time.time()
t = time.localtime(now)
print "begin time:",t[0], '-', t[1], '-', t[2], " ", t[3],':', t[4], " " ,t[5]
filename = sys.argv[1]
f = open(filename, "w")
for i in range(0, 10000000):
f.write("%11d
" % i)
now = time.time()
t = time.localtime(now)
print "begin time:",t[0], '-', t[1], '-', t[2], " ", t[3],':', t[4], " ", t[5]
//打乱排序程序执行时间:大概30秒
#coding:gb2312
import sys
import random
import time
def usage():
print "usage:program srcfilename dstfilename"
global filename
filename = ""
try:
filename = sys.argv[1]
except:
usage()
raise()
#open the phonebook file
now = time.time()
t = time.localtime(now)
print "begin time:",t[0], '-', t[1], '-', t[2], " ", t[3],':', t[4], " " ,t[5]
f = open(filename, 'r')
phonebook = f.readlines()
f.close()
#write to file randomly
try:
filename = sys.argv[2]
except:
usage()
raise()
f = open(filename, 'w')
random.shuffle(phonebook)
f.writelines(phonebook)
f.close()
now = time.time()
t = time.localtime(now)
print "begin time:",t[0], '-', t[1], '-', t[2], " ", t[3],':', t[4], " ", t[5]
|
|
|
----
(1)多回答问题。 (2)多提出问题。
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
python 里面文件是一个 object ,在收垃圾的时候会自动关闭文件的,所以理论上不用关,但是因为什么时候收垃圾我们是不知道的,所以有需要你可以自己关上,不管怎么说,跟C程序一样,程序执行完后,所有打开的文件系统会自动关闭的。
|
|
----
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
我明白你的意思的。这么说的话,程序运行结束的时候,文件是还没有关闭的,python关闭的时候去关闭这个文件的。
不过这样就带来一个问题,python关闭的时候会不会丢失数据啊。
在C程序里,如果你没有关闭一个文件,程序关闭的时候,os也会帮助你 关闭这个文件的,可是还是丢失了数据。
不过看了python的源代码,我得到了答案:
在os中,操作系统关闭文件的时候,可能不保证把缓冲的数据写入文件。
而在python中,它是保证调用C接口关闭文件的。
源代码如下:
static void
file_dealloc(PyFileObject *f)//如果没有主动关闭文件,当python结束的时候会调用此函数
{
int sts = 0;
if (f->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) f);
if (f->f_fp != NULL && f->f_close != NULL) {
Py_BEGIN_ALLOW_THREADS
sts = (*f->f_close)(f->f_fp); //关闭文件
。。。。。。。。。。。
}
static PyObject *
file_close(PyFileObject *f)//如果我们在python中主动关闭的话,会调用此函数
{
int sts = 0;
if (f->f_fp != NULL) {
if (f->f_close != NULL) {
Py_BEGIN_ALLOW_THREADS
errno = 0;
sts = (*f->f_close)(f->f_fp); //关闭文件
。。。。。。。。。。。。。。。
}
|
|
|
----
(1)多回答问题。 (2)多提出问题。
|
|
[Original]
[Print]
[Top]
|
|
|