python 学习笔记5------序列

本文深入探讨了Python中序列连接、重复、切片操作,以及字符串的切片、连接、内置函数、字符串内建函数等内容。同时,介绍了字符串与Unicode的区别、列表与元组的区别、浅拷贝和深拷贝的概念,并提供了实例分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、连接两个序列

>>> a = [1, 3, 4]
>>> b = [4, 5, 6]
>>> a + b

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

>>> a.extend(b)

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

>>> a.append(b)

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

上面的实验,采用了+、extend、append三种方法,其中+和expend功能一样,将两个序列合并为一个序列,但是extend效率会高一些。append则将b作为a的一个元素


2、重复序列

>>> b
[4, 5, 6]
>>> b*3
[4, 5, 6, 4, 5, 6, 4, 5, 6]


3、序列切片

  • 0 ~ len-1或者-len ~ -1,序列下表可以有两个维度,正索引和负索引
  • 切片操作是左闭右开区间
  • 第1或第2个参数如果不写,则表示最开始或到最结束
  • 第3个参数表示步长
例子如下:

>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> len(a)
10

>>> a[0]
0

>>> a[-10]
0

>>> a[1:]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[:1]
[0]

>>> a[0:9:2]>>>>>>>>>步长操作
[0, 2, 4, 6, 8]

>>> a[::-1]>>>>>>>>>>翻转操作
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

>>> a[::2]>>>>>>>>>>隔一个取一个
[0, 2, 4, 6, 8]

>>> a[::3]
[0, 3, 6, 9]

>>> range(0, 10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(0, 10, 2)
[0, 2, 4, 6, 8]
>>> range(-1, -10, -1)
[-1, -2, -3, -4, -5, -6, -7, -8, -9]


4、for else语句:

如果在for语句中,没有被break出来,else语句的内容将被执行


5、字符串操作:

  • 切片,和序列切片是一样一样的,不赘述了
  • in 和 not in
  • string 模块,有些有用的字符串常量,string.letters,string.digits等等。
  • 字符串连接+, join,split的使用。看下面例子。
  • ‘、“、’‘’区别,参见http://blog.youkuaiyun.com/beginning1126/article/details/17334891
  • 格式化操作符%,print "%d, %s" % (12, 12)
  • cmp,按照字符串的ASCII码进行比较,参数1小于参数2,则返回-1,否则返回1
  • max, min返回字符串序列ASCII码最大和最小字符
  • enumerate,
  • zip,合并两个list,形成元组list
例子:
>>> a = ['11', '22', '33']
>>> a
['11', '22', '33']
>>> ' '.join(a)
'11 22 33'
>>> b = '+'.join(a)
>>> b
'11+22+33'
>>> c = b.split('+')
>>> c
['11', '22', '33']
=============================
>>> str1 = 'abc'
>>> str2 = 'xyz'
>>> cmp(str1, str2)
-1
>>> cmp(str2, str1)
1
==============================
>>> s = 'abcdef'
>>> for i, t in enumerate(s):
...     print i, t
...
0 a
1 b
2 c
3 d
4 e
5 f
==============================
>>> s = '123'
>>> t = 'abc'
>>> zip(s,t)
[('1', 'a'), ('2', 'b'), ('3', 'c')]
==============================

6、字符串内建函数




7、str与unicode
8、列表函数
help(list),可以看到详细说明,这里不再赘述。

9、元组和列表的区别
  • 元组是只读的,这点和字符串很像,不能修改元组某一个元素,也不能把元组切片作为左值;
  • 要修改元组,只能重新创建新的元组;
  • 连接操作 +,重复操作 *,仍然是可以的;
  • 如果元组里,某个元素是列表,ok,这个列表是可以直接通过访问下标直接改变的;
  • return 1, 2, 3,返回的这个东西,会默认为什么类型变量呢,没错,元组,所有多对象的,用逗号分隔的,没有明确定义的,都默认为元组;
  • 单元素元组,(1),返回的是int型1,不是元组,只有定义成(1,)才会返回元组;
  • 什么时候会用到元组呢,一个不可变的列表,难道不觉得奇怪吗,如果想把某个参数传给某个陌生api,为了防止此api改变参数,在传入之前,强转成元组是个好方法。
10、浅拷贝和深拷贝
person = ['name', ['savings', 100.00]]
hubby = person[:] # slice copy
wifey = list(person) # fac func copy


print [id(x) for x in person, hubby, wifey]
print [id(x) for x in person]
print [id(x) for x in hubby]
print [id(x) for x in wifey]
print person, hubby, wifey


print "==========================================="


hubby[0] = 'joe'
wifey[0] = 'jane'
hubby[1][1] = 50.00
print [id(x) for x in person, hubby, wifey]
print [id(x) for x in person]
print [id(x) for x in hubby]
print [id(x) for x in wifey]
print person, hubby, wifey


print "==========================================="


import copy
person = ['name', ['savings', 100.00]]
hubby = copy.deepcopy(person)
wifey = copy.deepcopy(person)


print [id(x) for x in person, hubby, wifey]
print [id(x) for x in person]
print [id(x) for x in hubby]
print [id(x) for x in wifey]
print person, hubby, wifey


print "==========================================="


hubby[0] = 'joe'
wifey[0] = 'jane'
hubby[1][1] = 50.00
print [id(x) for x in person, hubby, wifey]
print [id(x) for x in person]
print [id(x) for x in hubby]
print [id(x) for x in wifey]
print person, hubby, wifey


运行结果:
[12277880, 12309448, 12311808]
[11191456, 12299360]
[11191456, 12299360]
[11191456, 12299360]
['name', ['savings', 100.0]] ['name', ['savings', 100.0]] ['name', ['savings', 100.0]]
===========================================
[12277880, 12309448, 12311808]
[11191456, 12299360]
[12510560, 12299360]
[12294208, 12299360]
['name', ['savings', 50.0]] ['joe', ['savings', 50.0]] ['jane', ['savings', 50.0]]
===========================================
[12349968, 12277880, 12309448]
[11191456, 12349928]
[11191456, 12350248]
[11191456, 12350328]
['name', ['savings', 100.0]] ['name', ['savings', 100.0]] ['name', ['savings', 100.0]]
===========================================
[12349968, 12277880, 12309448]
[11191456, 12349928]
[12510560, 12350248]
[12294208, 12350328]
['name', ['savings', 100.0]] ['joe', ['savings', 50.0]] ['jane', ['savings', 100.0]]

结果分析:
(1)完全切片操作[:],(2)利用工厂函数,比如list(),dict()等,(3)使用copy 模块的copy 函数.是属于浅拷贝,所以第一个用例当第二个参数hubby发生变化时,wifey也跟着变,随之而来的问题是,第一个参数name变化时,为啥wifey不跟着变呢。原因是第一个参数是字符串属于只读的,改变其值时,其实已经重新创建了对象。用例利用了copy模块的deepcopy,采用深拷贝,这样hubby和wifey就是完全不同的两个变量。



序列函数总结:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值