Python专题:十、字典(2)

字典定义x={}

get()函数

get(参数一,参数二)

参数一:

需要查找的关键词

参数二:

如果关键词不存在get返回的默认值

字典的更新

update()函数,字典y的元素,去更新字典x的元素,少补,异同

字典推导式

y={for in if }

词频统计

p = '''

I heard the echo, from the valleys and the heart

Open to the lonely soul of sickle harvesting

Repeat outrightly, but also repeat the well-being of

Eventually swaying in the desert oasis

I believe I am

Born as the bright summer flowers

Do not withered undefeated fiery demon rule

Heart rate and breathing to bear the load of the cumbersome

Bored

'''

lines = p.strip().split('\n')
words_cnt = {}
for line in lines:
    line = line.replace(',', '').lower()
    words = line.split(' ')
    for word in words:
        words_cnt[word] = words_cnt.get(word, 0) + 1

words_lst = list(zip(words_cnt.values(), words_cnt.keys()))
words_lst.sort()
words_lst.reverse()
for word in words_lst:
    print(word[1], words_cnt[word[1]])

'''三个单引号用来输入多段落字符串

相关推荐

  1. Python基础学习笔记(二)——字典

    2024-05-10 21:28:03       28 阅读
  2. python字典

    2024-05-10 21:28:03       32 阅读
  3. python字典

    2024-05-10 21:28:03       24 阅读
  4. python字典

    2024-05-10 21:28:03       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-10 21:28:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-10 21:28:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-10 21:28:03       82 阅读
  4. Python语言-面向对象

    2024-05-10 21:28:03       91 阅读

热门阅读

  1. 关于学习与智慧

    2024-05-10 21:28:03       30 阅读
  2. 说说SpringBoot自动配置原理

    2024-05-10 21:28:03       38 阅读
  3. thinkphp5 中路由常见的使用方法

    2024-05-10 21:28:03       31 阅读
  4. spring的核心详解

    2024-05-10 21:28:03       33 阅读
  5. office 官方下载地址

    2024-05-10 21:28:03       25 阅读
  6. Gradle设置引用的JAR包不编译到APK中

    2024-05-10 21:28:03       30 阅读
  7. SpringBean详解

    2024-05-10 21:28:03       25 阅读