URN Logo
UNIX Resources » Linux » China Linux Forum » Python 编 程 » 5 » 用python来打乱排序后的文件
announcement 声明: 本页内容为中国Linux论坛的内容镜像,文章的版权以及其他所有的相关权利属于中国Linux论坛和相应文章的作者,如果转载,请注明文章来源及相关版权信息。
Resources
China Linux Forum(finished)
Linux Forum(finished)
FreeBSD China(finished)
linuxforum.net
  业界新闻与评论
  自由软件杂谈
  IT 人生
  Linux软件快递
  翻译作坊
  Linux图书与评论
  GNU Emacs/XEmacs
  Linux 中文环境和中文化
  Linux桌面与办公软件
  Linux 多媒体与娱乐版
  自由之窗Mozilla
  笔记本电脑上的Linux
  Gentoo
  Debian 一族
  网络管理技术
  Linux 安装与入门
  WEB服务器和FTP服务器
  域名服务器和邮件服务器
  Linux防火墙和代理服务器应用
  文件及打印服务器
  技术培训与认证
  Linux内核技术
  Linux 嵌入技术
  Linux设备驱动程序
  Linux 集群技术
  LINUX平台数据库
  系统和网络安全
  CPU 与 编译器
  系统计算研究所专栏
  Linux下的GUI软件开发
  C/C++编程版
  PHP 技 术
  Java&jsp技术
  Shell编程技术
  Perl 编 程
  Python 编 程
  XML/Web Service 技术
  永远的Unix
  FreeBSD世界
   
用python来打乱排序后的文件
用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)]
 
Subject: 用python来打乱排序后的文件
Author: sunnyflurry    Posted: 2005-10-27 18:40    Length: 158 byte(s)
[Original] [Print] [Top]
我有一个文本文件,里面记录的是排序后的手机号,一行一个
现在要打乱这个排序,不知道用python实现起来是不是很容易?
新学python,大家给个思路:)
----
keep it simple & stupid
[Original] [Print] [Top]
Subject: Re: 用python来打乱排序后的文件
Author: flingfly    Posted: 2005-10-27 20:41    Length: 1,340 byte(s)
[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]
Subject: Re: 用python来打乱排序后的文件
Author: panhudie    Posted: 2005-10-28 01:08    Length: 209 byte(s)
[Original] [Print] [Top]
file = 'file.txt'
newfile = 'newfile.txt'

open(newfile, 'w').writelines (list (set (open(file).readlines())))

可以用set来hash里手机号码, 因为hash可以把顺序打乱了.
[Original] [Print] [Top]
Subject: Re: 用python来打乱排序后的文件
Author: sunnyflurry    Posted: 2005-10-28 09:17    Length: 16 byte(s)
[Original] [Print] [Top]
多谢二位指点:)
----
keep it simple & stupid
[Original] [Print] [Top]
Subject: Re: 用python来打乱排序后的文件
Author: passworld    Posted: 2005-10-28 13:03    Length: 91 byte(s)
[Original] [Print] [Top]
hash 其实是有序的可以重复的,random 里有一个 shuffle 可以直接做这件事。

----
[Original] [Print] [Top]
Subject: Re: 用python来打乱排序后的文件
Author: flingfly    Posted: 2005-10-28 14:45    Length: 46 byte(s)
[Original] [Print] [Top]
谢谢,我也知道怎么简洁的做这件事了,谢谢大家!
----
(1)多回答问题。
(2)多提出问题。
[Original] [Print] [Top]
Subject: Re: 用python来打乱排序后的文件
Author: flingfly    Posted: 2005-10-28 14:55    Length: 1,102 byte(s)
[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]
Subject: Re: 用python来打乱排序后的文件
Author: sunnyflurry    Posted: 2005-10-28 15:17    Length: 1,195 byte(s)
[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]
Subject: Re: 用python来打乱排序后的文件
Author: flingfly    Posted: 2005-10-28 18:03    Length: 1,993 byte(s)
[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]
Subject: Re: 用python来打乱排序后的文件
Author: sunnyflurry    Posted: 2005-10-28 18:37    Length: 66 byte(s)
[Original] [Print] [Top]
不会吧,1000万行不是1000行,文件就100多M,你的机子这么快就跑完了?
----
keep it simple & stupid
[Original] [Print] [Top]
Subject: Re: 用python来打乱排序后的文件
Author: flingfly    Posted: 2005-10-28 20:06    Length: 26 byte(s)
[Original] [Print] [Top]
是啊,很快的。是1000万啊。
----
(1)多回答问题。
(2)多提出问题。
[Original] [Print] [Top]
Subject: Re: 用python来打乱排序后的文件
Author: passworld    Posted: 2005-10-28 22:39    Length: 236 byte(s)
[Original] [Print] [Top]
python 里面文件是一个 object ,在收垃圾的时候会自动关闭文件的,所以理论上不用关,但是因为什么时候收垃圾我们是不知道的,所以有需要你可以自己关上,不管怎么说,跟C程序一样,程序执行完后,所有打开的文件系统会自动关闭的。

----
[Original] [Print] [Top]
Subject: Re: 用python来打乱排序后的文件
Author: flingfly    Posted: 2005-10-29 10:02    Length: 1,515 byte(s)
[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]
Subject: Re: 用python来打乱排序后的文件
Author: passworld    Posted: 2005-10-29 11:40    Length: 54 byte(s)
[Original] [Print] [Top]
在 C 程序里系统也应该会 flush 然后才关闭的。
----
[Original] [Print] [Top]
Subject: Re: 用python来打乱排序后的文件
Author: flingfly    Posted: 2005-10-29 11:46    Length: 53 byte(s)
[Original] [Print] [Top]
那为什么如果C里面不关闭一个文件,缓冲的数据会丢失那?
----
(1)多回答问题。
(2)多提出问题。
[Original] [Print] [Top]
Subject: Re: 用python来打乱排序后的文件
Author: passworld    Posted: 2005-10-29 12:43    Length: 93 byte(s)
[Original] [Print] [Top]
不知道,至少在 linux 上退出是会呼叫 exit(3) 的(__libc_start_main)。应该是 posix吧?
----
[Original] [Print] [Top]
« Previous thread
请教一个简单问题
Python 编 程
5
Next thread »
mxodbc postgresql 大对象问题
     

Copyright © 2007 UNIX Resources Network, All Rights Reserved.      About URN | Privacy & Legal | Help | Contact us
备案序号: 京ICP备05006143    webmaster: webmaster@unixresources.net
This page created on 2008-07-17 04:11:01, cost 0.061378002166748 ms.