亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

Notes of Py for informatics 3

2019-11-10 18:31:49
字體:
來源:轉載
供稿:網友

Chapter 9 Dictionaries

List: A linear collection of values that stay in order

       Lists index their entries based on the position in the list

Dictionary: A "bag" of values, each with its own label

       Dictionaries are like bags - no order

       So we index the things we put in the dictionary with a "lookup tag"

Dictionaries are like Lists except that theyuse keys instead of numbersto look up values.

purse = dict()  # kind of like, use the label to replace ordered number to mark the placepurse['money'] = 12purse['candy'] =3purse['tissues'] = 75PRint purseprint purse['candy']purse['candy'] = purse['candy'] + 2print purSEOutput:

{'money': 12, 'tissues': 75, 'candy': 3}3{'money': 12, 'tissues': 75, 'candy': 5}

# the label is not always 'string'

Empty dictionary

ooo = {}print ooo

Tracebacks: an error will appear if you refer a key which is not in the dictionary

We can use the in Operator to check if a key is in the dic

print 'csev' in cccoutput:FalseAn interesting example: count names

namelist = ['Joe', 'Joey', 'Joe', 'Mike', 'Mike', 'Joey', 'Mike', 'Yifan', 'Joe']counts = dict()for name in namelist:    if name in counts:        counts[name] = counts[name] + 1    else:        counts[name] = 1print countsAnother way might be more understandable:

for name in namelist:    if name not in counts:        counts[name] = 1    else:        counts[name] = counts[name] + 1output:

{'Yifan': 1, 'Mike': 3, 'Joe': 3, 'Joey': 2}

The get method for dictionary

If the key is not in the dictionary, make the value equal to aDefault Value

The following two code snippets have the same result

if name in counts:    print counts[name]else:    print 0
print count.get(name,0)Simplified counting with get()

namelist = ['Joe', 'Joey', 'Joe', 'Mike', 'Mike', 'Joey', 'Mike', 'Yifan', 'Joe']counts = dict()for name in namelist:    counts[name] = counts.get(name,0) + 1print counts

Def Loops and Dictionaries

for loop cango through all of the keys in the dic and look up the values by keys

namecount = {'Yifan': 1, 'Mike': 3, 'Joe': 3, 'Joey': 2}for key in namecount:    print key, namecount[key]output:

Yifan 1Mike 3Joe 3Joey 2

Get the list of keys (or values, or both)

namecount = {'Yifan': 1, 'Mike': 3, 'Joe': 3, 'Joey': 2}print list(namecount)print namecount.keys()print namecount.values()print namecount.items()output:

['Yifan', 'Mike', 'Joe', 'Joey']['Yifan', 'Mike', 'Joe', 'Joey'][1, 3, 3, 2]         # same order with the keys[('Yifan', 1), ('Mike', 3), ('Joe', 3), ('Joey', 2)]       <- ( , ) is a kind of tuples

Two Iteration Variables!

key-value in pairs

namecount = {'Yifan': 1, 'Mike': 3, 'Joe': 3, 'Joey': 2}for name,count in namecount.items():    print name, count

output:

Yifan 1Mike 3Joe 3Joey 2

An Exercise: Get the most frequently appeared Word in the text and count the number of it

file_name = raw_input("Enter the file name:")fhand = open(file_name,'r')content = fhand.read()wordslist = content.split()count = dict()for word in wordslist:    count[word] = count.get(word,0) + 1biggestcount = Nonebiggestword = Nonefor word, wordcount in count.items():    if biggestword == None or wordcount > biggestcount :        biggestcount = wordcount        biggestword = wordprint biggestword, biggestcountComment: Do not forget the split() function.

Chapter 10 Tuples

Tuples are another kind of sequence that function much like a list

- they have elements which are indexed starting at 0

list: [  ]

tupes: ( )

But,

Tuples are none-changeable, immutable. Kind of similar to a string.

z = (5, 4, 3)         z[2] = 0.      z.sort()

Tuples are more efficient

(x, y) = (100, 4)print y
Conduct the assignment at a time

Tuples are Comparable

print (0, 1, 2) < (5, 1, 2)print (0, 1, 2) < (0, 3, 10000)print ('Jones', 'Sally') < ('Jones', 'Fred')print ('Jones', 'Sally') > ('Jane', 'Sally')print ('Jones', 'Sally') > ('Adam', 'Sally')output:

TrueTrueFalseTrueTrue

Sorting Lists of Tuples

We can take advantage of the ability to sort a list of tuples toget a sorted version of a dictionary

#use d.items() and t.sort()namedic = {'Yifan': 1, 'Mike': 3, 'Joe': 3, 'Joey': 2}nametuple = namedic.items()print nametuple    # sort by keysnametuple.sort()print nametuple#use d.items() and sorted(). More directlynamedic = {'Yifan': 1, 'Mike': 3, 'Joe': 3, 'Joey': 2}print namedic.items()t = sorted(namedic.items())print tfor k,v in sorted(namedic.items()):    print k,voutput:

[('Yifan', 1), ('Mike', 3), ('Joe', 3), ('Joey', 2)][('Joe', 3), ('Joey', 2), ('Mike', 3), ('Yifan', 1)][('Yifan', 1), ('Mike', 3), ('Joe', 3), ('Joey', 2)][('Joe', 3), ('Joey', 2), ('Mike', 3), ('Yifan', 1)]Joe 3Joey 2Mike 3Yifan 1

Sort by values instead of key

namedic = {'Yifan': 1, 'Mike': 3, 'Joe': 3, 'Joey': 2}tmp = list()for k, v in namedic.items():    tmp.append((v,k))print tmptmp.sort(reverse=True)  #(reverse=True) means it presents from the highest to lowestprint tmpoutput:

[(1, 'Yifan'), (3, 'Mike'), (3, 'Joe'), (2, 'Joey')][(3, 'Mike'), (3, 'Joe'), (2, 'Joey'), (1, 'Yifan')]

it seems that the tuple sort focus on the first stuff in each item: (focus, xxx)

An Exercise: The top 10 most common words

file_name = raw_input("Enter the file name:")fhand = open(file_name,'r')content = fhand.read()wordslist = content.split()count = dict()for word in wordslist:    count[word] = count.get(word,0) + 1lll = list()for k,v in count.items():    lll.append((v,k))lll.sort(reverse=True)for i in range(10):    print lll[i]output:

(352, 'Jan')(324, '2008')(245, 'by')(243, 'Received:')(219, '-0500')(218, 'from')(203, '4')(194, 'with')(183, 'Fri,')(136, 'id')

for v,k in lll[:10]:    print v,koutput:

352 Jan324 2008245 by243 Received:219 -0500218 from203 4194 with183 Fri,136 id

Even Shorter Version

namedic = {'Yifan': 1, 'Mike': 3, 'Joe': 3, 'Joey': 2}print sorted([(v,k) for k,v in namedic.items()]) #here '[]' is a list comprehensionoutput:

[(1, 'Yifan'), (2, 'Joey'), (3, 'Joe'), (3, 'Mike')]

Comments:

    List comprehension: creates a dynamic list that meets certain requirements.

              In this case, we make a list of reversed tuples and then sort it.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
最近2019中文字幕mv免费看| 91精品久久久久久久久| 一区二区三欧美| 成人黄色av免费在线观看| 91av在线国产| 国产在线拍揄自揄视频不卡99| 欧美裸体xxxxx| 久久久久久一区二区三区| 精品久久久一区| 亚洲国产欧美一区二区三区同亚洲| 国产成人精品av| 亚洲第一综合天堂另类专| 亚洲精品美女在线观看| 欧美成人精品在线观看| 国产精品久久久久影院日本| 国产精品av在线| 欧美视频裸体精品| 伊人一区二区三区久久精品| 欧美福利视频在线观看| 精品二区三区线观看| 国产成人精品久久久| 日本精品中文字幕| www.国产精品一二区| 福利二区91精品bt7086| 91久久精品一区| 亚洲欧美国产高清va在线播| 亚洲国产成人在线播放| 亚洲国产97在线精品一区| 亚洲欧洲日韩国产| 精品国产欧美一区二区五十路| 精品国产一区二区三区四区在线观看| 91精品国产综合久久香蕉922| 亚洲一区二区久久久久久久| 亚洲女人天堂成人av在线| 亚洲韩国日本中文字幕| 国产综合在线视频| 麻豆成人在线看| 亚洲黄色在线看| 成人免费大片黄在线播放| 日韩免费高清在线观看| 国产欧美日韩中文| 成人黄色中文字幕| 久久久久久网站| 久久视频在线视频| 欧美成人免费小视频| 日韩有码片在线观看| 久久好看免费视频| 中文字幕欧美视频在线| 欧美午夜精品在线| 欧美电影在线观看高清| 日韩在线不卡视频| 成人av在线亚洲| 91国内在线视频| 国自在线精品视频| 91精品国产亚洲| 亚洲精品自拍第一页| 亚洲成成品网站| 亚洲精品国产精品久久清纯直播| 欧美刺激性大交免费视频| 97在线视频免费| 国产精品麻豆va在线播放| 日韩有码片在线观看| 亚洲另类图片色| 久久久久国产精品一区| 亚洲性视频网站| 国产日韩欧美视频在线| 在线观看91久久久久久| 国产一级揄自揄精品视频| 欧美一区二粉嫩精品国产一线天| 日韩三级成人av网| 九九精品视频在线观看| 色婷婷久久av| 亚洲欧美成人在线| 欧美视频二区36p| 中国日韩欧美久久久久久久久| 久久久国产一区二区三区| 1769国内精品视频在线播放| 欧美高清videos高潮hd| 91亚洲精品久久久| 亚洲精品在线视频| 精品国产一区二区三区四区在线观看| 青青草原一区二区| 高清在线视频日韩欧美| 久久91精品国产91久久跳| 欧美又大粗又爽又黄大片视频| 亚州国产精品久久久| 国产精品久久久久久久久久久久久久| 欧洲日韩成人av| 最近的2019中文字幕免费一页| 欧美成人精品h版在线观看| 日韩成人中文字幕在线观看| 亚洲第一区中文字幕| 国产精品久久77777| 日韩精品免费一线在线观看| 久久久久在线观看| 裸体女人亚洲精品一区| 亚洲网站在线观看| 激情av一区二区| 亚洲人成网站999久久久综合| 亚洲国产91精品在线观看| 国产成人鲁鲁免费视频a| 26uuu另类亚洲欧美日本一| 欧美理论电影在线观看| 8050国产精品久久久久久| 久久久久在线观看| 亚洲第一页自拍| 国产精品高潮呻吟视频| www国产91| 国产亚洲欧美日韩一区二区| 久久久久久久久久久网站| 久久精品视频在线| 欧美日韩aaaa| 日韩欧美国产网站| 国产v综合ⅴ日韩v欧美大片| 国产福利精品视频| 国产欧美一区二区三区在线| 国产精品成人久久久久| 欧美中文字幕第一页| 久久国产精品首页| 欧美—级a级欧美特级ar全黄| 久久久久久久久久久久久久久久久久av| 国产精品69久久久久| 亚洲欧美日韩一区二区三区在线| 日韩电影免费观看在线观看| 91在线观看免费高清完整版在线观看| 色爱av美腿丝袜综合粉嫩av| 亚洲国产精品悠悠久久琪琪| 91午夜理伦私人影院| 九九热在线精品视频| 国产精品久在线观看| 美女黄色丝袜一区| 日韩国产一区三区| 欧美大片免费观看| www.欧美三级电影.com| 欧美激情视频在线免费观看 欧美视频免费一| 欧美在线观看日本一区| 亚洲精品第一页| 日韩美女视频免费看| 亚洲国产精品一区二区久| 国产精品视频中文字幕91| 日韩成人av在线| 成人激情春色网| 久久久久久久一区二区| 国产精品视频一区二区三区四| 亚洲第一精品福利| 久久久久国产视频| 日韩av在线直播| 欧美大片免费看| 欧美精品aaa| 在线观看欧美成人| 亚洲国产精品热久久| 日韩成人xxxx| 欧美高清自拍一区| 性欧美办公室18xxxxhd| 国产精品成人品| 亚洲福利在线观看| 国产美女久久精品香蕉69| 日本精品一区二区三区在线播放视频| 欧美放荡办公室videos4k| 91精品国产综合久久香蕉| 日韩在线观看网站| 在线观看日韩视频| 91色视频在线导航| 精品自在线视频|