# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name: common_module.py
Author : LiSen
Date: 2018/6/27 9:51:
-------------------------------------------------
"""
# 1.字典无序的问题。
'''
字典虽两个显著的缺点,其中之一就是无序,如何在构造字典的时候,让字典里面填入的内容有序呢,
一般的方法都是等字典的内容填充好了之后,用sorted排序,但是sorted,也并不能保证是按照添加的顺序进行迭代
from collections import OrderedDict
from string import ascii_uppercase
print ascii_uppercase #ABCDEFGHIJKLMNOPQRSTUVWXYZ
print zip(ascii_uppercase,range(5)) #[('A', 0), ('B', 1), ('C', 2), ('D', 3), ('E', 4)]
print dict(zip(ascii_uppercase,range(5))) #{'A': 0, 'C': 2, 'B': 1, 'E': 4, 'D': 3} 无序
a = OrderedDict(zip(ascii_uppercase,range(5))) #OrderedDict([('A', 0), ('B', 1), ('C', 2), ('D', 3), ('E', 4)])
print a
'''
#2.字典无缺省值的问题
'''
from collections import defaultdict
student = defaultdict(lambda :60)
student['bob'] = 80
student['jon'] = 90
print student #defaultdict(<function <lambda> at 0x000000000332FC18>, {'bob': 80, 'jon': 90})
print student['ll'] #60
print student #{'bob': 80, 'll': 60, 'jon': 90})
d = defaultdict(set)
name = ['leo','jack','john']
for index,n in enumerate(name):
d[n].add(('180cm',60+index))
print d #defaultdict(<type 'set'>, {'john': set([('180cm', 62)]), 'jack': set([('180cm', 61)]), 'leo': set([('180cm', 60)])})
'''
#3.像类一样玩转元组数据
'''
from collections import namedtuple
student = namedtuple('students','name,score,weight')
s1 = student(name='leo',score=100,weight=65)
print s1 #students(name='leo', score=100, weight=65)
print s1.name #leo
'''
# 4.题目:出现1次的数字
'''
给定一个数组里面有一串数字,其中有2个数字只出现了1次,剩下的数字出现2次!要求返回只出现1次的数字的和!
比如repeats([4,5,7,5,4,8]) = 15,因为7和8数字出现1次,它的和为15
'''
'''
num_array = [4,5,7,5,4,8]
a = [i for i in num_array if num_array.count(i) ==1]
print sum(a)
print a
print num_array.count(5)
unique_num_dict = {e:num_array.count(e) for e in num_array if num_array.count(e)==1}
print unique_num_dict
def repeats(num_array):
unique_num_dict = {e:num_array.count(e) for e in num_array if num_array.count(e)==1}
total = sum((list(unique_num_dict.keys())))
return total
print repeats(num_array)
def repeats1(arr):
numbers = set()
for i in arr:
if i in numbers:
numbers.remove(i)
else:
numbers.add(i)
return sum(numbers)
print repeats1(num_array)
'''
#5.enumerate
'''
cities = ['shanghai','beijing']
for i, city in enumerate(cities):
print i+1,'-->',city
1 --> shanghai
2 --> beijing
'''
#6.zip
'''
a = [1,2,3]
b = [1,2,3,4]
print zip(a,b)
[(1, 1), (2, 2), (3, 3)]
'''
#7.循环查找
"""即在for 循环中,如果没有从任何一个break中退出,则会执行和for对应的else
只要从break中退出了,则else部分不执行。"""
'''
target_letter = 'd'
letters = ['a','b','c']
for litter in letters:
if litter == target_letter:
print 'found'
break
else:
print 'not found'
'''
# 8.字符串——————join
url = ['www','baidu','com']
print '.'.join(url)
line = '*'*30
print line
a = 'hello'
b = a[::]
print id(b),id(a) #59367056 59367056
import re
word = 'hello,word; python, i , like '
print re.split(r'[,;]',word) #['hello', 'word', ' python', ' i ', ' like ']
students = 'boy 101,girl 102'
print re.sub(r'\d+','200',students) #boy 200,girl 200
import string
instr = 'to'
outstr = 'TO'
table = string.maketrans(instr,outstr)
old_str = 'hello world,welcome to use python.123'
new_str = old_str.translate(table,'hel')
print new_str #O wOrd,wcOm TO us pyTOn.123