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

python基础三

时间:2016-10-25 13:55:03      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:浦东   break   爱奇艺   --   eve   unicode编码   nod   百度   bin   

多级菜单

  • 多级菜单
  • 可依次选择进入各子菜单
  • 所需新知识点:列表、字典
#!/usr/bin/env python
# -*- coding: utf-8 -*-
menu = {
    北京:{
        海淀:{
            五道口:{
                soho:{},
                网易:{},
                google:{}
            },
            中关村:{
                爱奇艺:{},
                汽车之家:{},
                youku:{},
            },
            上地:{
                百度:{},
            },
        },
        昌平:{
            沙河:{
                老男孩:{},
                北航:{},
            },
            天通苑:{},
            回龙观:{},
        },
        朝阳:{},
        东城:{},
    },
    上海:{
        闵行:{
            "人民广场":{
                炸鸡店:{}
            }
        },
        闸北:{
            火车战:{
                携程:{}
            }
        },
        浦东:{},
    },
    山东:{},
}

current_level = menu
last_level = []

while True:
    for key in current_level:
        print(key)
    choice = input(">>:").strip()
    if len(choice) ==0:break
    if choice ==b:
        if len(last_level)==0:break
        current_level = last_level[-1]
        last_level.pop()
    if choice not in current_level:continue
    last_level.append(current_level)
    current_level = current_level[choice]

 

字典

查询

#!/usr/bin/env python
# -*- coding: utf-8 -*-
dic={name:alex,age:18}


print(dic[name])
print(dic.get(namedf))#用get,如果没有这个值,查询不会报错

 

以上代码运行结果

alex
None

 

add

#!/usr/bin/env python
# -*- coding: utf-8 -*-
dic={name:alex,age:18}

dic[gender]=female
print(dic)#打印字典是无序的

 

以上代码运行结果

{age: 18, gender: female, name: alex}

 

change

#!/usr/bin/env python
# -*- coding: utf-8 -*-
dic={name:alex,age:18}

dic[name]=lhf
print(dic)

 

以上代码运行结果

{name: lhf, age: 18}

 

del

#!/usr/bin/env python
# -*- coding: utf-8 -*-
dic={name:alex,age:18}

del dic[name]
print(dic)

 

以上代码运行结果

{age: 18}

 

字典里嵌入其它数据类型

#!/usr/bin/env python
# -*- coding: utf-8 -*-
dic1={
    1:alex,
    name:lhf,
    (1,2,3):{age:18},
    name:alex
}

print(dic1[1])
print(dic1[name])
print(dic1[(1,2,3)])
print(dic1[(1,2,3)][age])

 

以上代码运行结果

alex
alex
{age: 18}
18

 

字典的其它用法

#!/usr/bin/env python
# -*- coding: utf-8 -*-

dic1=dict.fromkeys(abc,1)
print(dic1)

 

以上代码运行结果(注意:此处类似浅拷贝)

{b: 1, c: 1, a: 1}

 

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-
dic={name:alex,age:18}

dic2 =dict.fromkeys([a,b],1)
print(dic2)
print(dic.get(name))

 

以上代码运行结果

{b: 1, a: 1}
alex

 

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-
dic={name:alex,age:18}


for k,v in dic.items():
    print(k,v)
print(dic.keys())
for i in dic.keys():
    print(key is %s,value is %s%(i,dic[i]))

 

以上代码运行结果

age 18
name alex
dict_keys([age, name])
key is age,value is 18
key is name,value is alex

 

指定删除

#!/usr/bin/env python
# -*- coding: utf-8 -*-
dic={name:alex,age:18}


dic.pop(name)
print(dic)

 

以上代码运行结果

{age: 18}

 

随机删除

#!/usr/bin/env python
# -*- coding: utf-8 -*-
dic={name:alex,age:18}

dic.popitem()
print(dic)

 

以上代码运行结果

{name: alex}

 

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-
dic={name:alex,age:18}


dic.setdefault(gender,[]).append(male)
dic[gender].append(female)

 

以上代码运行结果

{age: 18, name: alex, gender: [male, female]}

 

 

__author__ = "zhou"
#!/usr/bin/env python
# -*- coding: utf-8 -*-
dic={name:alex,age:18}
dic1 = {gender:male,name:ldf}

dic.update(dic1)
print(dic)

 

以上代码运行结果

{name: ldf, age: 18, gender: male}

 

常用的取字典里面键值的方法

#!/usr/bin/env python
# -*- coding: utf-8 -*-
dic={name:alex,age:18}

for k in dic:
    print(k,dic[k])
for i in dic.values():
    print(i)

 

以上代码运行结果

age 18
name alex
18
alex

 

浅拷贝

#!/usr/bin/env python
# -*- coding: utf-8 -*-
dic ={name:alex,age:18,gfs:["Monkey","Cythrina","Rachel"]}

dic2=dic.copy()
print(dic)
print(dic2)
dic2["name"]="Jack"

dic2["gfs"][2] = "hhhh"
print(dic)
print(dic2)

 

以上代码运行结果

{gfs: [Monkey, Cythrina, Rachel], name: alex, age: 18}
{gfs: [Monkey, Cythrina, Rachel], name: alex, age: 18}
{gfs: [Monkey, Cythrina, hhhh], name: alex, age: 18}
{gfs: [Monkey, Cythrina, hhhh], name: Jack, age: 18}

集合

 

 

 

 

 

 

字符编码

字符---gb2313--->硬盘
硬盘---gb2313--->字符
字符 --->unicode--->内存

1、内存固定使用unicode编码,硬盘的编码(即你可以修改的软件编码)
2、使用什么编码往硬盘存,就用什么编码去读
3、程序运行分两阶段:
  1、从硬盘读到内存
  2、python解释器运行已经读到内存的代码
4、针对一个test.py文件来说python与nodpad++\vim区别是多了第二步。

 

python基础三

标签:浦东   break   爱奇艺   --   eve   unicode编码   nod   百度   bin   

原文地址:http://www.cnblogs.com/xone/p/5996153.html

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