跟我一起学Python之五:序列

1.什么是数据结构?
通过某种方式把很多的数据元素组织在一起的集合。

 

2.Python内建序列(共6种)
列表和元组、字符串、Unicode字符串、buffer对象、xrange对象。

 

3.序列操作
[1]索引

索引从0开始(这点和Perl是一样的)


Python变量也存在索引
-1 最后一个元素,以此类推。

e.g.
months=['January','February','March','April','May','June','July','August','September','October','November','December']
ending=['st','nd','rd']+17*['th']+['st','nd','rd']+7*['th']+['st']
year=raw_input('Year:')
month=raw_input('Month(1-12):')
day=raw_input('Dat(1-31):')
month_number=int(month)
day_number=int(day)
month_name=months[month_number-1]

ordinal=day+ending[day_number-1]

print month_name+' '+ordinal+', '+year

>>> Year:1974
>>> Month(1-12):8
>>> Dat(1-31):16
August 16th, 1974

[2]分片
提取一部分数据,
格式scalar[n:m],提取从索引n到m-1的区间值。
>>> numbers
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers[3:6]
[4, 5, 6]

可省略:前后的值
>>> numbers[-3:]
[8, 9, 10]
>>> numbers[:3]
[1, 2, 3]

e.g.
url=raw_input('Please enter the URL:')
domain=url[11:-4]
print "Domain name:"+domain

>>> 
Please enter the URL:http://www.python.org
Domain name:python

e.g.
>>> numbers
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers[0:10:1]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers[0:10:2]
[1, 3, 5, 7, 9]
>>> numbers[0:10:3]
[1, 4, 7, 10]
>>> numbers[0:10:4]
[1, 5, 9]
>>> numbers[0:10:5]
[1, 6]
>>> numbers[0:10:6]
[1, 7]
>>> numbers[0:10:7]
[1, 8]
>>> numbers[0:10:8]
[1, 9]
>>> numbers[0:10:9]
[1, 10]
>>> numbers[0:10:10]
[1]

格式[n:m:x]  x位置可以不给,默认为1


[3]序列相加


>>> [1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]
>>> 'Hello' + "world"
'Helloworld'

列表和字符串无法合并

[4]乘法


Python
>>> 'Python'*5
'PythonPythonPythonPythonPython'


Perl
‘Python’ x 5

e.g.
sentence=raw_input("Sentence:")
screen_width=80
text_width=len(sentence)
box_width=text_width+6
left_margin=(screen_width-box_width)//2

print
print ' '*left_margin+"+"+'-'*(box_width-4)+'+'
print ' '*left_margin+"|"+' '*text_width +'  |'
print ' '*left_margin+"|"+    sentence   +'  |'
print ' '*left_margin+"|"+' '*text_width +'  |'
print ' '*left_margin+"+"+'-'*(box_width-4)+"+"
print

Sentence:shuhao

                                  +--------+
                                  |        |
                                  |shuhao  |
                                  |        |
                                  +--------+

 

 

[5]包含

e.g.
>>> permissions='rw'
>>> 'w' in permissions
True
>>> 'x' in permissions
False

>>> users=["a","b","c"]
>>> 'a' in users
True

>>> subject='$$$ get tich now !!! $$$'
>>> '$$$' in subject
True

 

in函数检查成员是否存在

database=[['a','123456'],['b','123456'],['c','123456'],['d','123456']]
username=raw_input('User Name:')
pin=raw_input("PIN code:")

if [username,pin] in database:print 'Access granted'

User Name:a
PIN code:123456
Access granted

6.获取长度、最小值、最大值

len函数获取长度
>>> numbers=[100,34,678,]
>>> len(numbers)
3


max函数获取最大值
>>> max(numbers)
678


min函数获取最小值
>>> min(numbers)
34

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值