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

def func1():
dict1 = {}
dict2 = {‘name’:’earth’,’port’:80}
return dict1,dict2
def func2():
return dict(([‘x’,1],[‘y’,2]))
def func3():
adict = {}.fromkeys([‘x’,’y’],23)
return adict
def 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 false
def func7(akey):
alist = {‘name’:’earth’,’port’:80}
if alist.has_key(akey):
return true
else:
return false
def func8():
alist = {‘name’:’earth’,’port’:80}
print ‘host %(name)s is running on %(port)d’ % alist
def func9(akey):
alist = {‘name’:’earth’,’port’:80}
if akey in alist:
del alist[akey]
return true
else:
return false
def func10():
alist = {‘name’:’earth’,’port’:80}
alist.clear()
return alist
def func11():
alist = {‘name’:’earth’,’port’:80}
del alist
def 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),blist
def 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 adict
def 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 time
nowtime = 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 login
def 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] 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(ls)
import random
def 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基础学习代码之映像集合的内容,更多相关内容请关注php中文网(www.php1.cn)!

Posted in 未分类

发表评论