ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python 常见数据结构

2021-07-10 20:06:14  阅读:169  来源: 互联网

标签:元素 tuple python 常见 list 列表 hogwarts print 数据结构


大纲

  • 列表
  • 元组
  • 集合
  • 字典

列表

定义
Python 中可以通过组合一些值得到多种复合数据类型。
列表是其中最常用得数据结构
列表通过方括号起,逗号分隔的一组值得到
一个列表可以包含不同类型的元素,但通常使用时各个元素类型相同

列表的特性
list.append(x): 在列表的末尾添加一个元素。相当于a[len(a):] = [x].

list.insert(i,x): 在给定的位置插入一个元素。第一个参数是要插入的元素的索引,以a.insert(0,x)插入列表头部,a.insert(len(a),x)等同于a.append(x).

list.remove(x): 移除列表中第一个值为x的元素。如果没有这样的元素,则抛出ValueError异常。

list.pop([i]): 删除列表中给定位置的元素并返回它。如果没有指定位置,a.pop将会删除并返回列表中的最后一个元素、

list.sort(key=None, reverse=Flase): 对列表中的元素进行排序(参数可用于自定义排序,解释请参见 sorted()).

list.reverse(): 反转列表中元素

示例:

list_hogwarts=[1,2,3,5,7,4]
list_hogwarts.append(0)
#list_hogwarts.insert(0,9)
#list_hogwarts.remove(1)
y = list_hogwarts.pop(0)
print(y)
print(list_hogwarts)
list_hogwarts.sort(reverse=True) # True升序 Flase降序
print(list_hogwarts)

list_hogwarts.reverse() # 反转
print(list_hogwarts)

列表的特性
list.clear() 删除列表中所有的元素。相当于del a[:].

list.extend(iterable): 使用可迭代对象中的所有元素来扩展列表。相当于 a[len(a):] = iterable

list.index(x[,start[,end]])

返回列表中第一个值为x的元素的从来零开始的索引。如果没有这样的元素将会抛出ValueError异常

可选参数start和end是切片符号,用于将搜索限制为列表的特定子序列。返回的索引是相对于整个序列的开始计算的,而不是start参数

list.count(x): 返回元素x在列表中出现的次数

list.copy(): 返回列表的一个浅拷贝,相当于a[:]

注意:
insert, remove 或者sort方法,只修改列表,没有打印出返回值—它们返回默认值None,这是python中所有可变数据结构的设计原则
并非所有数据或可以排序或比较(字符串和数字等)

列表推导式
概念:列表推导式提供了一个更简单的创建列表的方法。常见的用法是把某种操作应用于序列或可迭代对象的每个元素上,然后使用其结果来创建列表,或者通过满足某些特定条件元素来创建子序列。

练习:如果我们想生成一个平方列表,比如[1,4,5,…],使用for循环应该怎么写,使用列表生成式又应该怎么写呢?

list_square=[]
for i in range(4):
    list_square.append(i**2)

print(list_square)

list_square2=[ i**2 for i in range(1,4)]
print("list_square2",list_square2)
list_square3=[]
for i in range(1,4):
    if i!=1:
        list_square3.apped(i**2)
print(list_square3)


list_square3=[i**2 for i in range(1,4) if i!=1]
list_square4=[]
for i in range(1,4):
    for j in range(1,4):
        list_square4.append(i*j)


list_square4=[i*j for i in range(1,4) for j in range(1,4)]
print(list_square4)

元组
元组使用()进行定义
tuple,list,range都是序列数据类型
元组是不可变的,可以通过解包,索引来访问

#元组定义
tuple_hogwarts = (1,2,3)
tuple_hogwarts2 = 1,2,3

print("tuple_hogwarts",tuple_hogwarts)
print(type(tuple_hogwarts))

print("tuple_hogwarts2",tuple_hogwarts2)
print(type(tuple_hogwarts2))

元组的不可变特性

list_hogwarts = [1,2,3]
list_hogwarts[0] = "a"
print(list_hogwarts)

tuple_hogwarts = (1,2,3)
tuple_hogwarts[0] = "a"   # 会报错元组,不可修改
tuple_hogwarts = (1,2,a)
print(id(tuple_hogwarts[2]))
tuple_hogwarts[2][0] = "a"  # 嵌套可以修改 
print(id(tuple_hogwarts[2]))
print(tuple_hogwarts) 
a= (1,2,3,"a","a")
print(a.count("a")) # 统计元素出现次数
print(a.index(3)) # 求对应元素的索引

集合
集合是由不重复元素组成的无序的集
它的基本用法包括成员检测和消除重复元素
可以使用{}或者set()函数创建集合
要创建一个空集合只能用set()而不能用{}

a = {1}
b = set()
print(len(b))

print(type(a))
print(type(b))
a = {1,2,3}
b = {1,4,5}

print(a.union(b))
print(a.intersection(b))
print(a.difference(b))

a.add("a")
print(a)
print({i for i in "asdasdafafasfasf"})

c = "dasdasdasdasd"
print(set(c))

字典
字典是以【关键字】为索引
关键字可以是任意不可变类型,通常是字符串或数字。如果一个元组只包含字符串,数字或元组,那么这个元组也可以用作关键字

hogwarts_dict = {"a":1,"b":2}
hogwarts_dict2 = dict(a=1,b=2)
print(" hogwarts_dict", hogwarts_dict)
print(type(hogwarts_dict))
print(" hogwarts_dict2", hogwarts_dict2)
print(type(hogwarts_dict2))
a = {"a":1,"b":2}
b = dict(a=1,b=2)

print(a.keys())
print(a.values())

print(a.pop("a"))
print(a)

print(a.popitem()) # 随机删除键值对
print(a)
a={}
a.fromkeys((1,2,3),"a")
print(b)


print({i : i*2 for i in range(1,3)})

标签:元素,tuple,python,常见,list,列表,hogwarts,print,数据结构
来源: https://blog.csdn.net/qq_26086231/article/details/118639761

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有