欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python Day2

程序员文章站 2024-02-24 15:01:37
...

1. 元组

元组是一种固定长度,不可变的python对象序列。创建元组是用逗号分隔序列值。

In [1]: tup = 3, 4, 5
In [2]: tup
Out[2]: (3, 4, 5)

更复杂的表达式来定义元组时,要用括号包起来,例如:

In [3]: nested_tup = (4, 5, 6), (7, 8)
In [4]: nested_tup
Out[4]: ((4, 5, 6), (7, 8))

也可以使用turple函数将任意序列或迭代器转为元组

In [5]: tuple([4, 0, 2])
Out[5]: (4, 0, 2)

In [6]: tup = tuple('string')

In [8]: tup 
Out[8]: ('s', 't', 'r', 'i', 'n', 'g')

In [9]: tup[0]
Out[9]: 's'

元组一旦被创建,各个位置上的对象是无法被修改的,但如果其中一个对象是可变的(如列表),可以在内部将其修改

In [12]: tup = ('foo'), ([1, 2]), (True)

In [13]: tup[2] = False
Traceback (most recent call last):

  File "<ipython-input-13-b89d0c4ae599>", line 1, in <module>
    tup[2] = False

TypeError: 'tuple' object does not support item assignment

In [16]: tup[1].append(3)

In [17]: tup
Out[17]: ('foo', [1, 2, 3], True)

可以用‘+’生成更长的元组

In [18]: (4, None, 'foo') + (6, 0) + ('bar',)  
		# pls notice that ',' is a must in the concatenated tuple('bar',)
		# otherwise, the output result is shown below.
Out[18]: (4, None, 'foo', 6, 0, 'bar')

In [19]: (4, None, 'foo') + (6, 0) + ('bar')
Traceback (most recent call last):

  File "<ipython-input-19-e573c8aae91e>", line 1, in <module>
    (4, None, 'foo') + (6, 0) + ('bar')

TypeError: can only concatenate tuple (not "str") to tuple

In [20]: (4, None, 'foo') + (6, 0) + tuple('bar')
Out[20]: (4, None, 'foo', 6, 0, 'b', 'a', 'r')

可以将元组乘以整数,生成含有很多copy的元组

In [22]: ('foo','bar') * 4
Out[22]: ('foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'bar')

1)元组拆包
可以将元组型的表达式赋值给变量,Python会对右边的值进行拆包:

In [23]: tup = (4, 5, 6)

In [24]: a, b, c = tup

In [25]: b 
Out[25]: 5

嵌套元组也可以拆包:

In [26]: tup = 4, 5, (6, 7)

In [27]: a, b, (c, d) = tup

In [28]: d
Out[28]: 7

这个功能可以轻易地交换变量名,而不需要temp作为中间变量(tmp = a, a=b, b=temp)

In [29]: a, b = 1,2

In [30]: a
Out[30]: 1

In [31]: b
Out[31]: 2

In [32]: a, b = b, a

In [33]: a
Out[33]: 2

In [34]: b
Out[34]: 1

运用拆包地一个常用场景是遍历元组列表组成的序列

In [39]: seq = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]

In [40]: for a, b, c in seq:
    ...:     print('a = {0}, b = {1}, c = {2}'.format(a, b, c))
    ...:     
a = 1, b = 2, c = 3
a = 4, b = 5, c = 6
a = 7, b = 8, c = 9

In [41]: for a in seq:
    ...:     print('a = {0}'.format(a))
    ...:     
a = (1, 2, 3)
a = (4, 5, 6)
a = (7, 8, 9)

In [42]: for a, b in seq:
    ...:     print('a = {0}, b = {1}'.format(a, b))
    ...:     
Traceback (most recent call last):

  File "<ipython-input-42-0f5653a6f70f>", line 1, in <module>
    for a, b in seq:

ValueError: too many values to unpack (expected 2)

特殊的语法 *rest 可以用于在函数调用时获取任意长度的位置参数列表:

In [43]: values = 1, 2, 3, 4, 5  	#元组类型
In [50]: a, b, *rest, d = values

In [51]: d
Out[51]: 5

In [52]: rest		#list类型
Out[52]: [3, 4]

如果rest不是想要的变量名,则可用下划线来表示

In [53]: a, b, *_, d = values

In [54]: a, b, d
Out[54]: (1, 2, 5)

In [55]: _
Out[55]: [3, 4]

2) 元组方法
由于不可改变的性质,实例运用很少,常见用法有count,用来计算某个数值在元组中出现的次数

In [56]: a = (1, 2, 3, 2, 2, 3, 4, 2)

In [57]: a.count(2)
Out[57]: 4

In [59]: tup
Out[59]: (4, 5, (6, 7))

In [60]: tup = (4, 5, (6, 4, 4, 7))

In [61]: tup.count(4)
Out[61]: 1

In [62]: tup = (4, 5, 'python', [1, 2], 'python')

In [63]: tup.count('python')
Out[63]: 2

2. 列表

列表是一种有序的可变的列表,可以把要存储的东西放进去,也可以访问其中的元素,可以随机访问,也可以通过索引进行线性访问。
list在数据处理中,常用于将迭代器或生成器转化为列表:

In [67]: tup = ('foo', 'bar', 'baz')

In [68]: b_list = list(tup)

In [69]: b_list
Out[69]: ['foo', 'bar', 'baz']
In [64]: gen = range(10)

In [65]: gen
Out[65]: range(0, 10)

In [66]: list(gen)
Out[66]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

1)增加和移除元素
append 可以将元素添加到列表的尾部:

In [70]: b_list.append('dwarf')

In [71]: b_list
Out[71]: ['foo', 'bar', 'baz', 'dwarf']

insert 方法可以将元素插入到指定的列表位置:

In [72]: b_list.insert(1, 'red')

In [73]: b_list
Out[73]: ['foo', 'red', 'bar', 'baz', 'dwarf']

insert的反操作是pop, 可以将特定位置的元素移除并返回:

In [74]: b_list.pop(2)
Out[74]: 'bar'

In [75]: b_list
Out[75]: ['foo', 'red', 'baz', 'dwarf']

remove 可以将第一个符合要求的元素移除:

In [76]: b_list.append('foo')

In [77]: b_list
Out[77]: ['foo', 'red', 'baz', 'dwarf', 'foo']

In [78]: b_list.remove('foo')

In [79]: b_list
Out[79]: ['red', 'baz', 'dwarf', 'foo']

in 和 not in 可以检查关键字是否在列表里,但是和字典,集合相比速度非常缓慢,因为在列表中进行了逐个扫描,但字典和集合是同时扫描所有元素的(哈希表)

In [80]: 'dwarf' in b_list
Out[80]: True

In [81]: 'dwarf' not in b_list
Out[81]: False

2) 连接和联和列表
可以使用 ‘ + ’ 来连接两个列表
如果已有一个定义的列表,可以用extend向列表添加多个元素:

In [87]: [4, None, 'foo'] + [7, 8, (2, 3)]
Out[87]: [4, None, 'foo', 7, 8, (2, 3)]

In [88]: x = [4, None, 'foo']

In [89]: x.extend([7, 8, (2, 3)])

In [90]: x
Out[90]: [4, None, 'foo', 7, 8, (2, 3)]

而append是将一整个[7, 8, (2, 3)]作为一个元素加在x后面

In [94]: x = [4, None, 'foo']

In [95]: x.append([7, 8, (2, 3)])

In [96]: x
Out[96]: [4, None, 'foo', [7, 8, (2, 3)]]

用extend的方法要比 ‘+’的方法实现更快,且不需要创建新的列表。
everthing = []
for chunk in list_of_list:
everything.extend(chunk)
这种表达更好。

3. 字符串string

字符串的表示既可以用单引号(’ '),也可以用双引号(“ ”)。
对于含有换行的多行字符串,可以使用三个单引号或三个双引号:
以下的例子可以计算出有多少个换行(回车符)(三个)

In [97]: c = """
    ...: This is a longer string that
    ...: spans multiple lines
    ...: """

In [98]: c
Out[98]: '\nThis is a longer string that\nspans multiple lines\n'

In [99]: print(c)

This is a longer string that
spans multiple lines

In [100]: c.count("\n")
Out[100]: 3

python字符串是不可变的,不能修改一个字符串

In [101]: a = 'this is a string'

In [102]: a[10] = f
Traceback (most recent call last):

  File "<ipython-input-102-fbaee0a5e345>", line 1, in <module>
    a[10] = f

NameError: name 'f' is not defined

可以通过str函数转化成字符串:

In [103]: a = 3.1415

In [104]: s = str(a)

In [105]: s
Out[105]: '3.1415'

字符串是Unicode字符的序列,因此也可以看成另一种序列:

In [107]: s = 'python'

In [108]: list(s)
Out[108]: ['p', 'y', 't', 'h', 'o', 'n']

In [109]: s[:3]	#这种形式被称为切片
Out[109]: 'pyt'

反斜杠符号\是一种专一符号,用来指明特殊符号,字符串中要写反斜杠也要将其转义;
如果要写大量的反斜杠,可以在前面加一个前缀符号‘ r ’,表明这些字符是原生字符,而不是指明特殊符号:
r = raw

In [111]: s = r'C:\Users\abcd\Documents\work\chapter1'

In [112]: s
Out[112]: 'C:\\Users\\abcd\\Documents\\work\\chapter1'

’ + ’ 也可以将两个字符串结合到一起产生一个新的字符串

In [113]: a = 'this is the first half string'

In [114]: b = 'and this is the second half'

In [115]: a + b
Out[115]: 'this is the first half stringand this is the second half'

字符串格式化指使字符串对象拥有一个format对象

In [120]: template = '{0: .2f} {1:s} are worth US${2:d}'

In [121]: template.format(4.5560, 'Argentine Pesos', 1)
Out[121]: ' 4.56 Argentine Pesos are worth US$1'

其中,
{0: .2f} 表示将第一个参数格式化为两位小数的浮点数
{1:s} 表示第二个参数格式化为字符串
{2:d} 表示第三个参数格式化为整数

Reference:
利用Python进行数据分析, Wes Mackinney著

相关标签: python

上一篇: Python day10

下一篇: python day10