• 作业5


    1.

    d={'001':'88','002':'84','003':'89','004':'86','005':'82'}
    >>> d['002']
    '84'
    >>> d.get('003')

    SyntaxError: unexpected indent
    >>> d.get('003')
    '89'
    >>> d.pop('003')
    '89'
    >>> d.get('003')
    >>> print(d.get('003'))
    None
    >>> d
    {'001': '88', '002': '84', '004': '86', '005': '82'}
    >>> d.keys()
    dict_keys(['001', '002', '004', '005'])
    >>> d.values()
    dict_values(['88', '84', '86', '82'])
    >>> d.items()
    dict_items([('001', '88'), ('002', '84'), ('004', '86'), ('005', '82')])

    2、列表,元组,字典,集合的遍历。

    l=list('12438849374837')
    for i in l:
        print(l)

    t=tuple('nhaildhabvka')
    for i in t:
        print(t)

    d={'0001':'99','0003':'89','0004':'98','0005':'100','0006':'78'}
    for i in d:
        print(d)

    s=set(l)
    for i in s:
        print(s)

    总结列表,元组,字典,集合的联系与区别。

    列表:是一组有序序列,能进行查询,增加,删除,修改。是用中括号【】括起来的。

    元组:是一组有序序列,能进行查询。是用小括号()括起来的。

    字典:字典不是有序序列,是由一组组键值对组成。能进行增删改查等操作。是用大括号{}括起来的。

    集合:集合也不是有序序列,能进行查询等操作。是用大括号{}括起来的。

    3、英文词频统计实例

    A、待分析字符串

    B、分解提取单词

    a、大小写 txt.lower()

    b、分隔符'.,:;?!-_’

    c、单词列表

    C、单词计数字典

    t='''Wake up in the morning feeling like P Diddy
    Put my glasses on, I'm out the door
    I'm gonna hit this city (Let's go)
    Before I leave,
    Brush my teeth with a bottle of Jack
    Cause when I leave for the night,
    I ain't coming back
    I'm talking - pedicure on our toes, toes
    Trying on all our clothes, clothes
    Boys blowing up our phones, phones
    Drop-toping, playing our favorite CDs
    Pulling up to the parties
    Trying to get a little bit tipsy
    Don't stop, make it pop
    DJ, blow my speakers up
    Tonight, Imma fight
    Till we see the sunlight
    Tick tock, on the clock
    But the party don't stop
    Woah-oh oh oh
    Woah-oh oh oh
    Don't stop, make it pop
    DJ, blow my speakers up
    Tonight, Imma fight
    Till we see the sunlight
    Tick tock, on the clock
    But the party don't stop
    Woah-oh oh oh
    Woah-oh oh oh
    Ain't got a care in world,
    But got plenty of beer
    Ain't got no money in my pocket,
    But I'm already here
    And now, the dudes are lining up
    Cause they hear we got swagger
    But we kick 'em to the curb
    unless they look like Mick Jagger
    I'm talking about
    everybody getting crunk, crunk
    Boys trying to touch my junk, junk
    Gonna smack him if he getting too drunk, drunk
    Now, now - we goin' til they kick us out, out
    Or the police shut us down, down
    Police shut us down, down
    Po-po shut us -
    Don't stop, make it pop
    DJ, blow my speakers up
    Tonight, Imma fight
    Till we see the sunlight
    Tick tock, on the clock
    But the party don't stop
    Woah-oh oh oh
    Woah-oh oh oh
    Don't stop, make it pop
    DJ, blow my speakers up
    Tonight, Imma fight
    Till we see the sunlight
    Tick tock, on the clock
    But the party don't stop
    Woah-oh oh oh
    Woah-oh oh oh
    DJ, you build me up
    You break me down
    My heart, it pounds yeah, you got me
    With my hands up
    You got me now
    You gotta that sound yeah, you got me
    DJ, you build me up
    You break me down
    My heart, it pounds yeah, you got me
    With my hands up
    Put your hands up
    Put your hands up
    No, the party don't stop until I walk in
    Don't stop, make it pop
    DJ, blow my speakers up
    Tonight, Imma fight
    Till we see the sunlight
    Tick tock, on the clock
    But the party don't stop
    Woah-oh oh oh
    Woah-oh oh oh
    Don't stop, make it pop
    DJ, blow my speakers up
    Tonight, Imma fight
    Till we see the sunlight
    Tick tock, on the clock
    But the party don't stop
    Woah-oh oh oh
    Woah-oh oh oh
    '''
    t=t.lower()
    for i in ',.?!':
    t=t.replace(i,' ')
    t=t.replace(' ',' ')
    words=t.split(' ')

    s=set(words)
    dic={}

    lis=[]
    value=[]
    for i in s:
    if(i==' '):
    continue
    if(i==''):
    continue
    dic[i]=words.count(i)
    lis.append(words.count(i))
    vgalue=dic.values()

    lis=list(dic.items())
    lis.sort(key=lambda x:x[1],reverse=True)
    for i in range(10):
    print(lis[i])

  • 相关阅读:
    Grandpa's Estate
    The Fortified Forest
    Scrambled Polygon
    Wall
    激情的大三
    无聊的大二
    美好的大一
    高精度 加减乘
    Erasing Edges
    git放弃修改&放弃增加文件
  • 原文地址:https://www.cnblogs.com/lianghaohui123/p/7577332.html
Copyright © 2020-2023  润新知