免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2001 | 回复: 0
打印 上一主题 下一主题

《python 从入门到精通》§2 列表和数组 §2.2通用的Seq [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-16 09:24 |只看该作者 |倒序浏览

               

  Normal
  0
  
  7.8 磅
  0
  2
  
  false
  false
  false
  
   
   
   
   
   
   
   
   
   
   
   
   
  
  MicrosoftInternetExplorer4



/* Style Definitions */
table.MsoNormalTable
        {mso-style-name:普通表格;
        mso-tstyle-rowband-size:0;
        mso-tstyle-colband-size:0;
        mso-style-noshow:yes;
        mso-style-parent:"";
        mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
        mso-para-margin:0cm;
        mso-para-margin-bottom:.0001pt;
        mso-pagination:widow-orphan;
        font-size:10.0pt;
        font-family:"Times New Roman";
        mso-fareast-font-family:"Times New Roman";
        mso-ansi-language:#0400;
        mso-fareast-language:#0400;
        mso-bidi-language:#0400;}
§2    列表和数组
2009-8-16
磁针石:xurongzhong#gmail.com
博客:
oychw.cublog.cn
§2.1 Sequence概述
Sequence的种类:lists and tuples,strings ,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]
'http://www.python.org'
>>> 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
http://www.something.com
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

步长设置:
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
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/21908/showart_2029126.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP