标签:取值 split none 对组 pwd key值 账户 ini 反转
1 #!user/bin/env python 2 # -*- coding:utf-8 -*-练习 3 # test.expandtab 4 # 基本知识点编程(20) is to produce a table 5 # test = "username\temail\tpassword\nlaiying\tlying@qq.com\t123\nlaiying\tlying@qq.com\t123\n" 6 # v = test.expandtabs(20) 7 # print(v) 8 9 # test1 = ‘abc123‘ 10 # test2 = ‘456‘ 11 # test3 = ‘abc‘ 12 # test4 = ‘二‘ 13 # v1 = test1.isalpha() #to judge if all the inputs are words 14 # v2 = test2.isdecimal() #to judge if all the inputs are decimal number 15 # v3 = test3.isdigit() #to judge if all the inputs are number, this method includes specialised signs 16 # v4 = test4.isnumeric() #it can identifise chinese number, such as "二" 17 # print(v1) 18 # print(v2) 19 # print(v3) 20 # print(v4) 21 22 # transformation from lower words to upper words or from upper words to lower words 23 # test = "aLEx" 24 # v1 = test.lower() 25 # v2 = test.upper() 26 # v3 = test.swapcase() 27 # print(v1,v2,v3,v4) 28 29 # identifiers nr, letters,underline def class 30 # a = ‘123‘ #the first letter cannot initiate with nr, but it can be defined by letters or underline 31 # b = ‘abc123‘ 32 # v1 = a.isidentifier() 33 # v2 = b.isidentifier() 34 # print(v1) 35 # print(v2) 36 37 # replace 38 # test = "alexalexalexalex" 39 # test1 = test.replace("ex", ‘bbb‘,2) 40 # print(test1) 41 42 # 将文字对应的索引打印出来 43 # 法1 44 # test = input(">>>") 45 # print(test) 46 # l = len(test) 47 # print(l) 48 # r = range(0,l) 49 # for item in r: 50 # print(item, test[item]) 51 # 法2 52 # test = input(">>>") 53 # for item in range(0,len(test)): 54 # print(item, test[item]) 55 56 # 6.元组的一级元素不可修改/删除/增加 57 # tu = (111,"alex",(11,22),[(33,44)],True,33,44,) 58 # # 元组,有序。 59 # v = tu[3][0][0] 60 # print(v) 61 # v=tu[3] 62 # print(v) 63 # tu[3][0] = 567 64 # print(tu) 65 66 # 字典由一系列键值对组成,键对值由key和value值组成,key值不能是Ture,列表,字典;value可以嵌套出现。 67 68 # info = { 69 # "k1": 18, 70 # "k2": True, 71 # "k3": [ 72 # 11, 73 # [], 74 # (), 75 # 22, 76 # 33, 77 # { 78 # "kk1": "vv1", 79 # "kk2": "vv2", 80 # "kk3": (11,22) 81 # } 82 # ], 83 # "k4": (11,22,33,44) 84 # } 85 # print(info) 86 87 # info = { 88 # 2: "asdf", 89 # "k1": "123", 90 # True: "asdf", 91 # (11,22): 123, 92 # } 93 # print(info) 94 # for item in info.keys(): 95 # print(item,info[item]) 输出关键字和值 96 # 上面语句 = 下面语句 97 # for j,k in info.items(): 98 # print(j,k) 99 100 # info = { 101 # "k1": "asdf", 102 # True: "123", 103 # (11,22):123, 104 # # [11,22]:123, 105 # # {"k1":"v1"}:123, 106 # } 107 # print(info) 108 109 # 字典删除操作 110 # info = { 111 # "k1": 18, 112 # 2: True, 113 # "k3": [ 114 # 11, 115 # [], 116 # (), 117 # 22, 118 # 33, 119 # { 120 # ‘kk1‘: ‘vv1‘, 121 # ‘kk2‘: ‘vv2‘, 122 # ‘kk3‘: (11,22), 123 # } 124 # ], 125 # "k4": (11,22,33,44) 126 # } 127 # v1 = info[2] 128 # print(v1) 129 # v2 = info["k3"][5]["kk3"][0] 130 # print(v2) 131 # del info["k1"] 132 # print(info) 133 # del info["k3"][5]["kk1"] 134 # print(info) 135 136 # 根据序列创建字典,并指定统一的值,静态方法 137 # v = dict.fromkeys(["k1",123,"999"],123) 138 # print(v) 139 140 # 删除并获取值 141 # dic = { 142 # "k1": "v1", 143 # "k2": "v2", 144 # } 145 # v = dic.popitem() 146 # print(dic,v) 147 148 # 若key值已存在,不设置,获取当前key值 149 # 若key值不存在,新设定key值,并获取当前key对应值 150 # dic = { 151 # "k1": "v1", 152 # "k2": "v2", 153 # } 154 # v = dic.setdefault("k3","v3") 155 # print(dic,v) 156 157 # dic.update更新的两种方式 158 # dic = { 159 # "k1": "v1", 160 # "k2": "v2", 161 # } 162 # dic.update({‘k1‘:‘11111‘,‘k2‘:456}) 163 # print(dic) 164 # dic.update(k1=123,k3=345,k5=‘asdf‘) 165 # print(dic) 166 167 # 根据key读取值,key若不存在,直接输出key后面的值 168 # v = dic.get("k1",123) 169 # print(v) 170 # dic = { 171 # "k1": ‘v1‘, 172 # "k2": ‘v2‘ 173 # } 174 # v = dic[‘k11111‘] 175 # print(v) 176 # v = dic.get(‘k1‘,111111) 177 # print(v) 178 179 # 字典中最常用的操作 180 # key() value() item() update() 181 182 # 9-13节知识点(整理) 183 # 一、数字 184 # int(...) 185 186 # 二、字符串 187 # replace/find/join/strip/startswith/split/lower/format 188 # 字符串格式化的两种方式 189 # template = "i am {name}, age : {age}" 190 # v1 = template.format(name="alex",age=25) 191 # v2 = template.format(**{"name":"alex","age":25}) 192 # print(v1) 193 # print(v2) 194 195 # 三、列表 196 # append、extend、insert 197 # 索引,切片,循环 198 199 #四、元组 200 #忽略 201 #索引、切片、循环 元祖中一级元素不能被修改 202 203 # 五、字典 204 # get/update/keys/values/items 205 # for,索引 206 # dic = { 207 # "k1":‘v1‘, 208 # } 209 # 210 # v = ‘k1‘ in dic 211 # print(v) 212 # 213 # v = ‘v1‘ in dic.values() 214 # print(v) 215 216 # 六、布尔值 217 # 0 1 218 # bool(...) 219 # None "" () [] {} 0 --> Flase(上述布尔值均为false) 220 221 # day013 数字,字符串,列表,元组,字典作业练习 222 # 获取两个列表中相同的值 223 # l1 = [11,22,33,44] 224 # l2 = [66,77,22,33,44,55] 225 # for i in l1: 226 # for j in l2: 227 # if i == j: 228 # print(j) 229 # for k in l1: 230 # if k in l2: 231 # print(k) 232 # 获取l1中有,l2中没有的元素列表 233 # l1 = [11,22,33,44] 234 # l2 = [66,77,22,33,44,55] 235 # for i in l1: 236 # if i not in l2: 237 # print(i) 238 # for i in l1: 239 # if i in l2: 240 # continue 241 # else: 242 # print(i) 243 # 获取l1中没有,l2中有的元素列表 244 # l1 = [11,22,33,44] 245 # l2 = [66,77,22,33,44,55] 246 # for i in l2: 247 # if i not in l1: 248 # print(i) 249 # for i in l2: 250 # if i in l1: 251 # continue 252 # else: 253 # print(i) 254 # 获取l1和l2中都不同的元素。 255 # l1 = [0,5,22,6,7,8,9] 256 # l2 = [0,5,44,6,7,8,9] 257 # for i in l1: 258 # if i not in l2: 259 # print(i) 260 # for i in l2: 261 # if i not in l1: 262 # print(i) 263 264 # 有1,2,3,4,5,6,7,8 8个数字,组成互不相同且无重复数字的两位数 265 # num = 0 266 # list1 = [1,2,3,4,5,6,7,8] 267 # list2 = [1,2,3,4,5,6,7,8] 268 # for i1 in list1: 269 # for i2 in list2: 270 # if i1 != i2: 271 # str2 = str(i1) + str(i2) #列表元素转换为字符串 272 # # print(str2,end=" ") 273 # num += 1 274 # print(str2+"\t",end=‘‘) 275 # print("总共可以组成",num,"个") 276 277 # range 操作 例如range(1,9) 输出1-8 8个数 278 # for i in range(1,9): 279 # print(i) 280 281 # 9x9乘法表 282 # for i in range(1,10): 283 # str2 = ‘‘ 284 # for j in range(1,i+1): 285 # print(str2 + str(j) + ‘*‘ + str(i) + ‘=‘ + str(i*j) + ‘\t‘,end=‘‘) 286 # print(‘\n‘,end=‘‘) 287 # print(self, *args, sep=‘ ‘, end=‘\n‘, file=None) sep中间默认加空格,end中间加换行符 288 # 输出列表长度len()函数,print()函数具体用法 289 # li = [1,2,3,4] 290 # # print(len(li)) 291 # for i in range(0,len(li)-1): 292 # for j in range(i+1,len(li)): 293 # print(li[i],li[j],sep=‘‘,end=‘\t‘) 294 295 # 自动计算方案,公鸡5文钱一只,母鸡3文钱一只,小鸡3文钱三只,用100文钱买100只鸡,其中公鸡,母鸡,小鸡都要有,问公鸡,母鸡,小鸡要买多少只刚好凑足100文钱 296 # for x in range(1,100//5): 297 # for y in range(1,100//3): 298 # for z in range(1,100): 299 # if x + y + z == 100 and 5*x + 3*y + (1/3)*z == 100: 300 # print(x,y,z) 301 302 # 用代码实现,利用下划线将列表的每一个元素拼接成字符串,li = [‘alex‘,‘eric‘,‘rain‘] 303 # str2 = ‘‘ 304 # li = [‘a‘,‘b‘,123] 305 # for i in range(0,len(li)): 306 # str2 = str2 + str(li[i]) 307 # print(str2) 308 309 # join类型拼接列表元素 310 # li = [‘a‘,‘b‘,123] 311 # li[2] = str(li[2]) 312 # str1 = ‘‘.join(li) 313 # print(str1) 314 315 # 写代码,有如下元组,按照要求实现每一个功能 316 # tu = (‘alex‘,‘eric‘,‘rain‘) 317 # print(len(tu)) 318 # print(tu[2]) 319 # print(tu[1:]) 320 # for item in tu: 321 # print(item) 322 # for idx in range(0,len(tu)): 323 # print(idx,end=‘ ‘) 324 # for idx, elem in enumerate(tu,3): #enumerate重新添加列,再次从3开始编号 325 # print(idx, elem) 326 327 # 有如下变量,实现要求的功能 328 # a.讲述元祖的特性。 329 # 元组的一级元素不可修改,二级元组根据类型判断是否可以修改。 330 # b.请问tu变量中的第一个元素"alex"是否可以被修改。 331 # 不可以被修改 332 # c.请问tu变量中的"k2"对应的值是什么类型,是否可以被修改,如果可以请在其中添加一个 333 # k2对应的值是列表,可以被修改 334 # dic1=tu[1][2] 335 # list1=dic1.get("k2") 336 # list1.append("zhangsan") 337 # print(tu) 338 # d.请问tu变量中的“k3"对应的值是什么类型,是否可以被修改,如果可以请在其中添加一个 339 # tu=( 340 # "alex", 341 # [ 342 # 11, 343 # 22, 344 # { 345 # "k1":"v1", 346 # "k2":["age","name"], 347 # "k3":(11,22,33) 348 # }, 349 # 44 350 # ] 351 # ) 352 # tu[1][2][‘k2‘].append(‘seven‘) 353 # print(tu[1][2][‘k2‘]) 354 # 355 # dic1 = tu[1][2] 356 # list1 = dic1.get(‘k2‘) 357 # list1.append(‘sara‘) 358 # print(list1) 359 360 # 列举布尔值是False的所有值 361 # 6 + 1 种bool的false值 362 # 6 = 3 + 3 363 # 3:[],(),{} 364 # 3:三个基本类型,int, string, bool 365 # 1:None 366 367 # 有如下列表 368 # nums=[2,7,11,15,1,8,7] 369 # # 请找到列表中任意两个元素相加能够等于9的元素集合,如:[(0,1),(4,5)],输出索引 370 # s = [] 371 # for x in range(0,len(nums)): 372 # for y in range(0,len(nums)): 373 # if nums[x] + nums[y] == 9: 374 # s.append((x,y)) 375 # print(s) 376 377 # 写代码,有如下列表,按照要求实现每一个功能 378 # a.计算列表长度 379 # print(len(li)) 380 # b.列表中追加元素“seven”,并输出添加后的列表。 381 # li.append(‘seven‘) 382 # print(li) 383 # c.请在列表的第1个位置插入元素“tony”并输出添加后的列表。 384 # li.insert(1,‘tony‘) 385 # print(li) 386 # d.请修改第二个位置的元素为“kelly”,并输出修改后的列表。 387 # li[1] = ‘Kelly‘ 388 # print(li) 389 # e.请删除列表中的的元素“eric”,并输出修改后的列表。 390 # li.remove(‘eric‘) 391 # print(li) 392 # f.请删除列表中的第2个元素,并输出删除的元素值和删除元素后的列表 393 # v = li.pop(1) 394 # print(v,‘\t‘,li) 395 # g.请删除列表中的第3个元素,并输出删除元素后的列表。 396 # li.pop(2) 397 # print(li) 398 # h.请删除列表中的第2至第4个元素,并输出删除后的元素列表。 399 # pop值删除应从大到小 400 # li.pop(4) 401 # li.pop(3) 402 # li.pop(2) 403 # print(li) 404 # print(li[1:4]) 405 # i.请将列表所有元素反转,并输出翻转后的列表。 406 # li.reverse() 407 # print(li) 408 # j.请使用for len range 输出列表索引。 409 # for i1 in range(len(li)): 410 # print(i1) 411 # 使用enumrate输出列表元素和序号(序号从100开始) 412 # li = ["alex","eric","rain","tony","sara",‘111‘,‘222‘] 413 # for j,k in enumerate(li,100): 414 # print(j,k) 415 416 # 分页显示内容 417 # a.通过for循环创建301条数据,数据类型不限,如: 418 # Alex-1 Alex1@live.com pwd1 419 # Alex-2 Alex2@live.com pwd2 420 # Alex-3 Alex3@live.com pwd3 421 # user_list = [] 422 # for i in range(1,302): 423 # temp = {‘name‘: ‘alex‘ + str(i),‘email‘: ‘alex‘+ str(i) + ‘@lve.com‘, ‘pwd‘+str(i): ‘pwd‘} 424 # user_list.append(temp) 425 # while True: 426 # s = input(‘请输入1-30的任意一个数:‘) 427 # s = int(s) 428 # result = user_list[10*(s-1):10*s] 429 # for item in result: 430 # print(item,type(item)) 431 432 # 查找列表中元素,移除每个元素空格,并查找以a或A开头,并且以c结尾的所有元素。 433 # li = ["alec", " aric", "AleC", "Tony", "rain"] 434 # tu = ("alec", " aric","aleC", " Alex ", "Tony", "rain") 435 # dic = {"k1": "alec", "k2": "aric", "k3": "Alex", "k4": "Tony", "k5": "rain"} 436 # for li1 in li: 437 # li2 = li1.strip() 438 # if li2.startswith(‘a‘) or li2.startswith(‘A‘): 439 # if li2.endswith(‘c‘) or li2.endswith(‘C‘): 440 # print(li2) 441 # print(‘\n‘,end=‘‘) 442 # for tu1 in tu: 443 # tu2 = tu1.strip() 444 # if tu2.startswith(‘a‘) or tu2.startswith(‘A‘): 445 # if tu2.endswith(‘c‘) or tu2.endswith(‘C‘): 446 # print(tu2) 447 # print(‘\n‘,end=‘‘) 448 # for dic1 in dic.values(): 449 # dic2 = dic1.strip() 450 # if dic2.startswith(‘a‘) or dic2.startswith(‘A‘): 451 # if dic2.endswith(‘c‘) or dic2.endswith(‘C‘): 452 # print(dic2) 453 454 # 有如下元祖,按照要求实现每一个功能。 455 # tu = ("alex", "eric", "rain") 456 # a.计算元组的长度并输出. 457 # print(len(tu)) 458 # b.获取元祖的第2个元素,并输出 459 # print(tu[1]) 460 # c.获取元祖的第1-2个元素,并输出 461 # print(tu[0:2]) 462 # d.请使用for输出元祖的元素。 463 # for i in range(0,len(tu)): 464 # print(tu[i]) 465 # for tu1 in tu: 466 # print(tu1) 467 # e.请使用for、len、range输出元组的索引。 468 # for tu1 in range(len(tu)): 469 # print(tu1) 470 # f.请使用enummrate输出元组的元素和序号(序号从10开始) 471 # for idx,val in list(enumerate(tu,start=10)): 472 # print(idx,val) 473 # for idx,val in enumerate(tu,10): 474 # print(idx,val) 475 476 # 字典 477 # dic = {"K1": "V1", "K2": "V2", "K3": [11, 22, 33]} 478 # a.请输出所有的key 479 # for key in dic.keys(): 480 # print(key) 481 # b.请输出所有的value 482 # for values1 in dic.values(): 483 # print(values1) 484 # c.请循环输出所有的key和value 485 # for keys,values in dic.items(): 486 # print(keys,values) 487 # d.请在字典中添加一对键值对,“K4”:“V4”,输出添加后的字典。 488 # dic.setdefault(‘K4‘,‘V4‘) 489 # print(dic) 490 # e.请修改字典中的"K1"对应的值为"alex",输出修改后的字典。 491 # dic.update(K1=‘alex‘) #如果能找到对应的键,替换对应值,否则在字典末尾添加一对新的键值 492 # print(dic) 493 # f.请在K3对应的值中追加一个元素44,输出修改后的字典。 494 # dic[‘K3‘].append(44) 495 # print(dic) 496 # g.请在K3对应的值第1个位置插入个元素18,输出修改后的字典 497 # dic[‘K3‘].insert(0,18) 498 # print(dic) 499 500 # 转换 501 # a. 将字符串 s="alex"转换为列表 502 # s="alex" 503 # print(list(s),tuple(s)) 504 # c. 将列表 s=["alex","seven"]转换为元组 505 # s=["alex","seven"] 506 # tuple1 = tuple(s) 507 # print(tuple1) 508 # d. 将元组 s=("alex","seven")转换为列表 509 # s = (‘alex‘,‘seven‘) 510 # print(list(s)) 511 # e. 将列表 s=["alex","seven","zhansan","lisi"]转换成字典,而且字典的key按照10开始向后递增。 512 # s=["alex","seven","zhansan","lisi"] 513 # dic = {} 514 # for i,j in enumerate(s,start=10): 515 # dic.setdefault(i,j) 516 # print(dic) 517 518 # 元素和分类 519 # 有如下值的结合[11,22,33,44,55,66,77,88,99,90]将所有大于66的值保存在字典的第1个key值中,小于66保存在第2个key值中。即 520 # {"k1":大于66的所有值,"k2":小于66的所有值} 521 # li = [11,22,33,44,55,66,77,88,99,90] 522 # li1 = [] 523 # li2 = [] 524 # for i in li: 525 # if i>66: 526 # li1.append(i) 527 # else: 528 # li2.append(i) 529 # print({‘k1‘:li1,‘k2‘:li2}) 530 531 # 输出商品列表,用户输入序号,显示用户选中的商品。 532 # 商品 li =["手机","电脑","鼠标","游艇","飞机"] 533 # li =["手机","电脑","鼠标","游艇","飞机"] 534 # dic = {} 535 # while True: 536 # goods = input("请输入想要添加的商品:") 537 # if goods == ‘0‘: 538 # break 539 # li.append(goods) 540 # num = int(input("请输入显示内容的序号:")) 541 # for key,value in enumerate(li,start=1): 542 # dic.setdefault(key,value) 543 # print(dic.get(num)) #字典得到get的值,输出,get到的值不在键值对中,就输出None,或者输出自己定义的字符串 544 # print(dic) 545 546 # 购物车 547 # 功能要求: 548 # 要求用户输入总资产,比如:2000 549 # 显示商品列表,让用户根据序号选择商品,加入购物车。 550 # 购买,如果商品总额大于总资产,提示账户余额不足,否则购买成功。 551 # goods = [ 552 # {"name": "电脑", "price": 1999}, 553 # {"name": "鼠标", "price": 10}, 554 # {"name": "电灯", "price": 20}, 555 # {"name": "椅子", "price": 899} 556 # ] 557 # num = 0 558 # total = input("请输入持有金额:") 559 # for good in goods: 560 # print(num,good.get(‘name‘)) 561 # num += 1 #显示序号 562 # buy = 0 563 # while True: 564 # count = input("请输入购买物品序号,输入ok结束购买:") #c购买货物数量应答器 565 # if count == ‘ok‘: #输出是否购买成功消息 566 # if int(total) - buy >= 0: 567 # print(‘购买成功,请付款:‘,buy) 568 # break 569 # else: 570 # print(‘购买失败,你还需要:‘,buy-int(total)) 571 # break 572 # else: #输出购买累计金额 573 # if count.isdecimal() and int(count) <= len(goods):#isdecimal判断是否为十进制小数 574 # sp = goods[int(count)] 575 # buy += sp.get(‘price‘) 576 # else: 577 # continue
标签:取值 split none 对组 pwd key值 账户 ini 反转
原文地址:https://www.cnblogs.com/yuyukun/p/10354317.html