300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > python中元组和列表转化_4.Python列表/元组/集合/字典

python中元组和列表转化_4.Python列表/元组/集合/字典

时间:2023-02-17 12:19:50

相关推荐

python中元组和列表转化_4.Python列表/元组/集合/字典

4.1 Python列表

• 列表用 [ ] 标识,是Python 最通用的复合数据类型。

• 列表用 [ ] 表示,列表具有可嵌套性

4.1.1 Python列表截取

• 列表可以使用 [头下标:尾下标] 截取相应的子列表, 从左到右索引默认 0 开始,从右到左索引默认-1开始

4.1.2 Python列表截取与更新

• 列表操作(索引)

4.1.3 Python更新列表

• 可以对列表的数据项进行修改或更新,也可以使用append()方法来添加列表项。

4.1.4 Python删除列表元素

• 可以使用 del 语句来删除列表的的元素。

4.1.5 Python列表操作符

4.1.6 Python列表函数

4.1.7 Python列表方法

4.1.8 Python列表切片

• 列表操作(切片slicing ,用索引截取某一段列表)

• 加号+是列表连接运算符,星号*是重复操作。例如:

4.2 Python元组

• 元组是一种复合数据类型,类似于List(列表),元组用()”标识,内部元素用逗号隔开。但是元组不能二次赋值,相当于只读列表。

• Tuples(元组、序列)

• Tuples操作

4.2.1 Python元组截取

4.2.2 Python元组不允许二次赋值

• Python元组不允许更新,而列表是允许更新的。以下对元组的操作是无效的。

4.2.3 元组内置函数

4.3 Python集合(Set)

• 集合(set)是由一个或数个形态各异的大小整体组成的

• 可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是{ },因为 { } 是用来创建一个空字典。

• 创建集合格式:

• 或者

4.4 Python字典

• 字典是一种映射类型,字典用“{}”标识,它是一个无序的键(key) : 值(value)对的集合。键(key)必须使用不可变类型。在同一个字典中,键(key)必须是唯一的。

• 字典是无序的对象集合。

• Dictionaries (Key-Value型Map)

4.5 Python序列

• 序列(sequence),在Python中是一种具有相同特性的高级数据结构的统称

• 序列的通用操作:

① 通过索引来获取元素值

② 分片操作

③ 通过+合并元素

④ 通过*来复制元素

⑤ 支持成员运算符

⑥ 最大值、最小值和长度函数支持

4.6 Python数据类型转换

• 对数据内置的类型进行转换,只需要将数据类型作为函数名即可。

4.7 实验

In:

ls1 = ['a,',['b',['c','d']]]

In:

id(ls1)

out:

2078516844424

In:

ls1 = ['c','d']

In:

id(ls1)

out:

2078505627016

In:

ls1

out:

['a,', ['b', ['c', 'd']]]

In:

ls1[1][1][1]

out:

'd'

In:

a1 = 1

In:

id(a1)

out:

140727695483280

In:

a1 = 2

In:

id(a1)

out:

140727695483312

In:

ls1[0] = 'f'ls1

out:

['f', ['b', ['c', 'd']]]

In:

id(ls1)

out:

2078516829704

In:

ls1.append(['h','g'])

In:

id(ls1)

out:

2078516844424

In:

ls1

out:

['f', ['b', ['c', 'd']], ['h', 'g']]

In:

del ls1[0]ls1

out:

[['b', ['c', 'd']], ['h', 'g']]

In:

ls2 = ['q','e']ls1 + ls2

out:

[['b', ['c', 'd']], ['h', 'g'], 'q', 'e']

In:

'q' in ls2

out:

True

In:

for x in ls2:print(x, end=' ')

out:

q e

In:

list((1,2))

out:

[1, 2]

In:

ls1.extend(ls2)ls1

out:

[['b', ['c', 'd']], ['h', 'g'], 'q', 'e', 'q', 'e', 'q', 'e']

In:

ls1.insert(0,'z')

In:

ls1

out:

['z', ['b', ['c', 'd']], ['h', 'g'], 'q', 'e', 'q', 'e', 'q', 'e']

In:

ls1.pop()ls1

out:

['z', ['b', ['c', 'd']], ['h', 'g'], 'q', 'e', 'q', 'e']

In:

ls1.remove('z')

In:

ls1

out:

[['b', ['c', 'd']], ['h', 'g'], 'q', 'e', 'q', 'e']

In:

ls1.reverse()

In:

ls1

out:

['e', 'q', 'e', 'q', ['h', 'g'], ['b', ['c', 'd']]]

In:

ls2.sort()

In:

ls2.sort(reverse=True)

In:

# ls1.sort() #嵌套类型不能用

In:

ls2

out:

['q', 'e']

In:

ls2.append('e')ls2

out:

['q', 'e', 'e']

4.7.1 元组

In:

tu1 = ('a',1,['c','d'])

In:

tu1[0] = 'e'

out:

---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-38-75e55e2a1153> in <module>----> 1 tu1[0] = 'e'TypeError: 'tuple' object does not support item assignment

4.7.2 集合

不能重复set()无序

In:

set([1,1,2])

out:

{1, 2}

In:

a = set([1,2,3])b = set([4,2,3])

In:

a - b

out:

{1}

In:

a & b

out:

{2, 3}

In:

a | b

out:

{1, 2, 3, 4}

In:

a ^ b

out:

{1, 4}

4.7.3 字典

In:

dic1 = {'a':1,'b':2,'c':3}

In:

dic1['c']

out:

3

In:

#新增dic1['d'] = 4dic1

out:

{'a': 1, 'b': 2, 'c': 3, 'd': 4}

In:

s1 = None# null, nan

In:

dic1['e']

out:

---------------------------------------------------------------------------KeyError Traceback (most recent call last)<ipython-input-61-898ad45657b2> in <module>----> 1 dic1['e']KeyError: 'e'

In:

dic1.get('e',None)

In:

dic1.keys()

out:

dict_keys(['a', 'b', 'c', 'd'])

In:

for key in dic1.keys():print(dic1[key])

out:

1234

In:

dic1.values()

out:

dict_values([1, 2, 3, 4])

In:

dic1.items()

out:

dict_items([('a', 1), ('b', 2), ('c', 3), ('d', 4)])

In:

for key,val in dic1.items():print(key + "|" +str(val))

out:

a|1b|2c|3d|4

In:

lis1= ['a',1,['c','d']]

In:

a1,a2,a3 = lis1

In:

print(a1,a2,a3)

out:

a 1 ['c', 'd']

无序字典

In:

dic3 = {'a':1,'b':2}dic4 = {'b':2,'a':1}dic3 == dic4

out:

True

有序字典

In:

from collections import OrderedDictdic11 = OrderedDict()dic11['a'] = 1; dic11['b'] =2

In:

dic12 = OrderedDict()dic12['b'] = 2; dic12['a'] =1dic11 == dic12

out:

False

In:

print(dic11)

out:

OrderedDict([('a', 1), ('b', 2), ('c', 0), (1, 3)])

In:

dic11.

实验

在一个字典中保存了股票的代码和价格,找出股价大于100元的股票并创建一个新的字典。

说明:可以用字典的生成式语法来创建这个新字典。

In:

stocks = {'AAPL': 191.88,'GOOG': 1186.96,'IBM': 149.24,'ORCL': 48.44,'ACN': 166.89,'FB': 208.09,'SYMC': 21.29}

In:

tar_dic = {}for key,val in stocks.items():if val > 100:tar_dic[key] = val

In:

tar_dic

out:

{'AAPL': 191.88, 'GOOG': 1186.96, 'IBM': 149.24, 'ACN': 166.89, 'FB': 208.09}

In:

tar_dic2 = {key:val for key,val in stocks.items() if val > 100}

In:

tar_dic2

out:

{'AAPL': 191.88, 'GOOG': 1186.96, 'IBM': 149.24, 'ACN': 166.89, 'FB': 208.09}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。