首页 > 技术文章 > python基础之列表、字典元组

crdhm12040605 2021-08-02 18:19 原文

1.列表

  • List(列表) 是 Python 中使用最频繁的数据类型
  • 列表可以完成大多数集合类的数据结构实现。它支持字符,数字,字符串甚至可以包含列表(即嵌套)
  • 列表用 [ ] 标识,是 python 最通用的复合数据类型

#列表
str1 = ["chenran","帅哥","","aihao"]
str2 = ["hello","python"]
str3 = [1,1,22,33,33,22,444]
print(len(str1))  #打印列表的长度
print(str1.count("帅哥")) #统计列表中成员变量的数量
#运行结果为:
4
1
#######################################################
#查--取值--列表变量名[索引]开始
print(str1[1]) #取列表索引为1的值
print(str1[-1]) #取列表索引最后一个值
print(str1[::-1]) #倒序排序
print(str1.index("帅哥")) #取出str1中”帅哥“索引
#运行结果为:
帅哥
aihao
['aihao', '', '帅哥', 'chenran']
1
#######################################################
#对列表进行新增成员变量
str2.append("hahah") #列表末尾增加新的成员
print(str2)
str2.insert(0,"美女") #在列表索引为0的位置插入成员值
print(str2)
str2.extend(str3) #连接两个列表
print(str2)
#运行结果为:
['hello', 'python', 'hahah']
['美女', 'hello', 'python', 'hahah']
['美女', 'hello', 'python', 'hahah', 1, 1, 22, 33, 33, 22, 444]
######################################################
str3.reverse()  #列表反转
print(str3)
str3.sort() #升序排序
print(str3)
str3.sort(reverse=True) #降序排序
print(str3)
#运行结果为:
[444, 22, 33, 33, 22, 1, 1]
[1, 1, 22, 22, 33, 33, 444]
[444, 33, 33, 22, 22, 1, 1]
######################################################
del str1[1] #删除索引为1的数据
print(str1)
str1.remove("") #删除成员变量为”男“的变量
print(str1)
str2.pop() #删除末尾数据
print(str2)
str2.pop(2) #删除指定索引的数据
print(str2)
#运行结果为:
['chenran', '', 'aihao']
['chenran', 'aihao']
['美女', 'hello', 'python', 'hahah', 1, 1, 22, 33, 33, 22]
['美女', 'hello', 'hahah', 1, 1, 22, 33, 33, 22]

2.集合{}

#列表去重用集合显示
lista=[1,1,22,33,33,22,444]
seta = set(lista) #列表去重
print(seta)
#运行结果为:{1, 444, 22, 33}

3.字符串运算符:

print("*" * 10)
#运行结果为:**********
str1 = "我爱"
str2 = ""
print(str1 + str2)
#运行结果为:我爱你

4.字典:

  • 字典(dictionary)是除列表以外python之中最灵活的内置数据结构类型。列表是有序的对象集合,字典是无序的对象集合。
  • 两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。
  • 字典用"{ }"标识。字典由索引(key)和它对应的值value组成。

 

 

#字典
a = {"name":"chenran","city":20,"爱好":"篮球"}
b = {"friends":"小名","sex":""}
b.update(a) #a字典合并到b字典中
print(b)
#运行结果为:{'friends': '小名', 'sex': '男', 'name': 'chenran', 'city': 20, '爱好': '篮球'}
print(len(b)) #字典键值对的数量
#运行结果为:5
print(b["friends"]) #字典key取值,key不在会报错
#运行结果为:小名
print(b.get("friendss"))#字典key取值,key不在不会报错
#运行结果为:None
print(b.keys()) #字典的所有key
#运行结果为:dict_keys(['friends', 'sex', 'name', 'city', '爱好'])
print(b.values()) #字典的所有value
#运行结果为:dict_values(['小名', '男', 'chenran', 20, '篮球'])
print(b.items()) #字典的所有(key,value)元组列表
#运行结果为:dict_items([('friends', '小名'), ('sex', '男'), ('name', 'chenran'), ('city', 20), ('爱好', '篮球')])
del b["friends"] #指定key删除列表中数据,key不存在会报错
print(b)
#运行结果为:{'sex': '男', 'name': 'chenran', 'city': 20, '爱好': '篮球'}
b.pop("name") #指定key删除列表中数据,key不存在会报错
print(b)
#运行结果为:{'sex': '男', 'city': 20, '爱好': '篮球'}
b.popitem() #随机删除一个键值对
print(b)
#运行结果为:{'sex': '男', 'city': 20}
b.setdefault("房子","gangxu") #随机插入一个键值对
print(b) 
#运行结果为:{'sex': '男', 'city': 20, '房子': 'gangxu'}

5.元组

  • 元组是另一个数据类型,类似于 List(列表)。
  • 元组用 () 标识。内部元素用逗号隔开。但是元组不能二次赋值,相当于只读列表。
tuple = ( 'runoob', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john')
 
print tuple               # 输出完整元组
print tuple[0]            # 输出元组的第一个元素
print tuple[1:3]          # 输出第二个至第四个(不包含)的元素 
print tuple[2:]           # 输出从第三个开始至列表末尾的所有元素
print tinytuple * 2       # 输出元组两次
print tuple + tinytuple   # 打印组合的元组

#运行结果为:
('runoob', 786, 2.23, 'john', 70.2)
runoob
(786, 2.23)
(2.23, 'john', 70.2)
(123, 'john', 123, 'john')
('runoob', 786, 2.23, 'john', 70.2, 123, 'john')

 

 

 

推荐阅读