设为首页 收藏本站
查看: 943|回复: 0

[经验分享] Python 语法之序列

[复制链接]

尚未签到

发表于 2015-12-15 09:48:25 | 显示全部楼层 |阅读模式
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,tuplesStringstuples是固定长度的。列表可以插入、删除、替换。所有序列支持迭代。
另:Unicode strings, bufferobjects, and xrange也算是序列类型。
         通用操作有:indexing,区间, 相加, 相乘, 检查成员关系等
         序列的操作和方法:
项目

功能

s

 

s[i:j]

 

s[i:j:stride]

 

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[i:j] = t

Slice assignment

s[i:j:stride] = t

Extended slice assignment

del s

Item deletion

del s[i:j]

Slice deletion

del s[i:j:stride]

Extended slice deletion

  
  
索引
  
         索引:类似数组的索引,-1表示最后一个。
  
  
>>> fourth = raw_input('Year: ')[3]

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[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!'"
       这样看来还是使用str比较好。

Python自动连接在一起的字符串,不过建议还是使用加号。
Str展示的是给用户的外部形式, Repr显示python内部表述的形式。Repr和反引号的效果一样。3.0中已经取消反引号,repr可以继续使用。这里的反引号不同于shell中的。


         Python 22种字符串类型。比特字符串是8位的。Unicode16位的,其他不能表示的unicodesurrogate 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([encoding [,errors]])

 

s.encode([encoding [,errors]])

 

s.endswith(suffix [,start [,end]])

 

s.expandtabs([tabsize])

 

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([chrs])

 

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([sep [,maxsplit]])

 

s.rstrip([chrs])

 

s.split([sep [,maxsplit]])

 

s.splitlines([keepends])

 

s.startswith(prefix

 

s.strip([chrs])

 

s.swapcase()

 

s.title()

 

s.translate(table

 

s.upper()

 

s.zfill(width)

 

  
  
         如果字符串为空,则所有的判断方法返回False
         s.find(), s.index(), s.rfind(), ands.rindex()fin找不到的时候返回-1index()返回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)


  长格式格式化
         -左对齐,+右对齐。其他见参考283页。
         >>>'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来建立。后面暂略。
  
列表
  
  *特点:
  
  
  
  列表方法:
项目

功能

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([key [, reverse]])

 

  
如果s已经是listlist(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]

列表的方法:

附加:

>>> lst = [1, 2, 3]

>>> lst.append(4)

>>> lst

[1, 2, 3, 4]


统计:

>>> ['to', 'be', 'or', 'not', 'to', 'be'].count('to')

2

>>> x = [[1, 2], 1, 1, [2, 1, [1, 2]]]

>>> x.count(1)

2

>>> x.count([1, 2])

1


扩展:

>>> a = [1, 2, 3]

>>> b = [4, 5, 6]

>>> a.extend(b)

>>> a

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

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


索引:

>>> knights = ['We', 'are', 'the', 'knights', 'who', 'say','ni']

>>> knights.index('who')

4

       如果值不存在,报错:ValueError:

插入:

>>> numbers = [1, 2, 3, 5, 6, 7]

>>> numbers.insert(3, 'four')

>>> numbers

[1, 2, 3, 'four', 5, 6, 7]


出栈;

>>> x = [1, 2, 3]

>>> x.pop()

3

>>> x

[1, 2]

>>> x.pop(0)

1

>>> x

[2]


移除:

>>> x = ['to', 'be', 'or', 'not', 'to', 'be']

>>> x.remove('be')

>>> x

['to', 'or', 'not', 'to', 'be']

只移除第一次碰到的,无返回值。

如果值不存在,报错:ValueError:


翻转:

>>> x = [1, 2, 3]

>>> x.reverse()

>>> x

[3, 2, 1]

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


排序:

>>> x = [4, 6, 2, 1, 7, 9]

>>> x.sort()

>>> x

[1, 2, 4, 6, 7, 9]

无返回值

>>> x = [4, 6, 2, 1, 7, 9]

>>> y = x[:]

>>> y.sort()

>>> x

[4, 6, 2, 1, 7, 9]

>>> y

[1, 2, 4, 6, 7, 9]

注意不要使用,这样指向的是同一个列表。

函数sorted是有返回值的。


高级排序:

       内置函数cmp

>>> cmp(42, 32)

1

>>> cmp(99, 100)

-1

>>> cmp(10, 10)

0

>>> numbers = [5, 2, 9, 7]

>>> numbers.sort(cmp)

>>> numbers

[2, 5, 7, 9]


>>> x = ['aardvark', 'abalone', 'acme', 'add', 'aerate']

>>> x.sort(key=len)

>>> x

['add', 'acme', 'aerate', 'abalone', 'aardvark']



>>> x = [4, 6, 2, 1, 7, 9]

>>> x.sort(reverse=True)

>>> x

[9, 7, 6, 4, 2, 1]


更多参考: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[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 3range代替了。
         xrange一次只产生一个数,在大量循环的时候可以提高效率,一般情况没有明显效果。
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-151389-1-1.html 上篇帖子: python标准库 经典链接 下篇帖子: python中列表的相关函数
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表