python小白入门4——列表元组
2021-01-14 00:12
标签:xtend style dff Python小白 关系 dex 反向 lap none 一:列表 #索引 #增加 append insert #删 #改 #查 #公共方法: 列表转化成字符串 list -----> str join 二:元组 #元祖 只读列表,可循环查询,可切片。 三:字典 数据类型划分:可变数据类型,不可变数据类型 #增: #删 #改 update #查 #字典嵌套 四:集合 集合:可变的数据类型,他里面的元素必须是不可变的数据类型,无序,不重复。 #增 #删除 #查 #去重 python小白入门4——列表元组 标签:xtend style dff Python小白 关系 dex 反向 lap none 原文地址:https://www.cnblogs.com/huihuangyan/p/12945241.html1 li = [‘alex‘,[1,2,3],‘wusir‘,‘egon‘,‘女神‘,‘taibai‘]
2 l1 = li[0]
3 print(l1)
4 l2 = li[1]
5 print(l2)
6 l3 = li[0:3]
7 print(l3)
1 li.append(‘日天‘)
2 li.append(1)
3 print(li)
4 while 1:
5 username = input(‘>>>‘)
6 if username.strip().upper() == ‘Q‘:
7 break
8 else:
9 li.append(username)
10 print(li)
11 li.insert(4,‘春哥‘)
12 print(li)
13 li.extend(‘二哥‘)
14 li.extend([1,2,3])
15 print(li)
1 li = [‘taibai‘,‘alex‘,‘wusir‘,‘egon‘,‘女神‘,]
2 name = li.pop(1) # 返回值
3 name = li.pop() # 默认删除最后一个
4 print(name,li)
5
6 li.remove(‘taibai‘) # 按元素去删除
7 print(li)
8
9 li.clear() # 清空
10 print(li)
11
12 del li
13 del li[0:2] # 切片去删除
14 print(li)
1 li[0] = ‘男兽‘
2 li[0] = [1,2,3]
3 li[0:3] = ‘云姐plfdslkmgdfjglk‘
4 li[0:3] = [1,2,3,‘春哥‘,‘咸鱼哥‘]
5 print(li)
1 for i in li:
2 print(i)
3 print(li[0:2])
1 l = len(li)
2 print(l)
3 num = li.count(‘taibai‘)
4 print(num)
5 print(li.index(‘wusir‘))
6 li = [1,5,4,7,6,2,3]
7
8 li.sort() #正向排序
9 print(li)
10
11 li.sort(reverse=True) #反向排序
12 print(li)
13
14 li.reverse() #反转
15 print(li)
1 li = [‘taibai‘,‘alex‘,‘wusir‘,‘egon‘,‘女神‘,]
2 s = ‘++++‘.join(li)
#儿子不能改,孙子可能可以改。 1 tu = (1,2,3,‘alex‘,[2,3,4,‘taibai‘],‘egon‘)
2 print(tu[3])
3 print(tu[0:4])
4 for i in tu:
5 print(i)
6 tu[4][3]=tu[4][3].upper()
7 print(tu)
8 tu[4].append(‘sb‘)
9 print(tu)
10 s = ‘alex‘
11 s1 = ‘sb‘.join(s)
12 print(s1)
不可变数据类型:元组,bool int str 可哈希
可变数据类型:list,dict set 不可哈希
dict key 必须是不可变数据类型,可哈希,
value:任意数据类型。
dict 优点:二分查找去查询
存储大量的关系型数据
特点:无序的 1 dic = {
2 ‘name‘:[‘大猛‘,‘小孟‘],
3 ‘py9‘:[{‘num‘:71,‘avg_age‘:18,},
4 {‘num‘: 71, ‘avg_age‘: 18, },
5 {‘num‘: 71, ‘avg_age‘: 18, },
6 ],
7 True:1,
8 (1,2,3):‘wuyiyi‘,
9 2:‘二哥‘,
10 }
11 print(dic)
1 dic1 = {‘age‘: 18, ‘name‘: ‘jin‘, ‘sex‘: ‘male‘,}
2 dic1[‘high‘] = 185 #没有键值对,添加
3 dic1[‘age‘] = 16 #如果有键,则值覆盖
4
5 dic1.setdefault(‘weight‘) # 有键值对,不做任何改变,没有才添加。
6 dic1.setdefault(‘weight‘,150)
7 dic1.setdefault(‘name‘,‘二哥‘)
8 print(dic1)
1 print(dic1.pop(‘age‘)) # 有返回值,按键去删除
2 print(dic1.pop(‘二哥‘,None)) # 可设置返回值
3 print(dic1)
4
5 print(dic1.popitem()) # 随机删除 有返回值 元组里面是删除的键值。
6 print(dic1)
7
8 del dic1[‘name1‘]
9 print(dic1)
10 del dic1
11 print(dic1)
12
13 dic1.clear() #清空字典
1 dic1[‘age‘] = 16
2
3 dic = {"name":"jin","age":18,"sex":"male"}
4 dic2 = {"name":"alex","weight":75}
5 dic2.update(dic) #
6
7 print(dic)
8 print(dic2)
1 print(dic1.keys(),type(dic1.keys()))
2 print(dic1.values())
3 print(dic1.items())
4
5 for i in dic1:
6 print(i)
7 for i in dic1.keys():
8 print(i)
9
10 for i in dic1.values():
11 print(i)
12
13 a,b = 1,2
14 print(a,b)
15
16 a = 1
17 b = 2
18 a,b = b,a
19 print(a,b)
20 a,b = [1,2],[2,3]
21 a,b = (1,2)
22 print(a,b)
23
24 for k,v in dic1.items():
25 print(k,v)
26
27 v1 = dic1[‘name‘]
28 print(v1)
29
30 v2 = dic1[‘name1‘] # 报错
31 print(v2)
32
33 print(dic1.get(‘name1‘,‘没有这个键‘))
1 dic = {
2 ‘name‘:[‘alex‘,‘wusir‘,‘taibai‘],
3 ‘py9‘:{
4 ‘time‘:‘1213‘,
5 ‘learn_money‘:19800,
6 ‘addr‘:‘CBD‘,
7 },
8 ‘age‘:21
9 }
10 dic[‘age‘] = 56
11 print(dic[‘name‘])
12 dic[‘name‘].append(‘ritian‘)
13 l = [1,2,‘wusir‘]
14 l[2] = l[2].upper()
15 dic[‘name‘][1] = dic[‘name‘][1].upper()
16 print(dic)
17 dic[‘py9‘][‘female‘] = 6
18 print(dic)
19
20
21 info = input(‘>>>‘).strip()
22 for i in info:
23 if i.isalpha():
24 info = info.replace(i," ")
25 l = info.split()
26 print(len(l))
1 set1 = set({1,2,3})
2 set2 = {1,2,3,[2,3],{‘name‘:‘alex‘}} # 错的
3 print(set1)
4 print(set2)
5 set1 = {‘alex‘,‘wusir‘,‘ritian‘,‘egon‘,‘barry‘,}
1 #add
2 set1.add(‘女神‘)
3 print(set1)
4 #update
5 set1.update(‘abc‘)
6 print(set1)
1 set1.pop() # 随机删除
2 print(set1.pop()) # 有返回值
3 print(set1)
4
5 set1.remove(‘alex‘) # 按元素
6 print(set1)
7
8 } set()
9 set1.clear()
10 print(set1) # set()
11
12 del set1
13 print(set1)
1 for i in set1:
2 print(i)
3
4 set1 = {1,2,3,4,5}
5 set2 = {4,5,6,7,8}
6 set3 = set1 & set2
7 print(set3) # {4, 5}
8 print(set1.intersection(set2)) # {4, 5}
9
10 set1 = {1,2,3,4,5}
11 set2 = {4,5,6,7,8}
12 print(set1 | set2) # {1, 2, 3, 4, 5, 6, 7,8}
13 print(set2.union(set1)) # {1, 2, 3, 4, 5, 6, 7}
14
15 set1 = {1,2,3,4,5}
16 set2 = {4,5,6,7,8}
17 print(set1 ^ set2) # {1, 2, 3, 6, 7, 8}
18 print(set1.symmetric_difference(set2)) # {1, 2, 3, 6, 7, 8}
19
20 set1 = {1,2,3,4,5}
21 set2 = {4,5,6,7,8}
22 print(set1 - set2) # {1, 2, 3}
23 #set1独有的
24 print(set1.difference(set2)) # {1, 2, 3}
25
26 set1 = {1,2,3,}
27 set2 = {1,2,3,4,5,6}
28
29 print(set1 set2)
30 print(set1.issubset(set2)) # 这两个相同,都是说明set1是set2子集。
31
32 print(set2 > set1)
33 print(set2.issuperset(set1)) # 这两个相同,都是说明set2是set1超集。
1 li = [1,2,33,33,2,1,4,5,6,6]
2 set1 = set(li)
3 # print(set1)
4 li = list(set1)
5 print(li)
6 s1 = {1,2,3}
7 print(s1,type(s1))
8
9 s = frozenset(‘barry‘)
10 print(s,type(s))
11 for i in s:
12 print(i)
上一篇:多线程详解_1
下一篇:python获取http状态码