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

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

Notes of Py for informatics 3

2019-11-09 21:05:48
字體:
來源:轉載
供稿:網友

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
81精品国产乱码久久久久久| 亚洲综合av影视| 成人午夜黄色影院| 久久久国产视频| 国内精品久久久久久久久| 欧洲成人免费aa| 欧美一级片免费在线| 成人国内精品久久久久一区| 浅井舞香一区二区| 国产精品一久久香蕉国产线看观看| 国产精品毛片a∨一区二区三区|国| 欧美理论电影网| 日韩中文字幕在线播放| 亚洲国产私拍精品国模在线观看| 亚洲精品v欧美精品v日韩精品| 国产成人精品视频在线| 亚洲一区二区三区在线免费观看| 久久精品久久精品亚洲人| 亚洲人成在线播放| 久久久亚洲天堂| 夜夜嗨av一区二区三区四区| 欧美激情欧美激情在线五月| 国产一区二区三区丝袜| 国产美女被下药99| 97不卡在线视频| 色偷偷88888欧美精品久久久| 在线播放日韩欧美| 国产一区二区日韩| 97久久伊人激情网| 久久国产视频网站| 国产视频精品久久久| 久久人人爽人人爽爽久久| 欧美精品在线免费播放| 伊人久久男人天堂| 亚洲一区国产精品| 日韩欧美亚洲范冰冰与中字| 欧美在线视频一二三| 久久久久久免费精品| 美女福利视频一区| 亚洲精品自在久久| 亚洲自拍偷拍一区| 久热精品视频在线观看一区| 国内精品视频在线| 亚洲毛片一区二区| 欧美裸体xxxxx| 91最新国产视频| 欧美日韩激情美女| 国产成人精品一区| 日本人成精品视频在线| 欧美在线视频导航| 国产一区二区三区免费视频| 成人av在线天堂| 日韩美女视频在线观看| 国产一区二区日韩精品欧美精品| 精品国产一区二区三区久久久狼| 日韩精品免费在线| 欧美午夜激情视频| 欧美一区二区三区……| 日韩成人av网| 亚洲美女久久久| 国产成人高潮免费观看精品| 91精品久久久久久久久久久久久| 久久成人一区二区| 久热精品视频在线| 国产成人精品一区二区三区| 亚洲欧美日韩中文在线制服| 国产精品27p| 最近2019年日本中文免费字幕| 中文字幕精品在线| 久久亚洲欧美日韩精品专区| 国产97免费视| 日本高清视频一区| 欧美性色19p| 久久99久久99精品免观看粉嫩| 欧美贵妇videos办公室| 97在线精品国自产拍中文| 中文字幕免费精品一区| 亚洲第一区第一页| 亚洲精品视频免费| 97精品视频在线| 日韩经典中文字幕在线观看| 中文国产成人精品久久一| 精品久久久一区二区| 久久久天堂国产精品女人| 国产精品69精品一区二区三区| 久久99精品国产99久久6尤物| 亚洲精品99久久久久| 欧美国产激情18| 国产一区玩具在线观看| 欧美精品午夜视频| 欧美亚洲日本黄色| 欧美国产激情18| 亚洲最新av网址| 亚洲欧洲一区二区三区久久| 亚洲激情久久久| 91丨九色丨国产在线| 久久国产加勒比精品无码| 国产美女精品免费电影| 国产一区香蕉久久| 久久久久在线观看| 欧美成人自拍视频| 国产99久久精品一区二区 夜夜躁日日躁| 久久国产精品电影| 国产日韩欧美视频在线| 亚洲欧美中文日韩在线v日本| 亚洲高清不卡av| 亚洲第一视频在线观看| 国产成人亚洲综合91| 国产亚洲视频在线| 美女福利精品视频| 国产一区二区久久精品| 亚洲人成网7777777国产| 久久人人爽亚洲精品天堂| 这里只有精品视频在线| 亚洲自拍欧美另类| 在线观看亚洲区| 91精品视频免费| 久久久久久久久电影| xvideos成人免费中文版| 亚洲国产精品小视频| 57pao精品| 日韩二区三区在线| 91久久精品在线| 国产精品电影网| 久久久国产精品免费| 久久国产视频网站| 国产精品青青在线观看爽香蕉| 欧美国产日韩中文字幕在线| 欧美成人免费全部观看天天性色| 欧美午夜宅男影院在线观看| 超在线视频97| 亚洲男人7777| 久久69精品久久久久久久电影好| 亚洲午夜女主播在线直播| 国产精品吊钟奶在线| 久久精品国产欧美激情| 亚洲第一精品自拍| 国产精品电影一区| 亚洲精品ady| 日韩一区在线视频| 亚洲大尺度美女在线| 亚洲成年网站在线观看| 欧美国产日韩一区二区| 亚洲综合精品一区二区| 黑人巨大精品欧美一区免费视频| 久久久精品国产网站| 精品国产一区二区三区久久久| 欧美与黑人午夜性猛交久久久| 欧美巨大黑人极品精男| 国产精品久久久久久久久久久新郎| 国产精品网红直播| 日韩视频在线观看免费| 国语自产在线不卡| 国产大片精品免费永久看nba| 美女av一区二区三区| 欧美在线激情网| 欧美性少妇18aaaa视频| 国产午夜精品一区理论片飘花| 久久久女女女女999久久| 欧美午夜视频在线观看| 国产精品美腿一区在线看| 精品无人区乱码1区2区3区在线| 国语自产在线不卡| 精品中文字幕视频|