Python Study Note 1

来源:互联网 发布:复杂网络数据集 编辑:程序博客网 时间:2024/06/05 09:05

python数据结构

对象类型 特点 例子 列表list 有序,可改变值 list=['bart','lisa','maggie'] 元组tuple 有序,创建完后无法修改 t=('bart','lisa','maggie') 字典dict 有序,键值对类似java的map,key值不能重复 d={'bart':69,'lisa':98,'maggie':89} set 无序,set值不能重复 s=(['bart','lisa','maggie'])或者list=['bart','lisa','maggie'] set=(list)

python的字符串编码问题

如果中文字符串在Python环境下遇到 UnicodeDecodeError,这是因为.py文件保存的格式有问题。可以在第一行添加注释

# -*- coding: utf-8 -*-

程序暂停

# -*- coding: utf-8 -*-import os#函数体# 引入这两个函数是因为程序执行完之后窗口不会关闭,可以看到输出结果# raw_input()os.system('pause')

python的函数

#有参数,带返回值,形参有默认值def fun(a,b=10,c=20):    return a+b+cprint fun()#错误,因为有形参,所以值不能为空print fun(10)#输出40print fun(10,10,10)#输出30# 带多个参数的函数# *args实际上是一个tuple(元组)def many_fun(*args):    print argsmany_fun()many_fun(1,2,3)many_fun(1,2,3,4,5,6)#输出# ()# (1, 2, 3)# (1, 2, 3, 4, 5, 6)

切片

List的切片

截取一个List,传统方法是利用循环十分麻烦,Python内置的一个方法完全取代了循环,这就是切片

切片取值包含起始索引,不包含结束索引

正序索引:

L = range(1, 101)# 从索引1开始到索引5,不包括索引5print L[1:5]# 索引5之前的print L[:5]print L[96:]# 每隔20元素取一个print L[::20]

输出:

[2, 3, 4, 5][1, 2, 3, 4, 5][97, 98, 99, 100][1, 21, 41, 61, 81]

倒叙索引:

L = range(1, 11)print L[-5:-1]print L[-5:]print L[:-5]print L[-10:-1:2]

输出:

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

对字符串切片

def firstCharUpper(s):    return s[:1].upper()+s[1:]print firstCharUpper('hello')print firstCharUpper('sunday')print firstCharUpper('september')

输出:

HelloSundaySeptember

迭代

因为 Python 的 for循环不仅可以用在list或tuple上,还可以作用在其他任何可迭代对象上。
因此,迭代操作就是对于一个集合,无论该集合是有序还是无序,我们用 for 循环总是可以依次取出集合的每一个元素。

注意: 集合是指包含一组元素的数据结构,我们已经介绍的包括:
1. 有序集合:list,tuple,str和unicode;
2. 无序集合:set
3. 无序集合并且具有 key-value 对:dict

#enumerate()函数吧一个List转化为他的索引和list值的tuple#例如:enumerate(['a','b','c'])等同于[(0,'a'),(1,'b'),(2,'c')]L = ['Adam', 'Lisa', 'Bart', 'Paul']for index, name in enumerate(L):    print index, '-', nameL2 = ['Adam', 'Lisa', 'Bart', 'Paul']for index, name in zip(range(1,5),L2):    print index, '-', name

输出:

0 - Adam1 - Lisa2 - Bart3 - Paul1 - Adam2 - Lisa3 - Bart4 - Paul

迭代-复杂表达式

利用迭代输出HTML表格:

d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }def generate_tr(name, score):    return '<tr><td style="color:red">%s</td><td style="color:red">%s</td></tr>' % (name, score)tds = [generate_tr(name,score) for name, score in d.iteritems()]print '<table border="1">'print '<tr><th>Name</th><th>Score</th><tr>'print '\n'.join(tds)print '</table>'

输出:

Name Score Lisa 85 Adam 95 Bart 59

迭代-条件过滤

def toUppers(L):    return [x.upper() for x in L if isinstance(x,str)]print toUppers(['Hello', 'world', 101])

输出:

['HELLO', 'WORLD']

迭代-多层表达式

利用 3 层for循环的列表生成式,找出对称的 3 位数。例如,121 就是对称数,因为从右到左倒过来还是 121。

print [a*100+b*10+c for a in range(0,10) for b in range(0,10) for c in range(0,10) if a==c and a !=0]

输出:

[101, 111, 121, 131, 141, 151, 161, 171, 181, 191,202, 212, 222, 232, 242, 252, 262, 272, 282, 292,303, 313, 323, 333, 343, 353, 363, 373, 383, 393,404, 414, 424, 434, 444, 454, 464, 474, 484, 494,505, 515, 525, 535, 545, 555, 565, 575, 585, 595,606, 616, 626, 636, 646, 656, 666, 676, 686, 696,707, 717, 727, 737, 747, 757, 767, 777, 787, 797,808, 818, 828, 838, 848, 858, 868, 878, 888, 898,909, 919, 929, 939, 949, 959, 969, 979, 989, 999]
0 0