码迷,mamicode.com
首页 > 编程语言 > 详细

python 造轮子(一)——序列与字典

时间:2017-02-26 23:50:04      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:技术分享   重复元素   play   指定   exp   rem   高级   max   print   

虽然说造轮子很少用了,什么底层东西很少写,但是还是很想学扎实,还是好多东西还是的会,没有底层的支持,比较高级的库学起来还是很困难的。

 

  • 序列的普遍用法:
技术分享
 1 #-*-coding:utf8-*-
 2 
 3 #索引
 4 l = [1,2,3,4]
 5 t = (1,2,3,4)
 6 d = {1:1,2:2,3:3,4:4}
 7 
 8 
 9 print l[0]
10 print t[0]
11 print d[1]  #键索引
12 
13 #切片
14 
15 print l[0:5]
16 print t[0:5]
17 
18 
19 #
20 print l + [5]
21 print t + (5,)
22 
23 #
24 print l*2
25 print t*2
26 
27 #in
28 if 1 in l:
29     print yes
30 if 1 in t:
31     print yes
32 if 1 in d:
33     print yes
34 
35 
36 #len
37 print len(l)
38 print len(t)
39 print len(d)
40 
41 
42 #min(seq)  max  sum
43 print min(l)
44 print max(t)
45 print sum(l)
View Code

 

  • 列表的用法:
技术分享
  1 #-*-coding:utf8-*-
  2 
  3 #列表的使用
  4 
  5 #列表的创建
  6 a = [a,b,c]
  7 
  8 #列表的访问
  9 print a[0]
 10 
 11 #修改
 12 a[0] = 1
 13 print a
 14 
 15 #增加元素
 16 
 17 ‘‘‘
 18 使用+ 在列表后面加一个列表
 19 ‘‘‘
 20 
 21 a = a + [d]
 22 
 23 print a
 24 
 25 
 26 ‘‘‘
 27 使用append()在列表尾部假如一个元素
 28 ‘‘‘
 29 
 30 a.append(e)
 31 print a
 32 
 33 ‘‘‘
 34 使用extend()在一个列表后面加一个列表
 35 ‘‘‘
 36 
 37 a.extend([f])
 38 print a
 39 
 40 
 41 ‘‘‘
 42 使用insert(id,x)在id位置加一个元素x‘‘‘
 43 
 44 a.insert(0,3)
 45 print a
 46 
 47 #检索元素
 48 ‘‘‘
 49 使用count(x)检查列表中x元素出现的次数
 50 ‘‘‘
 51 print a.count(1)
 52 print a.count(2)
 53 
 54 ‘‘‘
 55 使用in是否在列表里面
 56 ‘‘‘
 57 
 58 if 3 in a:
 59     print yes
 60 
 61 
 62 #删除元素
 63 ‘‘‘
 64 使用pop(id)弹出指定位置的元素,没有参数默认弹出最后一个
 65 ‘‘‘
 66 print a.pop()
 67 print a
 68 
 69 print a.pop(0)
 70 print a
 71 
 72 ‘‘‘
 73 使用del list[id]删除指定位置的元素
 74 ‘‘‘
 75 del a[0]
 76 print a
 77 
 78 ‘‘‘
 79 使用remove(x)删除某个特定值的元素(只会删掉一个)
 80 ‘‘‘
 81 
 82 a.append(e)
 83 print a
 84 
 85 a.remove(e)
 86 print a
 87 
 88 a.remove(e)
 89 print a
 90 
 91 
 92 #index(x)返回x的下标
 93 print a.index(b)
 94 
 95 #reverse()反转列表
 96 a.reverse()
 97 print a
 98 
 99 
100 #sort()排序列表
101 a.sort()
102 print a
View Code

 

  • 元组的用法:
技术分享
 1 #-*-coding:utf8-*-
 2 
 3 #元组操作
 4 
 5 #创建
 6 tup = (a,b,2017)
 7 
 8 #访问元组
 9 print tup[0]
10 print tup[:3]
11 
12 #元组不能增加,没有append,extend函数,但可以用+ 连接
13 tuptmp = (2,23)
14 tup = tup + tuptmp
15 print tup
16 
17 
18 #del 删除整个元组
19 #del tup
20 #print tup
21 
22 
23 #len 长度
24 print len(tup)
25 
26 
27 print tup*4
28 
29 
30 if a in tup:
31     print yes
View Code

 

  • 字典的用法:
技术分享
 1 #-*-coding:utf8-*-
 2 
 3 #字典操作
 4 d = {"tree":18,"id":2015,"dress":"happy"}
 5 
 6 print d["tree"]
 7 
 8 #keys()返回所有键
 9 print d.keys()
10 
11 #values()返回所有值
12 print d.values()
13 
14 
15 #has_kay()是否有某一键
16 print d.has_key("tree")
17 
18 
19 #get(x) 根据键x,返回对应的值,不存在返回None
20 print d.get("tree")
21 
22 
23 #items() 返回一个由(key,value)组成的元组
24 print d.items()
25 
26 
27 #字典元素的删除
28 ‘‘‘
29 del删除键对应的元素
30 ‘‘‘
31 
32 del d["tree"]
33 print d
34 
35 
36 ‘‘‘
37 pop(key)删除key对应的元素
38 ‘‘‘
39 
40 d.pop("id")
41 print d
42 
43 ‘‘‘
44 clear()删除全部元素
45 ‘‘‘
46 d.clear()
47 print d
48 
49 
50 #update方法 d1.update(d2),用d2去合并d1,覆盖掉d1中间的重复元素
51 d2 = {"wo":"you"}
52 d.update(d2)
53 print d
54 
55 
56 #in 键是否在字典里
57 print "wo" in d
View Code

 

  • 字符串的用法:
技术分享
 1 #-*-coding:utf8-*-
 2 
 3 #字符串操作
 4 
 5 s = "Python"
 6 
 7 #index(‘x‘) 返回x的下标
 8 print s.index(P)
 9 
10 
11 #find(‘x‘) 返回x的下标,找不到返回-1
12 print s.find(s)
13 
14 #replace(x,y) 把x换成y(全部替换掉)
15 print s.replace(P,a)
16 
17 s = "PPython"
18 print s.replace(P,A)
19 
20 #split(‘x‘)分割字符串,无参数以空格分割
21 
22 s = "I study Python"
23 print s.split()
View Code

 

  • 列表,元组,字符串转换:
技术分享
 1 #-*-coding:utf8-*-
 2 
 3 #list()字符串转为列表
 4 
 5 strs = "strings"
 6 print list(strs)
 7 
 8 
 9 #tuple()字符串转成元组
10 
11 print tuple(strs)
12 
13 
14 #列表、元组转成字符串,用join方法
15 
16 s = [h,a,p,p,y,str(1)]
17 
18 print "".join(list(s))
19 
20 
21 s = (h,a,p,p,y,str(1))
22 print "".join(tuple(s))
View Code

 

python 造轮子(一)——序列与字典

标签:技术分享   重复元素   play   指定   exp   rem   高级   max   print   

原文地址:http://www.cnblogs.com/TreeDream/p/6457909.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!