一些Python片段_2


基础回顾:

关于Python内置数据结构和库的最重要的功能,自行查阅:http://docs.python.org

  • Python的字符串格式化
  • 日期和时间
控制流:

if-elif-else
for循环
while循环
异常处理
三元表达式

数据结构和序列:
元组

元组拆包
元组方法( .count )

列表

添加和删除元素
合并列表
排序
切片

内置的序列函数

enumerate
sorted
zip
reversed

字典(使用大括号,并用冒号分隔key/value)

从序列类型创建字典(字典推导式)
默认值( .setdefault方法 & collections.defaultdict )
字典键的有效类型

集合

Python的集合运算(交、并、差)

推导式

列表、集合以及字典的推导式
嵌套列表推导式

####
def get_counts(sequence):
    counts = {}
    for x in sequence:
        if x in counts:
            counts[x] += 1
        else:
            counts[x] = 1
    return counts
####
from collections import defaultdict
def get_counts2(sequence):
    counts = defaultdict(int)
    for x in sequence:
        counts[x] += 1
    return counts
####

'''
1.Python语言精要
关于Python内置数据结构和库的最重要的功能 http://docs.python.org

变量和按引用传递
二元运算符和比较运算符(is关键字--判断两个引用是否指向同一对象)
Python的字符串格式化
日期和时间

控制流
    if-elif-else
    for循环
    while循环
    异常处理
    三元表达式

数据结构和序列
元组
    元组拆包
    元组方法( .count )
列表
    添加和删除元素
    合并列表
    排序
    切片
内置的序列函数
    enumerate
    sorted
    zip
    reversed

字典(使用大括号,并用冒号分隔key/value)
    从序列类型创建字典(字典推导式)
    默认值( .setdefault方法 & collections.defaultdict )
    字典键的有效类型
集合
    Python的集合运算(交、并、差)

列表、集合以及字典的推导式
    嵌套列表推导式


使用函数返回多个值
函数亦为对象
生成器(生成器表达式)

'''
path = 'C:test.txt'
lines = [x.rstrip() for x in open(path)]

f = open(path)
for line in f:
    pass


####生成器####
def make_change(amount, coins=[1, 5, 10, 25], hand=None):
	hand = [] if hand is None else hand
	if amount == 0:
		yield hand
	for coin in coins:
		if coin > amount or (len(hand) > 0 and hand[-1] < coin):
			continue
		for result in make_change(amount-coin, coins=coins, hand=hand+[coin]):
			yield result

for way in make_change(100, coins=[10, 25, 50]):
	print way
####

####函数亦为对象####
states = ['  Alabama ', 'Georgia!', 'Georgia', 'georgia', 'south    carolina##', ' West virginia?', 'New York .', 'China.', 'Beijing...']

import re
def clean_stings(strings):
    result = []
    for value in strings:
        value = value.strip()
        value = re.sub('[!#?]', '', value)
        value = value.title()
        result.append(value)
    return result
clean_stings(states)

def remove_punctuation(value):
    return re.sub('[!?#]', '', value)
clean_ops = [str.strip, remove_punctuation, str.title]
def clean_stings2(strings, ops):
    result = []
    for value in strings:
        for function in ops:
            value = function(value)
        result.append(value)
    return result
clean_stings2(states, clean_ops)

map(remove_punctuation, states) #内置的map函数有点牛逼啊!

####

def f():
    a = 5
    b = 6
    c = 7
    return a, b, c
x, y, z = f()


####推导式####

#列表推导式
strings = ['a', 'as', 'a', 'bad', 'boy', 'Python']
[x.upper() for x in strings]
[x.upper() for x in strings if len(x)>2]

#字典推导式
dict_map = {key:value for key, value in enumerate(strings)}
dict_map = dict((value, key) for key, value in enumerate(strings))

#集合推导式
set_map = {key for key in strings}

#嵌套列表推导式
all_data = [['Tom', 'Jerry', 'Lily', 'Lucy', 'Hello,world', 'Jefferson', 'Steven', 'Joe', 'Bill'], ['Susie', 'Cookie', 'Qunar', 'Baidu', 'Notepad', 'Apple', 'Alibaba']]
names_of_interset = []
for names in all_data:
    enough = [name for name in names if name.count('e')>2]
    names_of_interset.extend(enough)

names2 = [name for names in all_data for name in names if name.count('e') > 2]
names3 = [name for names in all_data for name in names]


#zip
seq1 = ['a', 'b', 'c']
seq2 = ['one', 'two', 'three']
zip(seq1, seq2)
#zip可以接受任意数量的序列,最终得到的元组数量由最短的序列决定
seq3 = ['True', 'False']
zip(seq1, seq2, seq3)


#控制流
if x < 0:
	print 'It's negative'
elif x == 0:
	print 'Equal to zero'
elif 0 < x < 5:
	print 'Between 0 and 5'
else:
	print 'Positive and larger than 5'

####
sequence = [1, 2, None, 4, None, 5]
total = 0
for value in sequence:
	if value is None:
		continue
	total += value
####

sequence = [1, 2, 0, 4, 6, 5, 2, 1]
total_until_5 = 0
for value in sequence:
	if value == 5:
		break
	total_until_5 += value
####

#Python三元表达式
value = true_expr if condition else false_expr

 

用Python下载豆瓣上的音乐
#!/usr/bin/env python
# -- coding:utf-8 --

import urllib, sys, gevent, re
from gevent import monkey

reload(sys)
sys.setdefaultencoding('utf8')
monkey.patch_all()

def encode(s):
    return s.decode('utf-8').encode(sys.stdout.encoding, 'ignore')

def worker(reg, url):
    response = urllib.urlopen(url)
    text = response.read()
    groups = re.finditer(reg, text)
    m_arr = []
    for g in groups:
        name = encode(g.group(1).strip()) + ".mp3"
        path = g.group(2).replace('\', '')
        #print encode(name), path; sys.exit()
        m_arr.append((name, path))
    return m_arr

def grun(path, name):
    urllib.urlretrieve(path, name)

if __name__  == '__main__':
    reg = re.compile('{"name":"(.+?)".+?"rawUrl":"(.+?)",.+?}', re.M)
    musicArray = worker(reg, "http://site.douban.com/huazhou/")
    jobs = []
    for (name, path) in musicArray:
        #print name, path
        jobs.append(gevent.spawn(grun, path, name))
    gevent.joinall(jobs)

代码参考地址使用gevent多线程下载豆瓣音乐

字符串逆序
def reverseWords(s):
    wordlist = s.split()
    return ' '.join(wordlist[::-1])
print reverseWords("Haha, just do it")

 

,

《 “一些Python片段_2” 》 有 3 条评论

  1. Python 3中下载图片的三种方法
    https://blog.csdn.net/qq_34504481/article/details/79716106
    `
    from urllib.request import urlretrieve
    urlretrieve(IMAGE_URL, ‘./image/img1.png’)

    import requests
    r = requests.get(IMAGE_URL)
    with open(‘./image/img2.png’, ‘wb’) as f:
    f.write(r.content)

    import requests
    r = requests.get(IMAGE_URL, stream=True)
    with open(‘./image/img3.png’, ‘wb’) as f:
    for chunk in r.iter_content(chunk_size=32):
    f.write(chunk)
    `

发表回复

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