zoukankan      html  css  js  c++  java
  • day5_python学习笔记_chapter7_字典

    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 ' 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(), 返回一个新集合, 潜复制


           

  • 相关阅读:
    递归神经网络 RNN 原理(上)
    递归神经网络
    队列-Python 实现
    Tableau 我常用函数整理
    代码重构(OOP)-小栗子(PyQt5)
    Python基础
    Excel 数据显示到网页
    采坑
    Python基础
    JavaScript一些对象。
  • 原文地址:https://www.cnblogs.com/yongpan666/p/3724076.html
Copyright © 2011-2022 走看看