Python list去重


基础方法

ids = [1,2,3,3,4,2,3,4,5,6,1]
news_ids = []
for id in ids:
    if id not in news_ids:
        news_ids.append(id)
print news_ids

set 容器

ids = [1,4,3,3,4,2,3,4,5,6,1]
news_ids = list(set(ids))
news_ids.sort(ids.index)

itertools.grouby

ids = [1,4,3,3,4,2,3,4,5,6,1]
ids.sort()
it = itertools.groupby(ids)
for k, g in it:
    print k

reduce

ids = [1,4,3,3,4,2,3,4,5,6,1]
func = lambda x,y:x if y in x else x + [y]
reduce(func, [[], ] + ids)
[1, 4, 3, 2, 5, 6]
阅读全文

让你的 Python 代码更 Pythonic


变量交换

a, b = b, a

循环遍历

for i in xrange(6):
    print i

带有索引位置的集合遍历

colors = ['red', 'green', 'blue', 'yellow']
for i, color in enumerate(colors):
    print i, color

字符串拼接

names = ['raymond', 'rachel', 'matthew', 'roger', 'betty', 'melissa', 'judith', 'charlie']
print ', '.join(names)

打开文件

with open('data.txt') as f:
    data = f.read()

列表推导式

[j for i in xrange(10)]

遍历字典

for k, v in d.iteritems():
    print k, v
for i, v in enumerate(l):
    pass

构建字典

names = ['raymond', 'rachel', 'matthew']
colors = ['red', 'green', 'blue']
d = dict(izip(names, colors))

阅读全文

Python 如何处理 ImmutableMultiDict 中的数据


Flask 与前端数据交互的时候,经常会使用 ImmutableMultiDict、MultiDict 类型。

combined = CombinedMultiDict([self])
getData = combined['endDate']

self 是一组 ImmutableMultiDict 类型,endDate 是一个 key。
下面是官方的样例:

>>> from werkzeug.datastructures import CombinedMultiDict, MultiDict
>>> post = MultiDict([('foo', 'bar')])
>>> get = MultiDict([('blub', 'blah')])
>>> combined = CombinedMultiDict([get, post])
>>> combined['foo']
'bar'
>>> combined['blub']
'blah'
阅读全文

Flask 时快时慢的问题


app.run(host="0.0.0.0", port=8080)

当 app.run 设置成这样的时候,有时候页面加载会时快时慢。
原来默认 Flask 是单线程的,需要更改参数。

app.run(host="0.0.0.0", port=8080, threaded=True)

官方文档原文:

werkzeug.serving.run_simple(hostname, port, application, use_reloader=False, use_debugger=False, use_evalex=True, extra_files=None, reloader_interval=1, reloader_type='auto', threaded=False, processes=1, request_handler=None, static_files=None, passthrough_errors=False, ssl_context=None)

threaded – should the process handle each request in a separate thread?
processes – if greater than 1 then handle each request in a new process up to this maximum number of concurrent processes.
阅读全文

Python ascii 转成 utf8


a = '\xe4\xb8\xad'
print a
print type(a)
print a.decode()

这样会报错

➜  Test python unicode.py
中
<type 'str'>
Traceback (most recent call last):
  File "unicode.py", line 7, in <module>
    print a.decode()
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)

decode 的时候 需要注明编码

a = '\xe4\xb8\xad'
print a
print type(a)
aa = a.decode('utf-8')
print aa

结果

➜  Test python unicode.py
中
<type 'str'>
中
阅读全文

Python 升级导致 yum 命令无法使用的解决办法


报错信息如下

[root@develop local]# yum -y install prce  
There was a problem importing one of the Python modules  
required to run yum. The error leading to this problem was:  
  
  
   No module named yum  
  
  
Please install a package which provides this module, or  
verify that the module is installed correctly.  
  
  
It's possible that the above module doesn't match the  
current version of Python, which is:  
2.6.1 (r261:67515, Aug 7 2010, 11:36:17)  
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)]  
  
  
If you cannot solve this problem yourself, please go to  
the yum faq at:  
http://wiki.linux.duke.edu/YumFaq

阅读全文