|
File information. 1
序列... 1
字符串... 5
列表... 11
元组... 15
File information
2009-10-22
磁针石:xurongzhong#gmail.com
博客:oychw.cublog.cn
参考资料:
《Python Essential Reference 4thEdition 2009》
《beginning python from novice toprofessional second edition 2008》
序列
主要类型:strings, lists,和tuples。Strings和tuples是固定长度的。列表可以插入、删除、替换。所有序列支持迭代。
另:Unicode strings, bufferobjects, and xrange也算是序列类型。
通用操作有:indexing,区间, 相加, 相乘, 检查成员关系等。
序列的操作和方法:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Checks whether all items in s are True.
|
| Checks whether any item in s is True.
|
可变序列的操作:
|
|
|
|
|
|
| Extended slice assignment
|
|
|
|
|
|
|
索引
索引:类似数组的索引,-1表示最后一个。
>>> fourth = raw_input('Year: ')[3]
注:可以包含其他类型的类型又叫容器,主要为序列(比如列表和元组)和映射(比如字典)。
Indexing实例:
# Print out adate, 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 numberfrom 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 dayto 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
注意示例中的相乘。
区间
包含下限,不包括上限
用空可以表示到结尾和开始
取值时默认俺从左到右,不可逆序。
>>> numbers = [1, 2, 3, 4, 5, 6,7, 8, 9, 10]
>>> numbers[4:6]
[5, 6]
>>> numbers[7:10]
[8, 9, 10]
注意:并没有索引为10的元素,虽然没有错误,为了避免混淆,不建议使用。
>>> numbers[-3:-1]
[8, 9]
>>> numbers[-3:0]
[]
>>> numbers[-3:]
[8, 9, 10]
>>> numbers[:3]
[1, 2, 3]
>>> numbers[:]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
步长设置:
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]
相加
同类型的可以直接使用加号相加:
>>> [1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
>>> 'Hello, ' + 'world!'
'Hello, world!'
不同类型的是不能相加的:
>>>[1, 2, 3] + 'world!'
Traceback(most recent call last):
File "", line 1, in
TypeError:can only concatenate list (not "str") to list
乘法
>>> '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]
成员关系
>>> users = ['mlh', 'foo', 'bar']
>>> raw_input('Enter your username: ') in users
Enter your user name: mlh
True
>>> 'P' in 'Python'
True
长度、最大值、最小值
>>> numbers = [100, 34, 678]
>>> len(numbers)
3
>>> max(numbers)
678
>>> min(numbers)
34
字符串
字符串包含1个或3个单引号或双引号里面。3个引号可以换行,单个引号则不可以。无论何种形式,python内部一般是用单引号表示的。
数值等转换为字符串的方法:
str(),repr(),or format()
s ="The value of x is " + str(x)
s ="The value of x is " + repr(x)
s ="The value of x is " + format(x,"4d")
它们的区别:
>>>x = 3.4
>>>str(x)
'3.4'
>>>repr(x)
'3.3999999999999999'
>>>
>>>format(x,"0.5f")
'3.40000'
>>>
>>>print repr("Hello, world!")
'Hello,world!'
>>>print str("Hello, world!")
Hello,world!
>>>print repr(10000L)
10000L
>>>print str(10000L)
10000
>>>`1L`
'1L'
>>>`"Hello, world!"`
"'Hello,world!'"
Python自动连接在一起的字符串,不过建议还是使用加号。
Str展示的是给用户的外部形式, Repr显示python内部表述的形式。Repr和反引号的效果一样。3.0中已经取消反引号,repr可以继续使用。这里的反引号不同于shell中的。
Python 2有2种字符串类型。比特字符串是8位的。Unicode是16位的,其他不能表示的unicode用surrogate pair表示,占用4个字节。Python有选项编译成用4个字节存储unicode,这样就可以存储所有unicode。
>>>print "The temperature is " + `temp`
The temperatureis 42
原始输入
>>>raw_input("Enter a number: ")
Enter a number:3
'3'
raw_input把输入转换为字符串。
原字符串:
>>>print r'C:\nowhere'
C:\nowhere
原字符串的最后一个字符不能使反斜杠。
>>>u'Hello, world!'
u'Hello, world!'
表示unicode。
|
|
|
|
|
| s.count(sub [,start [,end]])
|
| s.decode([encoding [,errors]])
|
| s.encode([encoding [,errors]])
|
| s.endswith(suffix [,start [,end]])
|
|
|
| s.find(sub [, start [,end]])
|
| s.format(*args, **kwargs)
|
| s.index(sub [, start [,end]])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| s.replace(old, new [,maxreplace])
|
| s.rfind(sub [,start [,end]])
|
| s.rindex(sub [,start [,end]])
|
|
|
|
|
| s.rsplit([sep [,maxsplit]])
|
|
|
| s.split([sep [,maxsplit]])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
如果字符串为空,则所有的判断方法返回False。
s.find(), s.index(), s.rfind(), ands.rindex()。fin找不到的时候返回-1。index()返回ValueError。所有这些方法不支持正则表达式,正则表达式在re模块中。
*短格式格式化
>>> format = "Hello, %s. %senough for ya?"
>>> values = ('world', 'Hot')
>>> print format % values
Hello, world. Hot enough for ya?
注意,除了元组和字典外,其他的序列都解释为单个值。
真正的百分号要用%%表示。
其他类型:
>>>format = "Pi with three decimals: %.3f"
>>> from math import pi
>>> print format % pi
Pi with three decimals: 3.142
另外一种类似shell的格式化方法:TEMPLATE STRINGS
>>> from string import Template
>>> s = Template('$x, glorious$x!')
>>> s.substitute(x='slurm')
>>> s = Template("It's${x}tastic!")
>>> s.substitute(x='slurm')
"It's slurmtastic!"
插入美元符号:
>>>s = Template("Make $$ selling $x!")
>>> s.substitute(x='slurm')
'Make $ selling slurm!'
配合字典的使用:
>>>s = Template('A $thing must never $action.')
>>> d = {}
>>> d['thing'] = 'gentleman'
>>> d['action'] = 'show his socks'
>>> s.substitute(d)
'A gentleman must never show his socks.'
还可以容错替换:safe_substitute,更多参考:(http://python.org/doc/lib/node40.html)
*长格式格式化
-左对齐,+右对齐。其他见参考2第83页。
>>>'Price of eggs: $%d' % 42
'Price of eggs: $42'
>>> 'Hexadecimal price of eggs:%x' % 42
'Hexadecimal price of eggs: 2a'
>>> from math import pi
>>> 'Pi: %f...' % pi
'Pi: 3.141593...'
>>> 'Very inexact estimate of pi:%i' % pi
'Very inexact estimate of pi: 3'
>>> 'Using str: %s' % 42L
'Using str: 42'
>>> 'Using repr: %r' % 42L
'Using repr: 42L'
具体介绍参见教材84页
*字符串方法
更多的方法参见参考2附录B
字符串常量:
string.digits: 数字 0–9
string.letters: 字母(uppercase and lowercase)(Python 3.0,中没有,要使用string.ascii_letters)
string.lowercase: 小写字母
string.printable可打印字符
string.punctuation: 标点符号
string.uppercase: 大写字母
这个东东跟所在区域有关的,比如:string.ascii_letters
-*Find 查找
>>> 'With a moo-moo here, and amoo-moo there'.find('moo')
7
如果没有查找到,返回-1.
>>> subject.find('$$$', 1) # Onlysupplying the start
20
1表示查找的起点。
类似的有:rfind, index, rindex, count, startswith, endswith,见附录
-*join 连接
连接必须是同一类型。
>>> seq = [1, 2, 3, 4, 5]
>>> sep = '+'
>>> sep.join(seq) # Trying to joina list of numbers
Traceback (most recent call last):
File "", line 1, in?
TypeError: sequence item 0: expectedstring, int found
>>>seq = ['1', '2', '3', '4', '5']
>>> sep.join(seq) # Joining a listof strings
'1+2+3+4+5'
>>>dirs = '', 'usr', 'bin', 'env' -- 这种形式实际构成了元组。
>>> '/'.join(dirs)
'/usr/bin/env'
类似的有split
-*lower 小写:
lower:
>>> 'Trondheim HammerDance'.lower()
'trondheim hammer dance'
相关的有: translate.附录B: islower, capitalize, swapcase, title,istitle, upper, isupper.
>>> "that's allfolks".title()
"That'S All, Folks"
与上面类似的功能有:
>>> import string
>>> string.capwords("that'sall, folks")
"That's All, Folks"
-*replace 替换:
找到所有进行替换
>>> 'This is a test'.replace('is','eez')
'Theez eez a test
相关的有: translate.附录B: expandtabs.
-*split切割:
>>> '1+2+3+4+5'.split('+')
['1', '2', '3', '4', '5']
相关的有: translate.附录B: expandtabs.rsplit, splitlines.
-*strip
去掉多余的空格:
>>> '*** SPAM * for * everyone!!!***'.strip(' *!')
'SPAM * for * everyone'
相关的有:附录B: lstrip, rstrip.
-*translate
替换单个字符:translate
translate
针对单个字符,比replace更有效率,可以同时进行几次替换。
要事先建立翻译表,可以考虑使用string模块中的maketrans来建立。后面暂略。
列表
*特点:
列表方法:
|
|
|
|
| Appends a new element, x, to the end of s.
|
| Appends a new list, t, to the end of s.
|
| Counts occurrences of x in s.
| s.index(x [,start [,stop]])
|
|
|
|
|
|
|
|
|
| s.sort([key [, reverse]])
|
|
如果s已经是list,list(s)进行浅拷贝。
s.index(x)报错:ValueError,如果找不到元素。s.remove(x)找不到x也会报这个错。sort() and reverse()都返回None.
names =[ "Dave", "Mark", "Ann", "Phil" ]
附加:names.append("Paula")
插入: names.insert(2,"Thomas")
b =names[0:2] # Returns [ "Jeff", "Mark" ]
c =names[2:] # Returns [ "Thomas", "Ann", "Phil","Paula" ]
names[1]= 'Jeff' # Replace the 2nd item in names with 'Jeff'
names[0:2]= ['Dave','Mark','Jeff'] # Replace the first two items of
# thelist with the list on the right.
连接:a = [1,2,3] + [4,5] # Result is[1,2,3,4,5]
创建空列表:
names =[] # An empty list
names =list() # An empty list
实例1:
importsys
iflen(sys.argv) >> list('Hello')
['H', 'e', 'l', 'l', 'o']
列表转换为字符串
''.join(somelist)
基本的列表操作:
列表的赋值类似C语言中的数组操作。
删除元素:
>>> names = ['Alice', 'Beth','Cecil', 'Dee-Dee', 'Earl']
>>> del names[2]
>>> names
['Alice', 'Beth', 'Dee-Dee', 'Earl']
批量操作:
>>> name = list('Perl')
>>> name
['P', 'e', 'r', 'l']
>>> name[2:] = list('ar')
>>> name
['P', 'e', 'a', 'r']
>>> name = list('Perl')
>>> name[1:] = list('ython')
>>> name
['P', 'y', 't', 'h', 'o', 'n']
可以插入元素:
>>>numbers = [1, 5]
>>> numbers[1:1] = [2, 3, 4]
>>> numbers
[1, 2, 3, 4, 5]
删除元素:
>>> numbers
[1, 2, 3, 4, 5]
>>> numbers[1:4] = []
>>> numbers
[1, 5]
>>> ['to', 'be', 'or', 'not', 'to', 'be'].count('to')
>>> x = [[1, 2], 1, 1, [2, 1, [1, 2]]]
>>> knights = ['We', 'are', 'the', 'knights', 'who', 'say','ni']
>>> numbers = [1, 2, 3, 5, 6, 7]
>>> numbers.insert(3, 'four')
[1, 2, 3, 'four', 5, 6, 7]
>>> x = ['to', 'be', 'or', 'not', 'to', 'be']
['to', 'or', 'not', 'to', 'be']
修改实际列表,无返回值。reversed是有返回值的,不修改实际表。
>>> x = [4, 6, 2, 1, 7, 9]
>>> x = [4, 6, 2, 1, 7, 9]
>>> numbers = [5, 2, 9, 7]
>>> x = ['aardvark', 'abalone', 'acme', 'add', 'aerate']
['add', 'acme', 'aerate', 'abalone', 'aardvark']
>>> x = [4, 6, 2, 1, 7, 9]
更多参考:http://wiki.python.org/moin/HowTo/Sorting
元组
基本上可以吧元组看做不能修改的列表,是列表的简化。
创建:
stock = ('GOOG', 100, 490.10)
address = ('www.python.org', 80)
person = (first_name,last_name, phone)
不加括号也是可以的。
a = () #0-tuple (empty tuple)
b =(item,) # 1-tuple (note the trailing comma)
c =item, # 1-tuple (note the trailing comma)
取值:
name, shares, price = stock
first_name, last_name,phone = person
元组更节约内存
portfolio=[]
for linein open("c:\portfolio.csv"):
fields=line.split()
name=fields[0]
shares=int(fields[1])
price=float(fields[2])
stock=(name,shares,price)
portfolio.append(stock)
>>> 1, 2, 3
(1, 2, 3)
是不能修改的列表
以下第一个不是元组:
>>> 42
42
>>> 42,
(42,)
>>> (42,)
(42,)
>>> 3*(40+2)
126
>>> 3*(40+2,)
(42, 42, 42)
函数:
>>> tuple([1, 2, 3])
(1, 2, 3)
>>> tuple('abc')
('a', 'b', 'c')
>>> tuple((1, 2, 3))
(1, 2, 3)
基本操作:
>>> x = 1, 2, 3
>>> x[1]
2
>>> x[0:2]
(1, 2)
多用于key映射
xrange()
一般用于循环,python 3被range代替了。
xrange一次只产生一个数,在大量循环的时候可以提高效率,一般情况没有明显效果。
|
|