Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19699212
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: Python/Ruby

2009-08-16 09:24:53

§2    列表和数组


2009-8-16

磁针石:xurongzhong#gmail.com

博客:oychw.cublog.cn


§2.1 Sequence概述

Sequence的种类:lists and tuplesstrings Unicode strings, buffer objects, and xrange。通用操作有:indexing,slicing, adding, multiplying, and checking for membership

lists是变长的,但是tuples不是。

 

>>> edward = ['Edward Gumby', 42]

>>> john = ['John Smith', 50]

>>> database = [edward, john]

>>> database

[['Edward Gumby', 42], ['John Smith', 50]]

 

索引:

 

>>> greeting = 'Hello'

>>> greeting[0]

'H'>>> greeting[-1]

'o'

 

>>> fourth = raw_input('Year: ')[3]

Year: 2005

>>> fourth

'5'

 

§2.2通用的Sequence操作

# Print out a date, given year, month, and day as numbers

 

months = [

    'January',

    'February',

    'March',

    'April',

    'May',

    'June',

    'July',

    'August',

    'September',

    'October',

    'November',

    'December'

]

 

# A list with one ending for each number from 1 to 31

endings = ['st', 'nd', 'rd'] + 17 * ['th'] \

        + ['st', 'nd', 'rd'] +  7 * ['th'] \

        + ['st']

 

year    = raw_input('Year: ')

month   = raw_input('Month (1-12): ')

day     = raw_input('Day (1-31): ')

 

month_number = int(month)

day_number = int(day)

 

# Remember to subtract 1 from month and day to get a correct index

month_name = months[month_number-1]

ordinal = day + endings[day_number-1]

 

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

 

运行结果:

D:\python\Chapter02>listing2-1.py

Year: 2005

Month (1-12): 2

Day (1-31): 30

February 30th, 2005

 

Slicing(区间)

>>> tag = 'Python web site'

>>> tag[9:30]

''

>>> tag[32:-4]

'Python web site'

 

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

       numbers[-3:-1][-3:0]是不合法的。必须要求第2个数在第一个数的右边。注意最后一个元素是不取的。

       用空可以表示到结尾和开始

       >>> numbers[-3:]

[8, 9, 10]

>>> numbers[:3]

[1, 2, 3]

 

In [2]: numbers[0:10]

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

 

所有元素:

>>> numbers[:]

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

 

Listing 2-2. Slicing Example

# Split up a URL of the form

url = raw_input('Please enter the URL: ')

domain = url[11:-4]

print "Domain name: " + domain

 

运行结果:

Please enter the URL:

Domain name: python

 

步长设置:

numbers[0:10:2]

[1, 3, 5, 7, 9]

步长不可以为0,但是可以为负,表示逆序操作。

>>> numbers[8:3:-1]

[9, 8, 7, 6, 5]

>>> numbers[10:0:-2]

[10, 8, 6, 4, 2]

 

添加Sequences

同类型的可以直接使用加号相加:

>>> [1, 2, 3] + [4, 5, 6]

[1, 2, 3, 4, 5, 6]

>>> 'Hello, ' + 'world!'

'Hello, world!'

乘法:

>>> 'python' * 5

'pythonpythonpythonpythonpython'

>>> [42] * 10

[42, 42, 42, 42, 42, 42, 42, 42, 42, 42]

 

空值:

>>> sequence = [None] * 10

>>> sequence

[None, None, None, None, None, None, None, None, None, None]

 

实例2-3

Listing 2-3. Sequence (String) Multiplication Example

# Prints a sentence in a centered "box" of correct width

# Note that the integer division operator (//) only works in Python

# 2.2 and newer. In earlier versions, simply use plain division (/)

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-2) + '+'

print ' ' * left_margin + '| ' + ' ' * text_width + ' |'

print ' ' * left_margin + '| ' + sentence + ' |'

print ' ' * left_margin + '| ' + ' ' * text_width + ' |'

print ' ' * left_margin + '+' + '-' * (box_width-2) + '+'

print

 

 

 

 

成员关系:

>>> users = ['mlh', 'foo', 'bar']

>>> raw_input('Enter your user name: ') in users

Enter your user name: mlh

True

 

>>> 'P' in 'Python'

True

 

实例2-4

# Check a user name and PIN code

 

database = [

    ['albert',  '1234'],

    ['dilbert', '4242'],

    ['smith',   '7524'],

    ['jones',   '9843']

]

 

username = raw_input('User name: ')

pin = raw_input('PIN code: ')

 

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

 

长度,最大值、最小值

>>> numbers = [100, 34, 678]

>>> len(numbers)

3

>>> max(numbers)

678

>>> min(numbers)

34

阅读(1459) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~