1. 内建方法fromkeys()创建一个默认字典, 字典中元素具有相同的值,默认为None

  dict1 = {}.fromkeys(('x', 'y'), -1)

2. 访问字典中的值,  for key in dict1.keys():

            print 'key=%s, value=%s' % (key, dict1[key])

3. 删除字典元素和字典: del dict1['x'],  dict1.clear():删除所有条目,del dict1 删除字典, dict1.pop('x', defalult): 删除x,并返回其value ,若不存在key,则  default

4. 字典比较算法: 1. 比较字典长度, 2. 比较字典额键值,3. 比较字典的值, 4. 完全匹配

5.  内建方法: dict1.keys(), dict1.values() ,dict1.items()

    dict1.get(key,default), 返回key对应的value, 若不存在该key,则返回default的值

    dict1.has_key(key)

    dict1.update(dict2), 将字典dict2的键值对添加到字典dict1中

6. 利用dict1 存储用户名密码, 模拟用户登录注册功能

'''
Created on 2014-5-12
@author: jyp
'''
db = {}

def newuser():
    prompt = 'login desired: '
    while True:
        name = raw_input(prompt)
        if db.has_key(name):
            prompt = 'name taken, try anthoer: '
            continue
        else:
            break
    pwd = raw_input('passwd: ')
    db[name] = pwd

def olduser():
    name = raw_input('login: ')
    print name
    print db
    if db.has_key(name):
        pwd = raw_input('passwd: ')
        passwd = db.get(name)
        if pwd == passwd:
            print "welcome back: " , name
        else:
            print "password error"
    else:
        print "no this name"
            
def showmenu():
    prompt = """
    New User Login
    Existing User Login
    Quit
    Enter choice: """
    done = False
    while not done:
        chosen = False
        while not chosen:
            try:
                choice = raw_input(prompt).strip()[0].lower()
            except (EOFError, KeyboardInterrupt):
                choice = 'q'
            print '\n You picked: [%s]' % choice
            if choice not in 'neq':
                print 'invalid option , try again'
            else:
                chosen = True
        if choice == 'q': done = True
        if choice == 'n': newuser()
        if choice == 'e': olduser()

if __name__ == '__main__':
    showmenu()

----------------------------------------------------------------------------------------------------------------------------------------------

7. 集合类型

  可变集合set: s = set('cheeseshop')   set 不允许重复,元素有序。

    增加元素,s.add('zzz'), s.uppdate('abcde') , s.remove('z') :删除元素 , s -= set('abcde'):删除abcde

  不可变集合: t = frozenset('bookshop') ,

8. 集合的交差并补: 联合: '|', 交集:'&', 差集:'-', 差分/异或: '^'

9. 集合类型工厂函数: set和frozenset()

10. 集合类型内建方法:

  s.issubset(t), s是否为t的子集

  s.issuperset(t),  s.symmetric_different(t): 该集合是s或者t的成员,但不是s和t共有的成员。

  s.union(t), 并集, s.intersection(t), 交集, s.different(t):该集合是s的成员,但不是t的成员

  s.copy(), 返回一个新集合, 潜复制

day5_python学习笔记_chapter7_字典的更多相关文章

  1. python学习笔记整理——字典

    python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...

  2. Object C学习笔记13-Dictionary字典

    通过Array数组和Set集合的学习和理解,可以想象得到Dictionary也分为两种情况了,那就是可变和不可变两种类型的.的确如此,在Object C中提供了两个字典类,分别为NSDictionar ...

  3. Python学习笔记之字典

    一.创建和使用字典 1.创建字典 phonebook={'Alice':'2341','Beth':'9102','Cecil':'3258'} 2.dict,通过映射创建字典 >>> ...

  4. python3.5学习笔记--利用字典对指定文本字符串进行替换

    事情缘起于同事整理excel,需要批量的对某一列的内容进行替换. 举例: 数据格式:以下为一列内容,每行都在一个单元格中,目的是将数字替换为制定的中文字符. 1,2,31 ,4,33 ,21,, 对于 ...

  5. Objective -C学习笔记之字典

    //字典:(关键字 值) // NSArray *array = [NSArray array];//空数组 // NSDictionary *dictionary = [NSDictionary d ...

  6. 【Python学习笔记】字典操作

    字典dict是Python中唯一内置的映射类型,由键值对组成,字典是无序的.字典的键必须是不变对象,如字符串.数字.元组等,而包含可变对象的列表.字典和元组则不能作为键.这里可变和不可变的意思是指这个 ...

  7. Python学习笔记四--字典与集合

    字典是Python中唯一的映射类型.所谓映射即指该数据类型包含哈希值(key)和与之对应的值(value)的序列.字典是可变类型.字典中的数据是无序排列的. 4.1.1字典的创建及赋值 dict1={ ...

  8. Python学习笔记004_字典_集合

    >>> # 字典 用大括号表示, 它是影射类型,相当于java中的Map >>> >>> dict1 = {'李宁': '一切皆有可能', '耐克 ...

  9. Python学习笔记(2)-字典

    什么是字典? 像列表一样,但是不能想列表用下表索引,而是通过'键',键及其关联的值称为'键-值'对.字典经常会简写成dict 创建字典{} 例如: >>> dict = {'one' ...

随机推荐

  1. UART串口协议基础1

    Louis kaly.liu@163.com 串口协议基础 1 串口概述 串口由收发器组成.发送器是通过TxD引脚发送串行数据,接收器是通过RxD引脚接收串行数据. 发送器和接收器都利用了一个移位寄存 ...

  2. SQL 数据类型、约束、索引及视图

    一.数据类型:整数:int,bigint,smallint小数:float,real,decimal(长度,精度),numeric(长度,精度)字符:char(n),varchar(n) 8000英文 ...

  3. this.parentMenu.dataRecord.data.testID的作用

    在JS里,有个this.parentMenu.dataRecord.data.XXID的方法,这个方法一般都是用来加载某个控件到一个面板或控件上的.如: loaddata(this.parentMen ...

  4. html系列教程--embed fieldset legend figure figurecaption

    <embed> 标签:定义嵌入的内容 <embed src="" type="" /> embed属性: 1.src:嵌入内容地址 2. ...

  5. SQL Server执行计划那些事儿(3)——书签查找

    接下来的文章是记录自己曾经的盲点,同时也透漏了自己的发展历程(可能发展也算不上,只能说是瞎混).当然,一些盲点也在工作和探究过程中慢慢有些眉目,现在也愿意发扬博客园的奉献精神,拿出来和大家分享一下. ...

  6. alimama open source mdrill启动后访问蓝鲸任务时出错:Caused by:org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss

    启动后,访问:http://IP:1107/mdrill.jsp  蓝鲸任务

  7. Mantis的附件图片实现预览/弹出层动画效果预览图片(LightBox2)的完美解决方案[Z]

    方法1: 在Mantis的配置文件中,加入此句,将这个值设的很大,就可以直接看到图片 1 $g_preview_attachments_inline_max_size=1000000; 效果如图 这个 ...

  8. Apache Tomcat Not Showing in Eclipse Server Runtime Environments

    In my case I needed to install "JST Server Adapters". I am running Eclipse 3.6 Helios RCP ...

  9. c语言中的#ifndef、#def、#endif等宏是什么意思

    #ifndef.(或者#ifndef).#def.#endif等宏这几个宏是为了进行条件编译.一般情况下,源程序中所有的行都参加编译.但是有时希望对其中一部分内容只在满足一定条件才进行编译,也就是对一 ...

  10. JRainbow开发进度

    最新版本下载 http://pan.baidu.com/s/1c0GcDMg&third=15 相关信息 JRainbow的简单介绍:http://blog.csdn.net/jrainbow ...