zoukankan      html  css  js  c++  java
  • 组合数据类型,英文词频统计

    集合数据类型:

    输入代码:

     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)

    运行代码:

  • 相关阅读:
    CF1042E Vasya and Magic Matrix
    Luogu 4868 Preprefix sum
    CF1042F Leaf Sets
    CF1041F Ray in the tube
    【Luogu】P1410子序列(DP)
    【Luogu】P1383高级打字机
    【Luogu】P1681最大正方形2(异或运算,DP)
    【Luogu】P1122最大子树和(DFS,树上DP)
    【Luogu】P2258子矩阵(状态压缩,DP)
    【Luogu】P2158仪仗队(欧拉函数)
  • 原文地址:https://www.cnblogs.com/la-vie/p/9679930.html
Copyright © 2011-2022 走看看