集合数据类型:
输入代码:
1 b=[1,2,2,7,9,5,5,8,9,1,3,3] 2 s=set(b) 3 print(s) 4 5 s.add(4) 6 print(s) 7 8 s.remove(7) 9 print(s) 10 11 s1=set([1,2,3]) 12 s2=set([2,3,4]) 13 print(s1&s2) 14 print(s1|s2) 15 print(s1-s2)
运行结果:
英文词频统计
1、str
输入代码:
1 strThousand = '''Twilight 4Ever 2 Heartbeats fast 3 Colors and promises 4 How to be brave 5 How can I love when I'm afraid to fall 6 But watching you stand alone 7 All of my doubt suddenly goes away somehow 8 One step closer 9 I have died everyday waiting for you 10 Darling don't be afraid I have loved you 11 For a thousand years 12 I love you for a thousand more 13 Time stands still 14 Beauty in all she is 15 I will be brave 16 I will not let anything take away 17 What's standing in front of me 18 Every breath 19 Every hour has come to this 20 One step closer 21 I have died everyday waiting for you 22 Darling don't be afraid I have loved you 23 For a thousand years 24 I love you for a thousand more 25 And all along I believed I would find you 26 Time has brought your heart to me 27 I have loved you for a thousand years 28 I love you for a thousand more 29 One step closer 30 One step closer 31 I have died everyday waiting for you 32 Darling don't be afraid I have loved you 33 For a thousand years 34 I love you for a thousand more 35 And all along I believed I would find you 36 Time has brought your heart to me 37 I have loved you for a thousand years 38 I love you for a thousand more''' 39 strList = strThousand.split(' ') 40 print(len(strList),max(strList)) 41 print(strList.sort())
运行结果:
2、List
输入代码:
1 strThousand = '''Twilight 4Ever 2 Heartbeats fast 3 Colors and promises 4 How to be brave 5 How can I love when I'm afraid to fall 6 But watching you stand alone 7 All of my doubt suddenly goes away somehow 8 One step closer 9 I have died everyday waiting for you 10 Darling don't be afraid I have loved you 11 For a thousand years 12 I love you for a thousand more 13 Time stands still 14 Beauty in all she is 15 I will be brave 16 I will not let anything take away 17 What's standing in front of me 18 Every breath 19 Every hour has come to this 20 One step closer 21 I have died everyday waiting for you 22 Darling don't be afraid I have loved you 23 For a thousand years 24 I love you for a thousand more 25 And all along I believed I would find you 26 Time has brought your heart to me 27 I have loved you for a thousand years 28 I love you for a thousand more 29 One step closer 30 One step closer 31 I have died everyday waiting for you 32 Darling don't be afraid I have loved you 33 For a thousand years 34 I love you for a thousand more 35 And all along I believed I would find you 36 Time has brought your heart to me 37 I have loved you for a thousand years 38 I love you for a thousand more''' 39 strList = strThousand.split(' ') 40 print(strList) 41 print(set(strList)) 42 43 for word in strList: 44 print(word,strList.count(word))
运行结果:
3、Dict
输入代码:
1 strThousand = '''Twilight 4Ever 2 Heartbeats fast 3 Colors and promises 4 How to be brave 5 How can I love when I'm afraid to fall 6 But watching you stand alone 7 All of my doubt suddenly goes away somehow 8 One step closer 9 I have died everyday waiting for you 10 Darling don't be afraid I have loved you 11 For a thousand years 12 I love you for a thousand more 13 Time stands still 14 Beauty in all she is 15 I will be brave 16 I will not let anything take away 17 What's standing in front of me 18 Every breath 19 Every hour has come to this 20 One step closer 21 I have died everyday waiting for you 22 Darling don't be afraid I have loved you 23 For a thousand years 24 I love you for a thousand more 25 And all along I believed I would find you 26 Time has brought your heart to me 27 I have loved you for a thousand years 28 I love you for a thousand more 29 One step closer 30 One step closer 31 I have died everyday waiting for you 32 Darling don't be afraid I have loved you 33 For a thousand years 34 I love you for a thousand more 35 And all along I believed I would find you 36 Time has brought your heart to me 37 I have loved you for a thousand years 38 I love you for a thousand more''' 39 strList = strThousand.split() 40 print(len(strList),max(strList)) 41 42 strSet = set(strList) 43 print(len(strSet),strSet) 44 45 strDict = {} 46 for word in strSet: 47 strDict[word] = strList.count(word) 48 49 print(strDict['thousand']) 50 print(len(strDict),strDict)
运行代码: