1、用户交互,显示省市县三级联动的选择
dic
=
{
"河北"
: {
"石家庄"
: [
"鹿泉"
,
"藁城"
,
"元氏"
],
"邯郸"
: [
"永年"
,
"涉县"
,
"磁县"
],
}
"河南"
: {
...
}
"山西"
: {
...
}
}
#!/usr/bin/env python # coding:utf-8 dic = { "河北": { "石家庄": ["鹿泉", "藁城", "元氏"], "邯郸": ["永年", "涉县", "磁县"], }, "河南": { ... }, "山西": { ... } } print(type(dic)) def threedic(dict): while True: for i in dict: #河北 河南 山西 石家庄邯郸 print(i) key=input("》》》: ").strip() # 河北 if key == 'b' or key == 'q': return key if type(dict) == list: continue else : if key in dict.keys() and dict[key] : ret = threedic(dict[key]) # 河北的 值 石家庄 邯郸 if ret == 'q': return ret continue elif not dict.get(key) or not dict[key] : continue threedic(dic)
2.打印出一个文件的最后一行
def last(): f=open('test.py','rb') offs=-10 while True : res=[] f.seek(offs,2) a=f.readlines() for i in a : res.append(i) if len(res)>1 : print("文件的最后一行是: %s" %((res[-1]).decode('utf-8'))) exit() else : offs*=2 lsat()
3、统计字符串中大小写字母,数字出现的个数,并以字典方式打印出来
def tongji(x): a=b=c=0 for i in x: if i>='a' and i<= 'z': a+=1 if i>='A' and i <='Z' : b+=1 if i>='0' and i<='9': c+=1 list1=['大写字母','小写字母','数字'] list2=[a,b,c] dict(zip(list1,list2)) print (dict(zip(list1,list2))) tongji('A12345asdasd')