第1、2、3章节

第一、二章

安装包:conda install pack_name或者pip install package_name


升级包:conda update package_name

代码运行按ctrl+c终止运行

第三章

3.1 数据结构和序列

#元组
tup=4,5,6#创建元组最简单方式:逗号分隔一列值
tup
(4, 5, 6)
#复杂表达式定义元组
nested_up=(4,5,6),(7,8)
nested_up
((4, 5, 6), (7, 8))
#tuple可将序列转化为元组
tuple([1,2,3])
(1, 2, 3)
tup=tuple('dddd')
tup
('d', 'd', 'd', 'd')
#元组访问元素:[]
tup[1]
'd'
tup=tuple(['foo',[1,2],True])
tup[2]=false
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

<ipython-input-9-2cfaab47682d> in <module>
      1 tup=tuple(['foo',[1,2],True])
----> 2 tup[2]=false


NameError: name 'false' is not defined
tup[1].append(3)
tup
('foo', [1, 2, 3, 3], True)

元组中的对象是不可更改的,但是如果对象是列表等,可以对这个对象进行更改

#元组串联:+
(4,'none')+((6,0),'lu')
(4, 'none', (6, 0), 'lu')
#元组乘以数,会将元组的复制串联起来
('foo','bar')*4
('foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'bar')

拆分元组

#将元组拆分赋值
tup=(4,5,6)
a,b,c=tup
b
5
#含有元组的元组拆分
tup=(4,5,(6,7))
a,b,(c,d)=tup
d
7
#format方法:替换格式的参数为字符串
template='{0:.2f} {1:s} are worth US${2:d}'

{0:.2f} 表示格式化第一个参数为带有两位小数的浮点数


{1:s} 表示格式化第二个参数为字符串


{2:d} 表示格式化第三个参数为一个整数

#format方法替换参数为格式化参数
template.format(4.5600,'AA DFG',1)
'4.56 AA DFG are worth US$1'
seq=[(1,2,3),(4,5,6),(7,8,9)]

for a,b,c in seq:
    print('a={0},b={1},c={2}'.format(a,b,c))
a=1,b=2,c=3
a=4,b=5,c=6
a=7,b=8,c=9
#使用*rest抓取任意长度的参数
values=1,2,3,5
a,b,*rest=values

print(a)
print(b)
print(*rest)
1
2
3 5
#count统计某个值出现频率
a=(1,3,3,3,3)

print(a.count(1))
print(a.count(3))
1
4

列表

a_list=[2,3,7,'none']
tup=('foo','bar','baz')
b_list=list(tup)
b_list
['foo', 'bar', 'baz']
b_list[1]='peek'
b_list
['foo', 'peek', 'baz']
#list常用来实体化
gen=range(10)

print(gen)
print(list(gen))
range(0, 10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

列表:添加和删除元素

b_list.append('b')

b_list
['foo', 'peek', 'baz', 'b']
#insert在特定位置插入元素
b_list.insert(0,'dd')
b_list
['dd', 'foo', 'peek', 'baz', 'b']
#pop运算:移除并返回指定位置元素
b_list.pop(0)

'foo'
b_list
['peek', 'baz', 'b']
#remove:寻找第一个值去除
b_list.append('b')
b_list
['peek', 'baz', 'b', 'b']
b_list.remove('b')
b_list
['peek', 'baz', 'b']
#in:检查列表是否包含某个值
'dd' in b_list
False
'dd' not in b_list
True

串联和组合列表

['a',444]+['ee',55,'ff']
['a', 444, 'ee', 55, 'ff']
#extend:追加多个元素
x=[4,'none','goo']
x.extend([3,4,(3,4)])
print(x)
[4, 'none', 'goo', 3, 4, (3, 4)]

二分搜索和维护已排序的列表

bisect.bisect:找到应该插入位置后仍然保证排序的位置(基于二分查找)


bisect.insort:向指定位置插入值

import bisect

c = [1, 2, 2, 2, 3, 4, 7]

bisect.bisect(c,2)
4
bisect.bisect(c,5)
6
c#说明bisect.bisect只查找而不插入
[1, 2, 2, 2, 3, 4, 7]
bisect.insort(c,6)

序列函数

enumerate函数:返回(i,value)元组

some_list = ['foo', 'bar', 'baz']

mapping={}

for i,v in enumerate(some_list):
    mapping[v]=i
    
mapping
{'foo': 0, 'bar': 1, 'baz': 2}

sorted:从任意序列返回一个新排好序的列表

sorted([7,1,2,6,0])
[0, 1, 2, 6, 7]

zip:将多个列表、元组或其他序列成对组成为一个元组列表

seq1 = ['foo', 'bar', 'baz']
seq2 = ['one', 'two', 'three']

zipped=zip(seq1,seq2)

list(zipped)
[('foo', 'one'), ('bar', 'two'), ('baz', 'three')]

zip 可以处理任意多的序列,元素的个数取决于最短的序列

seq3 = [False, True]

list(zip(seq1,seq2,seq3))
[('foo', 'one', False), ('bar', 'two', True)]
for i,(a,b) in enumerate(zip(seq1,seq2)):
    print('{0},{1},{2}'.format(i,a,b))
0,foo,one
1,bar,two
2,baz,three

reversed函数:翻转序列

reversed(range(10))
<range_iterator at 0x25e20009e90>
list(reversed(range(10)))
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

reversed函数只有实体化(列表或者for循环)后才能显示出序列

字典

update方法实现字典与字典融合

d1={'a': 'some value', 'b': [1, 2, 3, 4], 7: 'an integer'}
d1.update({'d`':'gfgg','c':'hjk'})
d1
{'a': 'some value',
 'b': [1, 2, 3, 4],
 7: 'an integer',
 'd`': 'gfgg',
 'c': 'hjk'}

用序列创建字典

mapping=dict(zip(range(5),reversed(range(5))))
mapping
{0: 4, 1: 3, 2: 2, 3: 1, 4: 0}

集合

集合是无序的不可重复的元素的集合。你可以把它当做字典,但是只有键没有值

set([3,4,5])#集合创建方法1
{3, 4, 5}
{2,3,4,5}#集合创建方法2
{2, 3, 4, 5}
a = {1, 2, 3, 4, 5}
b = {3, 4, 5, 6, 7, 8}
#合并:union或者|,取两个集合不重复元素
a.union(b)
{1, 2, 3, 4, 5, 6, 7, 8}
a|b
{1, 2, 3, 4, 5, 6, 7, 8}
#取交集:intersection或&方法
a.intersection(b)
{3, 4, 5}
a&b
{3, 4, 5}

列表、集合和字典推导式

形式:[expr for val in collection if condition]

strings = ['a', 'as', 'bat', 'car', 'dove', 'python']

[x.upper() for x in strings if len(x)>2]
['BAT', 'CAR', 'DOVE', 'PYTHON']

字典推导式:dict_comp = {key-expr : value-expr for value in collection if condition}


集合推导式:set_comp = {expr for value in collection if condition}

unique_lengths={len(x) for x in strings}
unique_lengths
{1, 2, 3, 4, 6}
loc_mapping={index:value for index,value in enumerate(strings)}

loc_mapping
{0: 'a', 1: 'as', 2: 'bat', 3: 'car', 4: 'dove', 5: 'python'}

嵌套列表推导式

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]
#列表推导式的for部分是根据嵌套的顺序,过滤条件在最后
result
['Steven']
some_tuples = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]

fla=[x for tup in some_tuples for x in tup]

fla
[1, 2, 3, 4, 5, 6, 7, 8, 9]

函数

返回多个值
def f():
    a=5
    b=6
    c=7
    return a,b,c
a,b,c=f()
print(a)
print(b)
5
6
def f():
    a=5
    b=6
    c=7
    return {'a':a,'b:','c:'c}
函数也是对象
states = [' Alabama ', 'Georgia!', 'Georgia', 'georgia', 'FlOrIda',
          'south carolina##', 'West virginia?']
def remove_punctuation(value):
    return re.sub('[!#?]', '', value)

clean_ops = [str.strip, remove_punctuation, str.title]

def clean_strings(strings, ops):
    result = []
    for value in strings:
        for function in ops:
            value = function(value)
            result.append(value)
    return result
匿名函数

匿名函数通过lambda定义

def short_fucntion(x):
    return x*2

equiv_anon=lambda x :x*2
def apply_to_list(some_list, f):
    return [f(x) for x in some_list]

ints = [4, 0, 1, 5, 6]
apply_to_list(ints, lambda x: x * 2)
[8, 0, 2, 10, 12]
#根据各字符串不同字母的数量排序
strings = ['foo', 'card', 'bar', 'aaaa', 'abab']

strings.sort(key=lambda x:len(set(list(x))))

strings
['aaaa', 'foo', 'abab', 'bar', 'card']

3 柯里化:部分参数应用

#两数相加的函数
def add_numbers(x,y):
    return x+y
add_five=lambda y:add_numbers(5,y)
#functools模块可使用partial函数将过程简化
from functools import partial
add_five=partial(add_numbers,5)
生成器/迭代器

some_dict={'a':1,'b':2,'c':3}
for key in some_dict:
    print(key)
a
b
c
#迭代器:在如for之类的上下文中向python输送对象
dict_iterator=iter(some_dict)
dict_iterator#获得迭代器
<dict_keyiterator at 0x1ab9abe6360>
list(dict_iterator)#实体化迭代器
['a', 'b', 'c']
#生成器
def squares(n=10):
    print('Generating squares form 1 to {0}'.format(n**2))
    for i in range(1,n+1):
        yield i**2
#调用生成器
gen=squares()
gen
<generator object squares at 0x000001AB9B6CAB30>
#只有你在生成器中请求元素时,才会开始执行代码
for x in gen:
    print(x,end=' ')
Generating squares form 1 to 100
1 4 9 16 25 36 49 64 81 100 
生成器表达式
#生成器表达式:把列表推导式两端的方括号改成圆括号
gen=(x**2 for x in range(100))

gen
<generator object <genexpr> at 0x000001AB9B5032E0>
#生成器表达式也可代替列表推导式,作为函数参数
sum(x**2 for x in range(100))
328350
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('1.233')
1.233
#float('something')会报错
#解决办法:
def attempt_float(x):
    try:
        return float(x)
    except:
        return x
attempt_float('1.33')
1.33
attempt_float('someting')
'someting'
#type 错误
#flloat((1,2))

def attempt_float(x):
    try:
        return float(x)
    except ValueError:#只有valueerror才会运行以下代码
        return x
attempt_float((1,2))
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-26-102527222085> in <module>
----> 1 attempt_float((1,2))


<ipython-input-25-3e5502cec228> in attempt_float(x)
      4 def attempt_float(x):
      5     try:
----> 6         return float(x)
      7     except ValueError:
      8         return x


TypeError: float() argument must be a string or a number, not 'tuple'
attempt_float('sometin')
'sometin'
def attempt_float(x):
    try:
        return float(x)
    except (ValueError,TypeError):
        return x
attempt_float((1,2))
(1, 2)

不论try是否成功,都执行一段代码,可考虑使用finally:


f=open(path,‘w’)


try:


write_to_file(f)


finally:


f.clsoe()


此时文件f总会被关闭

# f=open(path,'w')
# try:
#     write_to_file(f)
#except:
#     print('Failed')
#else:
#     print('Succeeded')
#如果try能执行,输出succeeded
# finally:
#     f.clsoe()

3.3文件和操作系统

path='pydata-book/examples/segismundo.txt'

f=open(path)
#得到没有行结束符的行
lines=[x.rstrip() for x in open(path)]

lines
['Sue帽a el rico en su riqueza,',
 'que m谩s cuidados le ofrece;',
 '',
 'sue帽a el pobre que padece',
 'su miseria y su pobreza;',
 '',
 'sue帽a el que a medrar empieza,',
 'sue帽a el que afana y pretende,',
 'sue帽a el que agravia y ofende,',
 '',
 'y en el mundo, en conclusi贸n,',
 'todos sue帽an lo que son,',
 'aunque ninguno lo entiende.',
 '']
f.close()#如果使用open打开了文件,一定要关闭
with open(path) as f:
    lines=[x.rstrip() for x in f]
    
#使用with:代码结束自动关闭文件

文字的字节和unicode

f=open(path)
f.read(10)
#读10个字节
'Sue帽a el r'
f2=open(path,'rb')#二进位
f2.read(10)
b'Sue\xc3\xb1a el '
f.tell()
11
f2.tell()
10
f.seek(3)#指针放在第三个位置上
3
f.read(1)
'帽'
f.tell()#说明读的是2个byte
5
f.read(1)
'a'
f.tell()
6
f.close()
f2.close()
with open('tmp.txt','w') as handle:
    handle.writelines(x for x in open(path) if len(x)>1)
with open('tmp.txt') as f:
    lines=f.readlines()
lines
['Sue帽a el rico en su riqueza,\n',
 'que m谩s cuidados le ofrece;\n',
 'sue帽a el pobre que padece\n',
 'su miseria y su pobreza;\n',
 'sue帽a el que a medrar empieza,\n',
 'sue帽a el que afana y pretende,\n',
 'sue帽a el que agravia y ofende,\n',
 'y en el mundo, en conclusi贸n,\n',
 'todos sue帽an lo que son,\n',
 'aunque ninguno lo entiende.\n']

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值