• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python基础学习代码之映像集合

python 搞代码 4年前 (2022-01-09) 44次浏览 已收录 0个评论
def func1():    dict1 = {}    dict2 = {'name':'earth','port':80}    return dict1,dict2def func2():    return dict((['x',1],['y',2]))def func3():    adict = {}.fromkeys(['x','y'],23)    return adictdef func4():    alist = {'name':'earth','port':80}    for keys in alist.keys():        print "%s %s" % (keys,alist[keys])def func5():    alist = {'name':'earth','port':80}    for keys in alist:        print "%s %s" % (keys,alist[keys])def func6(akey):    alist = {'name':'earth','port':80}    if akey in alist:        return True    else:        return Falsedef func7(akey):    alist = {'name':'earth','port':80}    if alist.has_key(akey):        return True    else:        return Falsedef func8():    alist = {'name':'earth','port':80}    print 'host %(name)s is running on %(port)d' % alistdef func9(akey):    alist = {'name':'earth','port':80}    if akey in alist:        del alist[akey]        return True    else:        return Falsedef func10():    alist = {'name':'earth','port':80}    alist.clear()    return alistdef func11():    alist = {'name':'earth','port':80}    del alistdef func12(akey):    alist = {'name':'earth','port':80}    if akey in alist:        return alist.pop(akey)def func13():    alist = {'name':'earth','a':80}    blist = {'name':'earth','z':8080}    return cmp(alist,blist)def func14():    alist = zip(('x','y'),(1,2))    blist = dict([('xy'[i-1],i) for i in range(1,3)])    return dict(alist),blistdef func15():    adict = {'name':'earth','port':80}    bdict = adict.copy()    return bdict,len(bdict)def func16():    adict = {'name':'earth','port':80}    bdict = {'name':'earth','port':8080}    print adict.keys()    print adict.values()    print adict.items()    adict.update(bdict)    print adictdef func17():    adict = {'name':'earth','port':80}    for keys in sorted(adict):        print 'adict %s has value %s' % (keys,adict[keys])def func18(akey):    adict = {'name':'earth','port':80}    if adict.get(akey):        return True    else:        return 'no such key!'def func19():    adict = {'name':'earth','port':80}    bdict = {}.fromkeys('abc')    print bdict    return adict.setdefault('name','wycqhost')import timenowtime = time.time()def gettime(nowtime):    return time.strftime('%Y/%m/%d %H:%M:%S',time.localtime(nowtime))login = {}def newuser():    prompt = 'login desired:'    name = ''    while True:        name = raw_input(prompt)        if login.has_key(name):            prompt = 'name taken,try another:'            continue        else:            break    pwd = raw_input('passwd:')    login[name] = [abs(hash(pwd))]    login[name].append(0)    print logindef olduser():    nowtime = time.time()    name = raw_input('login:')    if name not in login:        choose = raw_input('will you create a new user(y/n)')        if choose.lower()[0] == 'y':            newuser()        else:            pass        showmenu()    else:        pwd = raw_input('passwd:')        passwd = login.get(name)[0]        if abs(hash(passwd)) == abs(hash(pwd)):            if login[name][1] == 0:                print login                print 'welcome back',name,'you first time loggin'            else:                print 'welcome back',name                if nowtime - login[name][1] <= 14400:                    print 'you are already logged at time',gettime(login[name][1])            login[name][1] = nowtime        else:            print 'login incorrect'            returndef showuser():    print 'show all user:'    for user in login.keys():        print userdef deleteuser():    duser = raw_input('delete user:').lower()    if duser in login.keys():        del login[duser]    else:        print 'user %s is not exists' % duser        showmenu()def showmenu():    prompt = '''(n)ew user login(o)ld user login(s)how all user(d)elete user(q)uitenter 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 'noqds':                print 'invalid option,try again'            else:                chosen = True            if choice == 'q':                done = True            if choice == 'n':                newuser()            if choice == 'o':                olduser()            if choice == 'd':                deleteuser()            if choice == 's':                showuser()#if __name__ == '__main__': #   showmenu()def func20():    str1 = '093keffeoelgn'    t = set(str1)    s = frozenset(str1)    return t == sdef func21():    aset = set('xiewenbin')    if 'x' in aset:        print "x in aset"def func22():    aset = set('strings')    aset.add('http')    aset.update('httpx')    aset.remove('http')    aset -= set('x')    for i in aset:        print i    del asetdef func23():    aset = set('abc')    bset = set('abcedf')    return aset <= bsetdef func24():    aset = set('markshop')    bset = frozenset('earthshop')    print aset | bset    print bset & aset    print aset ^ bset    print aset - bsetdef func25():    s = set('cheeseshop')    u = frozenset(s)    s |= set('xie')    print s    s = set(u)    s &= set('shop')    print s    s = set(u)    s -= set('shop')    print s    s = set(u)    t = frozenset('bookshop')    s ^= t    print s    print len(s)import osdef func26():    frozenset(['a','b','c'])    f = open('test.txt','w')    for i in range(5):        f.write('%d\n'%i)    f.close()    f = open('test.txt','r')    print set(f)    f.close()    os.remove('test.txt')def func27():    alist = ['a','b']    blist = [1,2]    print dict(zip(alist,blist))def func28():    adict = {'a':1,'b':2,'c':3}    bdict = {}    for keys in adict.keys():        bdict[adict[keys]] = keys    return bdictdef func29(sstr,dstr,string,casemap=True):    assert len(sstr) >= len(dstr)    table = dict(zip(sstr,dstr))    print table    if len(sstr) > len(dstr):        temp = {}.fromkeys(sstr[len(dstr)])        table.update(temp)        print table    ls = []    for ch in string:        if not casemap:            if ch.lower() in table:                ls.append(table[ch.lower()])            elif ch.upper() in table:                ls.append(table[ch.upper()])            else:                ls.append(ch)                continue        if ch in table:            ls.append(table[ch])        else:            ls.append(ch)    ls = [ch for ch in ls if ch]    print ls    return " ".join(ls)def func30(sstr):    alist = [chr((num + 13) % 26 + ord('a')) for num in range(26)]    blist = [chr(num + ord('a')) for num in range(26)]    table = dict(zip(blist,alist))    astr = "".join(alist).upper()    bstr = "".join(blist).upper()    table.update(dict(zip(bstr,astr)))    ls = []    for ch in sstr:        if ch in table:            ls.append(table[ch])        else:            ls.append(ch)    return " ".join(l<strong style="color:transparent">本文来源gaodai#ma#com搞@@代~&码*网/</strong>s)import randomdef func31():    alist = [random.randint(i,10) for i in range(10)]    blist = [random.randint(i,10) for i in range(10)]    aset = set(alist)    bset = set(blist)    print aset | bset    print aset & bset

以上就是Python基础学习代码之映像集合的内容,更多相关内容请关注搞代码(www.gaodaima.com)!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python基础学习代码之映像集合
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址