gaojinguan 发表于 2015-12-15 09:48:25

Python 语法之序列

Python 语法之序列
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,区间, 相加, 相乘, 检查成员关系等。
         序列的操作和方法:
项目
功能
s
 
s
 
s
 
len(s)
 
min(s)
 
max(s)
 
sum(s [,initial])
Sum of items in s
all(s)
Checks whether all items in s are True.
any(s)
Checks whether any item in s is True.
  
  可变序列的操作:
项目
功能
s = v
Item assignment
s = t
Slice assignment
s = t
Extended slice assignment
del s
Item deletion
del s
Slice deletion
del s
Extended slice deletion
  
  
索引
  
         索引:类似数组的索引,-1表示最后一个。
  
  
>>> fourth = raw_input('Year: ')
Year: 2005
>>> fourth
'5'
  
         注:可以包含其他类型的类型又叫容器,主要为序列(比如列表和元组)和映射(比如字典)。
  
  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
  ordinal = day + endings
  
  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 =
  >>> numbers
  
  >>> numbers
  
  注意:并没有索引为10的元素,虽然没有错误,为了避免混淆,不建议使用。
  >>> numbers[-3:-1]
  
  >>> numbers[-3:0]
  []
  >>> numbers[-3:]
  
  >>> numbers[:3]
  
  >>> numbers[:]
  
  
  步长设置:
  numbers
  
  步长不可以为0,但是可以为负,表示逆序操作。
  >>> numbers
  
  >>> numbers
  
  
相加
  同类型的可以直接使用加号相加:
  >>> +
  
  >>> 'Hello, ' + 'world!'
  'Hello, world!'
  不同类型的是不能相加的:
  >>> + 'world!'
  Traceback(most recent call last):
  File "", line 1, in
  TypeError:can only concatenate list (not "str") to list
  
乘法
  >>> 'python' * 5
  'pythonpythonpythonpythonpython'
  >>> * 10
  
  
  空值
  >>> sequence = * 10
  >>> sequence
  
  
  
成员关系
  >>> users = ['mlh', 'foo', 'bar']
  >>> raw_input('Enter your username: ') in users
  Enter your user name: mlh
  True
  
  >>> 'P' in 'Python'
  True
  
  
长度、最大值、最小值
  >>> numbers =
  >>> 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!'"
       这样看来还是使用str比较好。
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.capitalize()
 
s.center(width [, pad])
 
s.count(sub [,start [,end]])
 
s.decode(])
 
s.encode(])
 
s.endswith(suffix [,start [,end]])
 
s.expandtabs()
 
s.find(sub [, start [,end]])
 
s.format(*args, **kwargs)
 
s.index(sub [, start [,end]])
 
s.isalnum()
 
s.isalpha()
 
s.isdigit()
 
s.islower()
 
s.isspace()
 
s.istitle()
 
s.isupper()
 
s.join(t)
 
s.ljust(width [, fill])
 
s.lower()
 
s.lstrip()
 
s.partition(sep)
 
s.replace(old, new [,maxreplace])
 
s.rfind(sub [,start [,end]])
 
s.rindex(sub [,start [,end]])
 
s.rjust(width [, fill])
 
s.rpartition(sep)
 
s.rsplit(])
 
s.rstrip()
 
s.split(])
 
s.splitlines()
 
s.startswith(prefix
 
s.strip()
 
s.swapcase()
 
s.title()
 
s.translate(table
 
s.upper()
 
s.zfill(width)
 
  
  
         如果字符串为空,则所有的判断方法返回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 =
  >>> 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来建立。后面暂略。
  
列表
  
  *特点:
  
  
  
  列表方法:
项目
功能
list(s)
Converts s to a list.
s.append(x)
Appends a new element, x, to the end of s.
s.extend(t)
Appends a new list, t, to the end of s.
s.count(x)
Counts occurrences of x in s.
s.index(x [,start [,stop]])
 
s.insert(i,x)
 
s.pop()
 
s.remove(x)
 
s.reverse()
 
s.sort(])
 
  
如果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 # Returns [ "Jeff", "Mark" ]
  c =names # Returns [ "Thomas", "Ann", "Phil","Paula" ]
  names= 'Jeff' # Replace the 2nd item in names with 'Jeff'
  names= ['Dave','Mark','Jeff'] # Replace the first two items of
  # thelist with the list on the right.
  
  连接:a = + # Result is
  创建空列表:
  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
  >>> names
  ['Alice', 'Beth', 'Dee-Dee', 'Earl']
  
  批量操作:
  >>> name = list('Perl')
  >>> name
  ['P', 'e', 'r', 'l']
  >>> name = list('ar')
  >>> name
  ['P', 'e', 'a', 'r']
  
  >>> name = list('Perl')
  >>> name = list('ython')
  >>> name
  ['P', 'y', 't', 'h', 'o', 'n']
  
         可以插入元素:
         >>>numbers =
  >>> numbers =
  >>> numbers
  
         删除元素:
  >>> numbers
  
  >>> numbers = []
  >>> numbers
  

列表的方法:
附加:
>>> lst =
>>> lst.append(4)
>>> lst


统计:
>>> ['to', 'be', 'or', 'not', 'to', 'be'].count('to')
2
>>> x = [, 1, 1, ]]
>>> x.count(1)
2
>>> x.count()
1

扩展:
>>> a =
>>> b =
>>> a.extend(b)
>>> a

与连接类似,不过扩展返回的是新列表。

索引:
>>> knights = ['We', 'are', 'the', 'knights', 'who', 'say','ni']
>>> knights.index('who')
4
       如果值不存在,报错:ValueError:
插入:
>>> numbers =
>>> numbers.insert(3, 'four')
>>> numbers


出栈;
>>> x =
>>> x.pop()
3
>>> x

>>> x.pop(0)
1
>>> x


移除:
>>> x = ['to', 'be', 'or', 'not', 'to', 'be']
>>> x.remove('be')
>>> x
['to', 'or', 'not', 'to', 'be']
只移除第一次碰到的,无返回值。
如果值不存在,报错:ValueError:

翻转:
>>> x =
>>> x.reverse()
>>> x

修改实际列表,无返回值。reversed是有返回值的,不修改实际表。

排序:
>>> x =
>>> x.sort()
>>> x

无返回值
>>> x =
>>> y = x[:]
>>> y.sort()
>>> x

>>> y

注意不要使用,这样指向的是同一个列表。
函数sorted是有返回值的。

高级排序:
       内置函数cmp
>>> cmp(42, 32)
1
>>> cmp(99, 100)
-1
>>> cmp(10, 10)
0
>>> numbers =
>>> numbers.sort(cmp)
>>> numbers


>>> x = ['aardvark', 'abalone', 'acme', 'add', 'aerate']
>>> x.sort(key=len)
>>> x
['add', 'acme', 'aerate', 'abalone', 'aardvark']


>>> x =
>>> x.sort(reverse=True)
>>> x


更多参考: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
host, port = address
  first_name, last_name,phone = person
  
  元组更节约内存
  portfolio=[]
  for linein open("c:\portfolio.csv"):
      fields=line.split()
      name=fields
      shares=int(fields)
      price=float(fields)
      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)
  >>> tuple('abc')
  ('a', 'b', 'c')
  >>> tuple((1, 2, 3))
  (1, 2, 3)
  
  基本操作:
  >>> x = 1, 2, 3
  >>> x
  2
  >>> x
  (1, 2)
  
  多用于key映射
  
  
xrange()
         一般用于循环,python 3被range代替了。
         xrange一次只产生一个数,在大量循环的时候可以提高效率,一般情况没有明显效果。
  
页: [1]
查看完整版本: Python 语法之序列