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

Python开发【第三篇】基本数据类型

时间:2018-08-13 23:46:22      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:移除   特殊   sed   move   配套   不可变类   obj   expect   wap   

整型 int

__author__ = Tang

# 将字符串转换为数字
a = "123"
b = int(a)
print(b) # 123

# 前面是0的数转换,默认base按照十进制
a = "0011"
b = int(a)
print(b) # 11

# 通过base,按照指定进制转换为十进制
a = "0011"
b = int(a,base=16)
c = int(a,base=8)
print(b) # 17
print(c) # 9

# bit_length() 表示这个数字的二进制至少用几位来表示
a = 15 # 1111
b = 16 # 10000
c = a.bit_length()
d = b.bit_length()
print(c) # 4
print(d)# 5

# a = "0b11"
# b = int(a)
# print(b) # 报错

# a = "123a"
# b = int(a)
# print(b) # 报错

 

字符串 str

技术分享图片
  1 __author__ = "Tang"
  2 
  3 """不要指望一下全背下来"""
  4 """必会的:join split find strip upper lower replace"""
  5 
  6 # capitalize() 首字母大写
  7 a = tang
  8 b = a.capitalize()
  9 print(b) # Tang
 10 
 11 # casefold() 大写变小写
 12 a = TANg
 13 b = a.casefold()
 14 print(b) # tang
 15 
 16 # lower() 大写变小写
 17 a = TANg
 18 b = a.lower()
 19 print(b) # tang
 20 
 21 """
 22 casefold() & lower() 的比较
 23 1.都能大写变小写
 24 2.casefold 比较全面,像一些特殊语言的变换都可以
 25 3.lower 只能处理英文转换
 26 """
 27 
 28 # center() 字符串放中间
 29 # center(self, width, fillchar=None) width=总长度 fillchar=填充符号(一个字符)
 30 a = tang
 31 b = a.center(20)
 32 c = a.center(20,*)
 33 d = a.center(20,)
 34 print(b) #         tang
 35 print(c) # ********tang********
 36 print(d) # 唐唐唐唐唐唐唐唐tang唐唐唐唐唐唐唐唐
 37 
 38 # count() 统计次数
 39 # count(self, sub, start=None, end=None) sub=要统计的字符 start=开始位置 end=结尾位置 以0开始计数 [start,end)
 40 a = tanglaoertanglaoer
 41 b = a.count(a)
 42 c = a.count(ao)
 43 d = a.count(a,5,-1)
 44 e = a.count(r,0,-1)
 45 f = a.count(r,0)
 46 print(b) # 4
 47 print(c) # 2
 48 print(d) # 3
 49 print(e) # 1
 50 print(f) # 2
 51 
 52 # endswith() 判断以什么结尾
 53 # endswith(self, suffix, start=None, end=None) suffix=要判断的字符 start=开始位置 end=结尾位置 以0开始计数 [start,end)
 54 a = tanglaoer
 55 b = a.endswith(er)
 56 print(b) # True
 57 
 58 # expandtabs() 每次取tabsize个,里面有\t的补后面的空格
 59 # expandtabs(self, tabsize=8)
 60 a = tanglaoer\t # 这里补三个空格 第一次取tangla   第二次取oer\t
 61 b = a.expandtabs(6)
 62 print(b) # tanglaoer | | |
 63 print(len(b)) #12
 64 
 65 # find() 寻找 返回所在位置,未找到返回-1
 66 # find(self, sub, start=None, end=None) sub=要查找的字符 start=开始位置 end=结尾位置 以0开始计数 [start,end)
 67 a = tanglaoer
 68 b = a.find(a)
 69 c = a.find(a,3)
 70 e = a.find(p,3)
 71 print(b)# 1
 72 print(c)# 5
 73 print(e)# -1
 74 
 75 # format 格式化,占位符替换为指定的值
 76 # format(self, *args, **kwargs)
 77 a = i am {name}
 78 print(a) # i am {name}
 79 b = a.format(name=tang)
 80 print(b) # i am tang
 81 
 82 a = i am {0},age {1}
 83 print(a) # i am {0},age {1}
 84 b = a.format(tang,22)
 85 print(b) # i am tang,age 22
 86 
 87 # format_map() 格式化
 88 # format_map(self, mapping) mapping=字典{}
 89 a = i am {name}
 90 b = a.format_map({name:tang}) # 传字典
 91 print(b) # i am tang
 92 
 93 # index() 寻找,找不到就报错
 94 # index(self, sub, start=None, end=None) sub=要查找的字符 start=开始位置 end=结尾位置 以0开始计数 [start,end)
 95 # a = ‘tanglaoer‘
 96 # b = a.index(‘a‘)
 97 # c = a.index(‘p‘)
 98 # print(b) # 1
 99 # print(c) # 报错
100 
101 """
102 find() & index() 的比较
103 1. 用find() 找不到不会报错
104 """
105 
106 # isalnum() 字符串中是否只包含 字母(包括汉字)和数字
107 a = tang8080唐
108 b = tang8080_
109 c = a.isalnum()
110 d = b.isalnum()
111 print(c) # True
112 print(d) # False
113 
114 # isalpha() 字符串是否只包含字母 汉字
115 a = "tanglaoer"
116 b = 
117 c = a.isalpha()
118 d = b.isalpha()
119 print(c) # True
120 print(d) # True
121 
122 # isdigest() isdecimal() 判断是否是数字
123 a = "123"
124 b = ""
125 t = 
126 c = a.isdecimal() # 只可以识别 这种"123" 用来判断计算就用这种
127 d = a.isdigit()
128 e = b.isdecimal()
129 f = b.isdigit()
130 g = t.isnumeric() # 可以识别中文数字
131 print(c,d,e,f,g) # True True False True True
132 """
133 isdigit() & isdecimal() 比较
134 1. 用isdigit() 范围更广
135 """
136 
137 # swapcase() 大小写转换
138 a = TangLaoEr
139 b = a.swapcase()
140 print(b) # tANGlAOeR
141 
142 # isidentifier() 判断是否是标识符
143 # 数字 字母 下划线 and 字母 下划线 开头
144 
145 
146 # isprintable() 判断是否存在不可显示字符 \n \t ,注意:空格是可显示的
147 a = "tang\tlao"
148 b = a.isprintable()
149 print(b) # False
150 
151 # isspace() 判断是否全部是空格,\t \n 都满足
152 a = "tanglaoer"
153 b = " "
154 t = "\t"
155 p = "\n"
156 c = a.isspace()
157 d = b.isspace()
158 g = t.isspace()
159 pp = p.isspace()
160 print(c,d,g,pp) # False True True True
161 
162 # istitle() 判断是否是标题 每个单词首字母大写
163 a = tang lao er
164 b = a.istitle()
165 c = a.title()
166 print(b)# False
167 print(c)# Tang Lao Er
168 
169 # 这个很重要
170 # join() 拼接字符串 每一个元素按照指定分隔符进行拼接
171 a = "tanglaoer"
172 c = 飞流直下三千尺
173 print(a)
174 b = " ".join(a)
175 d = "_".join(c)
176 print(b) # t a n g l a o e r
177 print(d) # 飞_流_直_下_三_千_尺
178 
179 # ljust() rjust() 指定宽度,字符串放一边 剩下的填充
180 # ljust(self, width, fillchar=None)
181 # rjust(self, width, fillchar=None)
182 a = tang
183 b = a.ljust(20,*)
184 c = a.rjust(20,*)
185 print(b) # tang****************
186 print(c) # ****************tang
187 
188 # zfill() 以0来填充
189 a = "tang"
190 b = a.zfill(20)
191 print(b) # 0000000000000000tang
192 
193 # islower() 判断是否是小写
194 a = Tang
195 b = a.islower()
196 print(b) # False
197 
198 # isupper() 判断是否是大写
199 a = Tang
200 b = a.isupper()
201 print(b) # False
202 
203 # upper() 转换为大写
204 a = Tang
205 b = a.upper()
206 print(b) # TANG
207 
208 # 移除指定字符串
209 #strip() 默认去除空格 \n \t ,可以指定匹配的子序列
210 # strip(self, chars=None)
211 a = " tang "
212 b = a.lstrip()
213 c = a.rstrip()
214 d = a.strip()
215 print(b)#tang
216 print(c)# tang
217 print(d)#tang
218 
219 # 注意:是子序列里面的都可以匹配掉
220 a = axtangxa
221 b = a.lstrip(ax)
222 c = a.rstrip(ax)
223 d = a.strip(ax)
224 print(b)#tangxa
225 print(c)#axtang
226 print(d)#tang
227 
228 # maketrans() translate() 自定义替换,两个函数配套使用
229 st = "ajketjaeoijmdgajtyopayjhpaeg"
230 a = str.maketrans("aeiou","12345") # a=1,e=2,i=3,o=4,u=5
231 at = st.translate(a)
232 print(at) # 1jk2tj1243jmdg1jty4p1yjhp12g
233 
234 # partition() 分割,分成三部分成元组,可以拿到sep
235 # partition(self, sep)
236 a = tanglaoer
237 b = a.partition(a)
238 c = a.partition(ao)
239 d = a.partition(r)
240 print(b) # (‘t‘, ‘a‘, ‘nglaoer‘)
241 print(c) # (‘tangl‘, ‘ao‘, ‘er‘)
242 print(d) # (‘tanglaoe‘, ‘r‘, ‘‘)
243 
244 # split() lsplit() rsplit() # 分割,按sep分割成列表,不可以拿到sep
245 # split(self, sep=None, maxsplit=-1) maxsplit=分割次数
246 a = tanglaoer
247 b = a.split(a)
248 c = a.rsplit(a,1)
249 print(b) # [‘t‘, ‘ngl‘, ‘oer‘]
250 print(c) # [‘tangl‘, ‘oer‘]
251 
252 # splitlines() 分割
253 # splitlines(self, keepends=None) keepends=True | False
254 a = "aldgjal\najlkgjal\nalgladjg"
255 b = a.splitlines(False)
256 c = a.splitlines(True)
257 print(b) # [‘aldgjal‘, ‘ajlkgjal‘, ‘algladjg‘]
258 print(c) # [‘aldgjal\n‘, ‘ajlkgjal\n‘, ‘algladjg‘]
259 
260 # startswith() 以什么开头
261 # startswith(self, prefix, start=None, end=None)
262 a = "tanglaoer"
263 b = a.startswith(a)
264 c = a.startswith(a,1)
265 print(b) # False
266 print(c) # True
267 
268 # replace() 替换
269 # replace(self, old, new, count=None) count=替换的次数
270 a = "tanglaoer"
271 b = a.replace("a","T")
272 c = a.replace("a","T",1)
273 print(b) #tTnglToer
274 print(c) #tTnglaoer
275 
276 # ******************************以下必须知道的***************
277 # 获取字符串中的某一个
278 # 通过索引,下标
279 a = tanglaoer
280 b = a[2]
281 print(b) # n
282 
283 # 通过[M:n]切片 -1:表示最后一个
284 a = tanglaoer
285 b = a[0:1] #[0,1)
286 c = a[0:-1] #[0,-1)
287 print(b) # t
288 print(c) # tanglaoe
289 
290 # len() 求长度
291 a = tanglaoer
292 c = 唐老二
293 b = len(a)
294 d = len(c)
295 print(b) # 9
296 print(d) # 3 注意: 在python2中拿到的是9
297 
298 # for 循环取值
299 a = tanglaoer
300 for i in a:
301     print(i,end= ) #t a n g l a o e r
302 print()
303 
304 # 字符串拼接
305 """
306 内存理解: 字符串是不可变类型
307 1.首先a申请一块内存存放"tanglaoer"
308 2.b申请一块内存存放"18"
309 3.c重新申请一块内存放"tanglaoer18"
310 """
311 a = "tanglaoer"
312 b = "18"
313 c = a + b
314 print(c) # tanglaoer18
View Code

布尔值 bool

a = bool(12)
print(a) # True

a = True
print(a) # True

a = False
print(a) # False

# 为False的值
# "" () [] {} 0 None

 

 

列表 list

技术分享图片
__author__ = "Tang"

# 列表中的元素 数字,字符串,列表,布尔值...
li = [1, age, [tang, [lao, er]], True]

# 索引取值
print(li[1])  # age
print(li[2][0])  # tang

# 切片 取值
print(li[2:-1])  # [[‘tang‘, [‘lao‘, ‘er‘]]]

# for循环 while 循环 取值
# for item in li:
#     print(item)

# 列表元素,可以被修改 是可变类型
li = [1, age, [tang, [lao, er]], True]
# 索引修改
li[0] = 20
print(li[0])

#  删除
del li[1]
print(li)  # [20, [‘tang‘, [‘lao‘, ‘er‘]], True]

# 切片 修改多个值
li = [1, 12, 9, age, [tang, [lao, er]], True]
li[1:3] = [120, 90]
print(li)  # [1, 120, 90, ‘age‘, [‘tang‘, [‘lao‘, ‘er‘]], True]

# 切片 删除多个值
li = [1, 12, 9, age, [tang, [lao, er]], True]
del li[2:6]
print(li)  # [1, 12]

# in 操作
li = [1, 12, 9, age, [tang, [lao, er]], True]
v = "age" in li
print(v)  # True

v = tang in li
print(v)  # False

v = [tang, [lao, er]] in li
print(v)  # True

# 取值操作
li = [1, 12, 9, age, [tang, [lao, er]], True]
print(li[3][1])  # g
print(li[4][1][0])  # lao

# 数字 字符串转换
a = "123"
s = int(a)
print(s, type(s))  # 123 <class ‘int‘>

a = 123
s = str(a)
print(s, type(s))  # 123 <class ‘str‘>

# 字符串转换成列表
s = "tanglaoer"
new_li = list(s)  # 内部使用for循环
print(new_li)  # [‘t‘, ‘a‘, ‘n‘, ‘g‘, ‘l‘, ‘a‘, ‘o‘, ‘e‘, ‘r‘]

# 列表转换成字符串
li = [11, 22, 33, "123", "tang", True]
r = str(li)
print(r)  # [11, 22, 33, ‘123‘, ‘tang‘, True]

# 列表转换成字符串 第一种 失败
li = [11, 22, 33, "123", "tang", True]
s = ""
li = str(li)  # 每一个字符是一个元素 11就代表两个元素 包括[]号也是元素
for i in li:
    print(i)
    """
    [
1
1
,

2
2
,

3
3
,

‘
1
2
3
‘
,

‘
t
a
n
g
‘
,

T
r
u
e
]
"""
    s += i
print(s)  # [11, 22, 33, ‘123‘, ‘tang‘, True]

# 列表转换成字符串 第二种 有点麻烦
li = [11, 22, 33, "123", "tang", True]
s = ""
for i in li:
    s = s + str(i)
print(s)  # 112233123tangTrue

# 列表转换成字符串 第三种 完美
li = [11, 22, 33, "123", "tang", True]

# 因为列表里面有整型 不能直接转换为字符串
# s = "".join(li) # 报错 TypeError: sequence item 0: expected str instance, int found
s = "".join(%s % i for i in li)
print(s)  # 112233123tangTrue

# 这个很重要 列表 元组 字符串 使用切片【M:N】取值 超出边界不报错
# 如果是根据【n】索引取值,就会报错
# li = [1,2,3,4,5,6,7]
# tt = li[10:]
# print(tt) # []
# print(tt[10]) # 报错

# st = "tanglaoer"
# st2 = st[10:]
# st3 = st[1:]
# print(st2) # 空格
# print(st3) # anglaoer
# print(st[20]) 报错

# tup = (1,2,3,4,5,)
# tup2 = tup[10:]
# tup3 = tup[10]
# print(tup2) # 空格
# print(tup3) # 报错

##############list 方法#################

# append() 追加
li = [11, 22, 33, 44]
v = li.append(55)
print(v)  # None
print(li)  # [11, 22, 33, 44, 55]

# clear() 清空
li = [11, 22, 33, 44]
v = li.clear()
print(v)  # None
print(li)  # []

# copy() 浅拷贝
li = [11, 22, 33, 44]
v = li.copy()
print(v)  # [11, 22, 33, 44]
print(li)  # [11, 22, 33, 44]

# count() 统计
li = [11, 22, 33, 44]
v = li.count(22)
print(v)  # 1
print(li)  # [11, 22, 33, 44]

# extend() 扩展
# extend(self, iterable) iterable=可迭代对象(字符串 列表 元组)
li = [11, 22, 33, 44]
v = li.extend([a, b, c, 90, 50, tang])
v2 = li.extend(tanglaoer)
print(v)  # None
print(li)  # [11, 22, 33, 44, ‘a‘, ‘b‘, ‘c‘, 90, 50, ‘tang‘, ‘t‘, ‘a‘, ‘n‘, ‘g‘, ‘l‘, ‘a‘, ‘o‘, ‘e‘, ‘r‘]
print(v2)  # None

"""
append() & extend() 的区别:
1. append() 参数如果是可迭代对象,不会循环取值,而是当做整体插入
2. extend() 循环取值,一个一个的插入
"""

# index() 寻找索引
# index(self, value, start=None, stop=None)
li = [11, 22, 33, 22, 44]
v = li.index(22)
v2 = li.index(22, 2)
print(v)  # 1
print(v2)  # 3

# insert() 在指定位置插入值
# insert(self, index, p_object)
li = [11, 22, 33, 44]
v = li.insert(3, 22)
print(v)  # None
print(li)  # [11, 22, 33, 22, 44]

# pop() 删除某索引并返回其位置的值 默认删除最后一个
# pop(self, index=None)
li = [11, 22, 33, 44]
v = li.pop(2)
v2 = li.pop()
print(v)  # 33
print(v2) # 44
print(li)  # [11, 22]

# remove() 删除某一个值
# remove(self, value)  左边优先
li = [11, 22, 33, 44]
v = li.remove(22)
print(v)  # None
print(li)  # [11, 33, 44]

# del() 删除
li = [11, 22, 33, 44]
del li[0]
print(li)  # [22, 33, 44]
del li[0:1]
print(li) # [33, 44]

"""
pop() & remove() & clear() & del 
1. pop() 参数为索引下标
2. remove() 参数为值
3. clear() 清空所有
4. del 删除某指定的位置,也可以切片
"""

# reverse() 将当前列表进行反转
li = [11, 22, 33, 44]
v = li.reverse()
print(v) # None
print(li) # [44, 33, 22, 11]

# sort() 排序
# sort(self, key=None, reverse=False) 默认从小到大排序
li = [11, 22, 33, 44]
v = li.sort()
print(v) # None
print(li) # [11, 22, 33, 44]

v2 = li.sort(reverse=True) # 从大到小排序
print(v2) # None
print(li) # [44, 33, 22, 11]

# 查看地址
li = [11, 22, 33, 44]
print(li ,id(li)) # [11, 22, 33, 44] 50110832

li.append(55)
print(li,id(li)) # [11, 22, 33, 44, 55] 50110832

# 列表是可变类型
li = [11, 22, 33, 44]
print(li[0]) # 11
li[0] = 99
print(li[0]) # 99

# 字符串是不可变类型
s = "tanglaoer"
s[0] = B
print(s[0]) # 报错
View Code

元组 tuple

技术分享图片
__author__ = "Tang"

# 元组 元素不可被修改
tu = (111, 22, 33, 44,)
# tu[0] = 999 # 报错
# del tu[0] # 报错
print(tu)  # (111, 22, 33, 44)

# 在最后多加一个逗号 区别于方法 整型 不加也不报错
l = (11)
print(l)  # 11  整型

l2 = (11,)
print(l2)  # (11,) 元组

tu = (111, tang, (1, 2, 3), [4, 5, 6], True, 45)
print(tu)  # (111, ‘tang‘, (1, 2, 3), [4, 5, 6], True, 45)

# 索引取值
tu = (111, tang, (1, 2, 3), [4, 5, 6], True, 45)
v = tu[0]
print(v) # 111

# 切片取值
tu = (111, tang, (1, 2, 3), [4, 5, 6], True, 45)
v = tu[0:3]
print(v) # (111, ‘tang‘, (1, 2, 3))

# 循环取值 是可迭代对象
# tu = (111, ‘tang‘, (1, 2, 3), [4, 5, 6], True, 45)
# for i in tu:
#     print(i)
"""
111
tang
(1, 2, 3)
[4, 5, 6]
True
45
"""

# 元组转列表
tu = (111, tang, (1, 2, 3), [4, 5, 6], True, 45)
tu = list(tu)
print(tu) # [111, ‘tang‘, (1, 2, 3), [4, 5, 6], True, 45]

# 列表转元组
li = [111, tang, (1, 2, 3), [4, 5, 6], True, 45]
tu = tuple(li)
print(tu) # (111, ‘tang‘, (1, 2, 3), [4, 5, 6], True, 45)

# 字符串转元组
s = "tanglaoer"
tu = tuple(s)
print(tu) # (‘t‘, ‘a‘, ‘n‘, ‘g‘, ‘l‘, ‘a‘, ‘o‘, ‘e‘, ‘r‘)


# 元组转字符串第一种 元组不仅仅是字符串 只能自己写for循环
tu = (111, tang, (1, 2, 3), [4, 5, 6], True, 45)
v = "".join("%s"%str(i) for i in tu)
print(v,type(v)) # 111tang(1, 2, 3)[4, 5, 6]True45 <class ‘str‘>


# 元组转字符串第二种 元组都是字符串
tu = (tang,lao,er,)
v = "".join(tu)
print(v) # tanglaoer

# 元组 列表都是有序的
tu = (111, tang, (1, 2, 3), [4, 5, 6], True, 45)
v = tu[2][2]
print(v) # 3

# 元组里面的一级元素不可以修改/删除/增加  二级元素可以修改
tu = (111, tang, (1, 2, 3), [4, 5, 6,(33,44)], True, 45)
print(tu[3][0]) # 4
tu[3][0] = 14
print(tu[3][0]) # 14

print(tu[3][3]) # (33, 44)
tu[3][3] = 8989
print(tu[3][3]) # 8989


##########################tuple  方法##########

# count() 获取指定元素的个数
tu = (111, tang, (1, 2, 3), [4, 5, 6,(33,44)], True, 45, 45,65,45)
v = tu.count(45)
print(v) # 3

# index() 获取指定元素的索引下标
# index(self, value, start=None, stop=None)
tu = (111, tang, (1, 2, 3), [4, 5, 6,(33,44)], True, 45, 45,65,45)
v = tu.index(45)
print(v) # 5
View Code

字典 dict

技术分享图片
__author__ = "Tang"

# 键值对 dict
a = {
    a: 1,
    b: 2,
    c: 3
}
print(a)  # {‘a‘: 1, ‘b‘: 2, ‘c‘: 3}

info = {
    "key0": True,
    "key1": 10,
    "key2": 20,
    "key3": 30,
    "key4": [11, 22, 33],
    "key5": (44, 55, 66,),
    "key6": {k0: v0, k1: v1, k2: v2}
}
print(info)
"""
{‘key0‘: True, ‘key1‘: 10, ‘key2‘: 20, ‘key3‘: 30, ‘key4‘: [11, 22, 33], ‘key5‘: (44, 55, 66), ‘key6‘: {‘k0‘: ‘v0‘, ‘k1‘: ‘v1‘, ‘k2‘: ‘v2‘}}
"""
# 通过上面例子可以知道 value 可以是任意类型
# 字典的key 可以是整型 字符串 元组 布尔 而列表跟字典可变数据类型不可以当key
tt = {
    2: 1,
    st: st,
    True: "123",
    (11, 22,): (11, 22)
}
print(tt)  # {2: ‘1‘, ‘st‘: ‘st‘, True: ‘123‘, (11, 22): (11, 22)}

# 字典是无序的 不可以通过索引取值 切片
# dt = {‘a‘: 1, ‘b‘: 2, ‘c‘: 3}
# print(dt[1]) # 报错

# 通过key取值
dt = {a: 1, b: 2, c: 3}
print(dt[a])  # 1

#  删除
dt = {a: 1, b: 2, c: 3}
del dt[a]
print(dt)  # {‘b‘: 2, ‘c‘: 3}

# 循环取值
info = {
    "key0": True,
    "key1": 10,
    "key2": 20,
    "key3": 30,
    "key4": [11, 22, 33],
    "key5": (44, 55, 66,),
    "key6": {k0: v0, k1: v1, k2: v2}
}
for item in info:  # 默认是取键
    print(item)
    """
key0
key1
key2
key3
key4
key5
key6
    """

for item in info.keys():  # 取键
    print(item)

for item in info.values():  # 取值
    print(item)

for k, v in info.items():  # 取键 & 值
    print(k, v)

# 字典的键要唯一 如果重复只显示第一个 注意:True=1 False=0
test = {1: 1, True: 1}
print(test)  # {1: ‘1‘}

test = {2: 1, True: 1}
print(test)  # {2: ‘1‘, True: ‘1‘}

################# dict 的方法
# fromkeys() 类的静态方法
# fromkeys(*args, **kwargs)
v = dict.fromkeys([11, 22, 33])
print(v)  # {11: None, 22: None, 33: None}

v = dict.fromkeys([11, 22, 33], [a, b])
print(v)  # {11: [‘a‘, ‘b‘], 22: [‘a‘, ‘b‘], 33: [‘a‘, ‘b‘]}

# 通过索引取值,如果键不存在就报错
# 用get函数取值,不存在不报错,并且可以指定默认值
# get() 取值
dt = {a: 1, b: 2, c: 3}
v = dt.get(a,99)
print(v) # 1

v = dt.get(z,99) # 如果不存在,取第二个参数的值
print(v) # 99

# pop() 删除
dt = {a: 1, b: 2, c: 3}
v = dt.pop(a,99)
print(v) # 1

v = dt.pop(z,99) # 键不存在 得到默认值
print(v) # 99

# popitem() 随机删除一个
dt = {a: 1, b: 2, c: 3}
v = dt.popitem() # 删除并返回一个元组
print(v) # (‘c‘, 3)

k,v = dt.popitem()
print(k,v) # b 2

# setdefault() 键已经存在,不设置并获取已存在的值,键不存在,就设置并取当前设置的值
dt = {a: 1, b: 2, c: 3}
v = dt.setdefault(a,10)
print(v) # 1
print(dt) # {‘a‘: 1, ‘b‘: 2, ‘c‘: 3}

v = dt.setdefault(z,10)
print(v) # 10
print(dt) # {‘a‘: 1, ‘b‘: 2, ‘c‘: 3, ‘z‘: ‘10‘}

# update() 更新
dt = {a: 1, b: 2, c: 3}
dt.update({a:10,z:100})
print(dt) # {‘a‘: ‘10‘, ‘b‘: 2, ‘c‘: 3, ‘z‘: ‘100‘}

dt.update(k=123,k2=456,k3="789")
print(dt) # {‘a‘: ‘10‘, ‘b‘: 2, ‘c‘: 3, ‘z‘: ‘100‘, ‘k‘: 123, ‘k2‘: 456, ‘k3‘: ‘789‘}

"""
必会: keys() values() get() update() items()
"""
View Code

 

整理常用的

__author__ = "Tang"

"""常用的"""

# 整型
# int()

# 字符串
# replace/find/join/strip/startswith/split/upper/lower/format
tempate = "my name is {name},hobby is {hobby}"
v = tempate.format(name=tang,hobby=游戏)
print(v) # my name is tang,hobby is 游戏

v = tempate.format(**{name:tang,hobby:游戏})
print(v) # my name is tang,hobby is 游戏


# 列表
# append/extend/insert
# 索引、切片、循环 取值

# 元组
# count/index

# 字典
# get/update/keys/values/items/fromkeys

 

Python开发【第三篇】基本数据类型

标签:移除   特殊   sed   move   配套   不可变类   obj   expect   wap   

原文地址:https://www.cnblogs.com/tangkaishou/p/9464355.html

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