python第八课:字典的魔法+练习题
作者:互联网
# 字典 # dict # info = {"k1":"v1","k2":"v2","k3":True,"k4":[11,22,33],"k5":()} # 1可以嵌套,value 可以是任何值 # 2布尔值(0,1),列表、字典不能作为字典的key # info = { # 1:"asdf", # "k1":"adadf", # True:"123", # (11,22):345 # } # print(info) # 3字典是无序的。打印出来的时候顺序不一定。 # 取值的时候中括号中参数加上key即可。 # 通索引方式找到指定元素 # v = info["k3"][2]["sd"] # # 4删除字典中的某一个键值对 # del info["k1"] # del info["k3"][3] # print(info) # 5for循环,默认for循环只有key # for item in info: # print(item) # # 6加上参数可以获取value或者只有获取value # for item in info.values(): # print(item,info[item]) # for item in info.keys(): # print(item,info[item]) # -------------------------------- # for k,v in info.items(): # print(k,v) # 字典特有的相关操作: # # 1、根据序列来创建字典,并给统一的值 # fromkeys # dic = { # "k1":"v1" # } # v = dict.fromkeys(["k1",124,"888"],124) # print(v) # 2、取值: # dic.get("k1") # get 的方式如果key不存在时,系统不会报错,可以指定默认值,none # # # 3、删除并获取值 # dic.pop # dic.popitem() # # 4、设置值,如果已经存在就是获取当前key对应的值, # 如果不存在就进行添加设置。 # dic.setdefault("key","value") # # # 5、更新 # 如果存在即更新 # 如果不存在就添加 # dic.update # # dic.update(k1=124,k3=345,k5="adfadf")#或者在括号中用“:”来标识键值对 # # # 6、keys() # 7、values() # 8、items() get() update # # # ############整理############必须要会的 # 1、数字 # int(..) , # #2、 字符串 # replace/find/join/strip/startswith/split/upper/lower/format # tempalte = "i am {name},age:{age}" # v = tempalte.format(name="alex",age=19) # v = tempalte.format({**"name":"alex","age":19}) # print(v) 3、列表 append/extend/insert/ 索引、切片、循环 4、元组 索引、切片、循环 以及元素不能被修改 5、字典 get/update/keys/values/items for,索引,in 默认是查询key 6、布尔值 0 1 bool() None "" ,(), [],{},0 =>Fals
-----------------------------------------------
练习题
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# l = [11,22,33,44,55,66,77,88,99,90]
# result = {}
# # {'k1': [77, 88, 99, 90], 'k2': [11, 22, 33, 44, 55]}
#
# for item in l:
# if item < 66:
# # result.update({"k1": item}) # {'k1':11}
# # {'k1': [11,22]}
# if "k1" not in result:
# result['k1'] = [item, ] # {'k1': [11,]}
# else:
# result['k1'].append(item)
# elif item > 66:
# if "k2" not in result:
# result['k2'] = [item, ] # {'k1': [11,]}
# else:
# result['k2'].append(item)
# for i in l:
# if i < 66:
# a.append(i)
# if i > 66:
# b.append(i)
# print({'k1':b,'k2':a})
# 有两个列表
# l1 = [11,22,33]
# l2 = [22,33,44]
# a. 获取内容相同的元素列表
# b. 获取 l1 中有, l2 中没有的元素列表
# c. 获取 l2 中有, l1 中没有的元素列表
# d. 获取 l1 和 l2 中内容都不同的元素
l1 = [11,22,33]
l2 = [22,33,44]
# for i in l2:
# if i not in l1:
# print(i)
# for i in l2:
# if i not in l1:
# print(i)
count =0
for i in range(1, 9):
for v in range(1, 9):
if i !=v:
count += 1
# count = count -8
print(count)
#
# count =0
# ls = [3,5,8,9]
# for i in range(0, len(ls)):
# for v in range(0, len(ls)):
# if i !=v:
# count += 1
# # count = count -8
# print(count)
#99乘法表
# for i in range(1,10):
# string = ""
# for j in range(1,i+1):
# string +=str(j) + " * "+str(i) + " = " +str(i*j)+"\t"
# print(string)
# print("aaa",end="")
# print("bbb")
# for i in range(1,10):
# for j in range(1,i+1):
# print(str(j) + " * "+str(i) + " = " +str(i*j)+"\t",end="")
# print("\n",end="")
# for i in range(1,10):
# for j in range(1,i+1):
#
# \n
# print('alex', end='SB')
# print('alex')
# print('alex','haifeng','gangniang',sep='搞')
# 这里面的数字两个组合 多少种不同样的,数字不重复的
# li = [1,2,3,4]
# l =len(li)
# for i in range(0,l-1):
# for v in range(i+1,l):
# print(li[i],li[v])
# for x in range(1,100//5):
# for y in range(1,100//3):
# for z in range(1,100):
# if x + y + z == 100 and 5*x + 3*y + z/3 == 100:
# print(x,y,z)
# li = ['alex','eric',123]
# li[2] = str(li[2])
#
#
# v = "_".join(li)
# print(v)
# tu = ('alex', 'eric', 'rain')
# print(len(tu))
# print(tu[2])
# print(tu[1:])
# #step 为正
# print(tu[1:90])
# for elem in tu:
# print(elem)
# for idx in range(len(tu)):
# print(idx)
# for idx, elem in enumerate(tu, 10):
# print(idx, elem)
# tu =(
# "alex",
# [
# 11,
# 22,
# {
# "k1":'v1',
# "k2":["age","name"],
# "k3":(11,22,33)
# },
# 44
# ]
# )
# tu[1][2]["k2"].append("")
# nums = [2,7,11,15,1,8,7]
# a =[]
# for i in range(len(nums)):
# for j in range(len(nums)):
# if nums[i] + nums[j] ==9:
# a.append((i,j,))
# print(a)
# li = ["sdsdsd"]
# print (len(li))
# li.append("dsad")
# li.insert(0,"dsad")
# li.remove("eric")
# v = li.pop(1)
# print(li,v)
#
# v = li.reverse()
# for i in range(len(li)):
# print(i)
# for i,q in enumerate(li,100):
# print(i,q)
# for i in li:
# print(i)
# user_list = [
# ]
# for i in range(1,302):
# temp = {'name': 'alex'+str(i), 'email': 'alex@lve.com' + str(i), 'pwd': 'pwd'+str(i)}
# user_list.append(temp)
#
# while True:
# # 每页显示10条数据
# s = input("请输入1,2,3-31页码:")
# s = int(s)
# # user_list[0,10] 1
# # user_list[10,20] 2
# # user_list[20,30] 3
# start = (s-1) * 10
# end = s * 10
# result = user_list[start: end]
# for item in result:
# print(item,type(item))
标签:练习题,info,python,k1,li,第八课,item,range,print 来源: https://www.cnblogs.com/lfluofei/p/15943900.html