Python Study Note 1

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+c
print fun()#错误,因为有形参,所以值不能为空
print fun(10)#输出40
print fun(10,10,10)#输出30

# 带多个参数的函数
# *args实际上是一个tuple(元组)
def many_fun(*args):
    print args

many_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,不包括索引5
print 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')

输出:

Hello
Sunday
September

迭代

因为 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, '-', name

L2 = ['Adam', 'Lisa', 'Bart', 'Paul']
for index, name in zip(range(1,5),L2):
    print index, '-', name

输出:

0 - Adam
1 - Lisa
2 - Bart
3 - Paul
1 - Adam
2 - Lisa
3 - Bart
4 - 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>'

输出:

NameScore
Lisa85
Adam95
Bart59

迭代-条件过滤

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值