Python的一些小知识点_5


1.如何比较两个文件的创建/修改时间?

确定搜索关键字:python compare file mtime/ctime

http://search.aol.com/aol/search?q=python+compare+file+ctime

file_list = sorted(file_list, key=os.path.getmtime) #参数中的file_list为要比较的文件列表
file_mtime = os.path.getmtime(file_abs_name) #获取文件的mtime的方法
2.如何按照某个特定的顺序读取文件内容进行合并?

确定搜索关键字:python read/concatenate multiple files in order


3.如何删除Python的字典中的某个元素?

http://search.aol.com/aol/search?q=python+delete+dict+items

dict_del

总而言之就2种方式:del语句 或 重新赋值覆盖掉

4.如何删除Python的list中的某个元素?

list.remove(x)
Remove the first item from the list whose value is x. It is an error if there is no such item.
list.pop([i])
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)

https://docs.python.org/2/tutorial/datastructures.html#more-on-lists #Python的list操作集合

>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a[0]
>>> a
[1, 66.25, 333, 333, 1234.5]
>>> del a[2:4]
>>> a
[1, 66.25, 1234.5]
>>> del a[:]
>>> a
[]
5.如何用Python移动文件?

http://search.aol.com/aol/search?q=python+move+file+to+another+dir

推荐使用:shutil.move()

http://stackoverflow.com/questions/8858008/moving-a-file-in-python

os.rename() or shutil.move()
Although os.rename() and shutil.move() will both rename files, the command that is closest to the Unix mv command is shutil.move(). The difference is that os.rename() doesn’t work if the source and destination are on different disks, while shutil.move() doesn’t care what disk the files are on.

6.Python中的sleep方法该怎么写?

http://search.aol.com/aol/search?q=python+sleep

http://stackoverflow.com/questions/510348/how-can-i-make-a-time-delay-in-python

import time
time.sleep(5) # delays for 5 seconds

Here is another example where something is run once a minute:

import time
while True:
     print "This prints once a minute."
     time.sleep(60)  # Delay for 1 minute (60 seconds)
7.Python中的字符串替换(长字符串替换的效率如何还不清楚?)

As long as you can make do with str.replace(), you should use it. It avoids all the pitfalls of regular expressions (like escaping), and is generally faster.
总而言之,建议就是:可以用字符串原生函数的地方就不要用正则表达式去完成类似的工作,效率隔了好多呢!

8.Python中的set和list的对比

http://stackoverflow.com/questions/2831212/python-sets-vs-lists

It depends on what you are intending to do with it. Sets are significantly faster when it comes to determining if an object is present in the set (as in x in s), but are slower than lists when it comes to iterating over their contents. You can use the timeit module to see which is faster for your situation.

即,效率和具体的应用有关:set结构在用于判断某个元素是否存在的时候是非常快的;但在迭代枚举包含的元素时,要比list慢。

9.Python的join方法的使用

如何连接对象数组?

http://stackoverflow.com/questions/497765/python-string-joinlist-on-object-array-rather-than-string-array

解法一:列表推导式(或生成器表达式)

', '.join([str(x) for x in list]) # list comprehension
', '.join(str(x) for x in list) # generator expression

解法二:善用map方法

''.join(map(str,list))

https://docs.python.org/2/library/stdtypes.html#str.join

10.Python的os.walk()方法返回内容的排序

之前一直使用Python的os.walk()方法遍历目录,也没什么其它的要求,用起来也是很爽的,但是近来有个需求:对os.walk()返回的列表按文件名/创建时间进行排序,就没法使用os.walk()自己返回的内容列表了,因为你不清楚它到底是怎么排序的,今天特地去搜了一下,原来如此:

http://search.aol.com/aol/search?q=python+os.walk%28%29+order

http://stackoverflow.com/questions/18282370/python-os-walk-what-order

os.walk uses os.listdir. Here is a the dostring for os.listdir:
listdir(path) -> list_of_strings
Return a list containing the names of the entries in the directory.
path: path of directory to list
The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory.(是按任意顺序返回的,而且在不同的filesystem上返回结果不同……只有自己弄了。)

所以,如果你有自己的需要,可以使用sort或sorted()方法自定义进行次排序。

借用str.index()或str.rindex()方法 && 列表切片可以达到某个目的:

files_dict = sorted( {item[item.rindex('_')+1:-4]:item for item in os.listdir(path)} )
'''
In [5]: xx = '192.168.100.110-80_192.168.100.93-51664_3469669002.txt'

In [6]: xx.index('_')
Out[6]: 18

In [7]: xx[xx.index('_'):-4]
Out[7]: '_192.168.100.93-51664_3469669002'

In [8]: xx[xx.rindex('_'):-4]
Out[8]: '_3469669002'

In [9]: xx[xx.rindex('_')+1:-4]
Out[9]: '3469669002'

In [10]: xx.rindex('_')
Out[10]: 39

In [11]: xx.index??
Type: builtin_function_or_method
String form: <built-in method index of str object at 0x036DD9D0>
Docstring:
S.index(sub [,start [,end]]) -> int

Like S.find() but raise ValueError when the substring is not found.

In [12]: xx.rindex??
Type: builtin_function_or_method
String form: <built-in method rindex of str object at 0x036DD9D0>
Docstring:
S.rindex(sub [,start [,end]]) -> int

Like S.rfind() but raise ValueError when the substring is not found.
'''

 

,

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注