300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > python组合数据类型有哪些_Python组合数据类型

python组合数据类型有哪些_Python组合数据类型

时间:2022-04-11 13:40:51

相关推荐

python组合数据类型有哪些_Python组合数据类型

序列类型

字符串(str)

列表(list)

列表则是一个可以修改数据项的序列类型,使用也最灵活

列表(list)是包含0个或多个对象引用的有序序列,属于序列类型。与元组不同,列表的

长度和内容都是可变的,可自由对列表中数据项进行增加、删除或替换。列表没有长度限制,

元素类型可以不同,使用非常灵活。

元组(tuple)

元组是包含0个或多个数据项的不可变序列类型。元组生成后是固定的,其中任何数据项不能替换或删除。

一旦创建就不能被修改

1

2

3creature="cat","dog","tiger","human"

print(creature)

##output:('cat','dog','tiger','human')

元组可以嵌套1

2

3

4

5

6

7

8color=("red",0x001100,"blude",creature)

print(color)

##output:('red', 4352, 'blude', ('cat', 'dog', 'tiger', 'human'))

print(color[2])

print(color[-1][2])

##output:blude

## tiger

元组的解包与组包1

2

3

4

5

6

7

8

9tup0=(1,2,3)

x,y,z=tup0 ##元组解包(x,y,z)=tup0

b=x,y,z ##元组组包b=(x,y,z)

print(tup0)

print(b)

print(x)

##output:(1, 2, 3)

## (1, 2, 3)

## 1

赋值一个元素给元组1

2

3t=0, ##','不可省略

print(type(t))

##output:

赋值多个元素1

2

3

4t=0,1

a,b=t

print(a,b)

##output:0 1

集合类型

集合

集合中元素不可重复,元素类型只能是固定数据类型,例如:整数、浮点数、字符串、元组等,

列表、字典和集合类型本身都是可变数据类型,不能作为集合的元素出现。

由于集合是无序组合,它没有索引和位置的概念,不能分片,集合中元素可以动态增加或删除。

集合用大括号({})表示,可以用赋值语句生成一个集合。

1

2

3

4

5

6s={425,"BIT",(10,"CS"),424}

print(s)

T={425,"bit",(10,"CS"),424,425,"BIT"}

print(T)

##output:{'BIT', 425, (10, 'CS'), 424}

## {424, 425, (10, 'CS'), 'bit', 'BIT'}

set(x)函数

由于集合元素是无序的,集合的打印效果与定义顺序可以不一致。

由于集合元素独一无二,使用集合类型能够过滤掉重复元素。set(x)函数可以用于生成集合。

1

2

3

4

5

6

7

8w=set('apple')

print(w)

v=set(("cat","dog","tiger","human","cat","human"))

print(v)

##output:{'p', 'l', 'e', 'a'}

##output:{'dog', 'cat', 'tiger', 'human'}

映射类型

映射类型是“键-值”数据项的组合,每个元素是一个键值对,即元素是(key, value),键和值通过冒号连接,

不同键值对通过逗号隔开。字典是集合类型的延续,各个元素没有顺序之分元素之间是无序的。

键值对(key, value)是一种二元关系。在python中,映射类型主要以字典(dict)体现。

字典

创建字典1

2

3

4Dnew={} ##创建空字典

Dnew["2*3"]=6 ##添加元素

print(Dnew)

##output:{'2*3': 6}

查找1

2

3Dcountry={"China":"BeiJing","America":"Washington","France":"Paris"}

print(Dcountry["China"])

##output:BeiJing

访问和赋值1

2

3

4Dcountry={"China":"BeiJing","America":"Washington","France":"Paris"}

Dcountry["China"]="Big BeiJing"

print(Dcountry)

##output:{'China': 'Big BeiJing', 'America': 'Washington', 'France': 'Paris'}

增加新元素1

2

3

4Dcountry={"China":"BeiJing","America":"Washington","France":"Paris"}

Dcountry["Britain"]="London"

print(Dcountry)

##output:{'China': 'BeiJing', 'America': 'Washington', 'France': 'Paris', 'Britain': 'London'}

字典类型的操作

字典中的元素以键信息为索引访问

字典是一个键值对的集合,该集合以键为索引,一个键信息只对应一个值信息

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26##.keys() 返回所有的键信息

##.values() 返回所有的值信息

Dcountry={"China":"BeiJing","America":"Washington","France":"Paris"}

print(Dcountry.keys(),Dcountry.values(),sep=":") ##sep,以 ":"为分隔符

##output:dict_keys(['China', 'America', 'France']):dict_values(['BeiJing', 'Washington', 'Paris'])

##.items() 返回所有的键值对

print(Dcountry.items())

##output:dict_items([('China', 'BeiJing'), ('America', 'Washington'), ('France', 'Paris')])

## in 如果键在字典中返回True,否则返回False

print("China" in Dcountry,"BeiJing" in Dcountry,sep=":")

##output:True:False

##.get(,) 键存在则返回相应值,否则返回默认值

print(Dcountry.get("China","GuangZhou"),Dcountry.get("Australia","Sydney"),sep=" ")

##output:BeiJing Sydney

##del[]删除字典中某一键值对

del Dcountry["America"]

print(Dcountry)

##output:{'China': 'BeiJing', 'France': 'Paris'}

##.clear() 删除所有的键值对

print(Dcountry.clear())

##output:None

for..in语句对其元素遍历1

2

3

4

5

6

7

8

9

10

11

12Dcountry={"China":"BeiJing","America":"Washington","France":"Paris"}

for key in Dcountry:

print(ket,end=' ')

##output:China America France

for key in Dcountry:

print(key,Dcountry.get(key),sep=":",end=" ")

##output:China:BeiJing America:Washington France:Paris

for k,v in Dcountry.items():

print(k,v,sep=":",end=" ")

##output:China:BeiJing America:Washington France:Paris

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