3.1字典,集合,异常,函数,文件

#字典:键值对
#创建字典
empty_dict = {}
d1 = {'a':'some value','b':[1,2,3,4]}
d1
{'a': 'some value', 'b': [1, 2, 3, 4]}
#访问,插入,设定字典元素
d1[7] = 'an integer'
d1
{'a': 'some value', 'b': [1, 2, 3, 4], 7: 'an integer'}
d1['b'] 
[1, 2, 3, 4]
'b' in d1
True
d1[5] = 'some value'
d1
{'a': 'some value', 'b': [1, 2, 3, 4], 7: 'an integer', 5: 'some value'}
d1['dummy'] = 'another value'
d1
{'a': 'some value',
 'b': [1, 2, 3, 4],
 7: 'an integer',
 5: 'some value',
 'dummy': 'another value'}
del d1[5]
d1
{'a': 'some value',
 'b': [1, 2, 3, 4],
 7: 'an integer',
 'dummy': 'another value'}
ret = d1.pop('dummy')
ret
'another value'
list(d1.keys())
['a', 'b', 7]
list(d1.values())
['some value', [1, 2, 3, 4], 'an integer']
d1.update({'b':'foo','c':12})
d1
{'a': 'some value', 'b': 'foo', 7: 'an integer', 'c': 12}
#用序列创建字典
mapping = dict(zip(range(5),reversed(range(5))))
mapping
{0: 4, 1: 3, 2: 2, 3: 1, 4: 0}
#默认值
words = ['apple','bat','bar','atom','book']
by_letter = {}
for word in words:
    letter = word[0]
    if letter not in by_letter:
        by_letter[letter] = [word]
    else:
        by_letter[letter].append(word)
by_letter
{'a': ['apple', 'atom'], 'b': ['bat', 'bar', 'book']}
for word in words:
    letter = word[0]
    by_letter.setdefault(letter,[]).append(word)
by_letter
{'a': ['apple', 'atom', 'apple', 'atom', 'apple', 'atom'],
 'b': ['bat', 'bar', 'book', 'bat', 'bar', 'book', 'bat', 'bar', 'book']}
#有效的键类型
hash('string')#可哈希
-5063368329735123182
#集合
set([2,1,2,3,4,1])
{1, 2, 3, 4}
{2,1,1,2,3,4}
{1, 2, 3, 4}
a = {1,2,3,4,5}
b = {3,4,5,6,7}
a.union(b)
{1, 2, 3, 4, 5, 6, 7}
a|b
{1, 2, 3, 4, 5, 6, 7}
a.intersection(b)
{3, 4, 5}
a&b
{3, 4, 5}
#列表,集合和字典推导式
string = ['a','as','bat','car','dove','python']
[x.upper()for x in string if len(x)>2]
['BAT', 'CAR', 'DOVE', 'PYTHON']
#dict_comp = {key-expr : value-expr for value in collection if condition}
unique_lengths = {len(x)for x in string}
unique_lengths
{1, 2, 3, 4, 6}
loc_mapping = {val:index for index,val in enumerate(string)}
loc_mapping
{'a': 0, 'as': 1, 'bat': 2, 'car': 3, 'dove': 4, 'python': 5}
#嵌套列表推导式
all_data = [['John','Emily','Michael','Mary','Steven'],['Maria','Juan','Javier','Natalia','Pilar']]
result = [name for names in all_data for name in names
         if name.count('e')>=2]
result
['Steven']
some_tuples = [(1,2,3),(4,5,6),(7,8,9)]
flatted = [x for tup in some_tuples for x in tup]
flatted
[1, 2, 3, 4, 5, 6, 7, 8, 9]
#函数
#变量的命名空间,作用域,局部函数
#返回多个值
#函数也是对象
states = ['   Alabama ', 'Georgia!', 'Georgia', 'georgia', 'FlOrIda',
          'south   carolina##', 'West virginia?']
import re
def remove_punctuation(value):
    return re.sub('[!#?]','',value)
clean_ops = [str.strip,remove_punctuation,str.title]
def clean_string(strings,ops):
    result = []
    for value in strings:
        for function in ops:
            value = function(value)
        result.append(value)
    return result
clean_string(states,clean_ops)
['Alabama',
 'Georgia',
 'Georgia',
 'Georgia',
 'Florida',
 'South   Carolina',
 'West Virginia']
#匿名函数lambda
equiv_anon = lambda x:x*2
def apply_to_list(some_list,f):
    return [f(x) for x in some_list]
ints = [2,3,3,4,2,1]
apply_to_list(ints,lambda x:x*2)
[4, 6, 6, 8, 4, 2]
string =  ['foo', 'card', 'bar', 'aaaa', 'abab']
string.sort(key=lambda x:len(set(list(x))))
string
['aaaa', 'foo', 'abab', 'bar', 'card']
#柯里化:部分参数应用
def add_number(x,y):
    return x+y
add_five = lambda y:add_number(5,y)
#迭代器
some_dict = {'a':1,'b':2,'c':3}
for key in some_dict:
    print(key)
a
b
c
dict_iterator = iter(some_dict)
dict_iterator
<dict_keyiterator at 0x1a2dfdfe228>
list(dict_iterator)
['a', 'b', 'c']
#生成器
def squares(n=10):
    print('Generating squares from 1 to {0}'.format(n**2))
    for i in range(1,n+1):
        yield i**2
gen = squares()
gen
<generator object squares at 0x000001A2DFDBF750>
for x in gen:
    print(x,end=" ")
Generating squares from 1 to 100
1 4 9 16 25 36 49 64 81 100 
#生成器表达式
gen = (x**2for x in range(100))
gen
<generator object <genexpr> at 0x000001A2DFDBF930>
sum(x**2 for x in range(100))
328350
dict((i,i**2)for i in range(5))
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
#itertools模块
import itertools
first_letter = lambda x :x[0]
names = ['Alan', 'Adam', 'Wes', 'Will', 'Albert', 'Steven']
for letter,names in itertools.groupby(names,first_letter):
    print(letter,list(names))
A ['Alan', 'Adam']
W ['Wes', 'Will']
A ['Albert']
S ['Steven']
#错误和异常处理
float('something')
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-52-588ffff62dd7> in <module>()
      1 #错误和异常处理
----> 2 float('something')


ValueError: could not convert string to float: 'something'
def attempt_float(x):
    try:
        return float(x)
    except:
        return x
attempt_float('something')
'something'
float((1,2))
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-54-a101b3a3d6cd> in <module>()
----> 1 float((1,2))


TypeError: float() argument must be a string or a number, not 'tuple'
def attempt_float(x):
    try:
        return float(x)
    except (TypeError, ValueError):
        return x
attempt_float((1,2))
(1, 2)
#finally:无论try是否成功执行都执行的一段代码
#else:让只在try部分成功的情况下执行
try:
    write_to_file(f)
except:
    print('Failed')
else:
    print('Succeeded')
finally:
    f.close()
#文件和操作系统
path = 'example.txt'
f = open(path)   #默认只读模式

在这里插入图片描述

lines = [x.rstrip() for x in open(path)]
lines
['lzq', 'lfp', 'lkp', 'mjy']
f.close()#关闭文件,释放资源
with open(path)as f:           #with 在代码结束自动释放资源
    lines = [x.rstrip()for x in f]
f = open(path)
f.read(3)
'lzq'
f2 = open(path,'rb')3#二进制
f2.read(3)
b'lzq'
import sys
sys.getdefaultencoding()
'utf-8'
f.close()
f2.close()
#写入文件
with open('example.txt','w')as handle:
    handle.writelines(x for x in open(path) if len(x)>1)
with open('example.txt')as f:
    lines = f.readlines()
lines
[]
#文件的字节和Unicode
with open(path)as f:
    chars = f.read(3)
chars
'lzq'
 with open(path, 'rb') as f:
                data = f.read(3)
data
b'lzq'
data.decode('utf8')
'lzq'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值