当前位置: 首页 > news >正文

【Python】字典 - Dictionary

字典 - Dictionary

  • keys()
  • values()
  • items()
  • get()
  • 获取文件中指定字符的个数
  • 进阶版:获取所有单词的频数
  • 进阶版:获取所有字符的频数

函数内容
keys()输出字典中的所有键
values()输出字典中的所有值
items()以元组的形式输出键值对
get()获取字典中指定键的值

keys()

test = {'chuck':1, 'fred':42, 'jan':100}
print(test.keys())
if "chuck" in test.keys():print("true")

keys() 方法将字典 test 中所有的键输出;
输出结果为:

dict_keys(['chuck', 'fred', 'jan'])
true

values()

test = {'chuck':1, 'fred':42, 'jan':100}
print(test.values())
if 1 in test.values():print("true")

values() 方法将字典 test 中所有的值输出;
输出结果为:

dict_values([1, 42, 100])
true

items()

test = {'chuck':1, 'fred':42, 'jan':100}
print(test.items())
for k, v in test.items():print("key =", k, "while values =", v)

items() 方法将字典 test 中的键值对输出;
输出结果为:

dict_items([('chuck', 1), ('fred', 42), ('jan', 100)])
key = chuck while values = 1
key = fred while values = 42
key = jan while values = 100

get()

inventory = {"apples":430, "banana":312, "pears":223, "oranges":221}print(inventory["banana"])
print(inventory.get("banana"))
print(inventory.get("banana",0))

get() 方法获取字典中指定键的值;
上述三种获取方法结果相同,而最后一种在找不到指定键的时候不会报错退出,而是会输出指定值,在这里是 0


获取文件中指定字符的个数

方法一:统计单个元素的个数

f = open('scarlet.txt', 'r')
txt = f.read()t_count = 0
for c in txt:if c == 't':t_count = t_count + 1
print("t: " + str(t_count) + " occurrences")

方法二:统计多个元素的个数

f = open('scarlet.txt', 'r')
txt = f.read()letter_counts = {}
letter_counts['t'] = 0
letter_counts['s'] = 0
for c in txt:if c == 't':letter_counts[c] = letter_counts[c] + 1elif c == 's':letter_counts[c] = letter_counts[c] + 1print("t: " + str(letter_counts['t']) + " occurrences")
print("s: " + str(letter_counts['s']) + " occurrences")

方法三:统计所有元素的个数

f = open('scarlet.txt', 'r')
txt = f.read()letter_counts = {}
for c in txt:if c not in letter_counts:letter_counts[c] = 0letter_counts[c] = letter_counts[c] + 1print("t: " + str(letter_counts['t']) + " occurrences")
print("s: " + str(letter_counts['s']) + " occurrences")

进阶版:获取所有单词的频数

word_counts = {}for word in sentence.split():word_counts[word] = word_counts.get(word, 0) + 1

进阶版:获取所有字符的频数

char_d = {}
for c in stri:char_d[c] = char_d.get(c, 0) + 1
http://www.lryc.cn/news/11087.html

相关文章:

  • LeetCode分类刷题----二叉树
  • Zipkin : Golang 微服务全链路监控(三)
  • 5.3 BGP路由黑洞
  • STM32 DFU模式烧录代码
  • 松下PLC通过fpwin上传写入MRTC模块方法
  • 就业大山之下的网络安全:安逸的安服仔
  • JavaWeb3-线程的3种创建方式7种写法
  • 驱动调试手段
  • [RK3568 Android12] 音频及路由
  • C++——C++11 第一篇
  • Spring Data JPA 中 CrudRepository 和 JpaRepository 的区别
  • 推荐几款好用的数据库管理工具
  • DPDK — 性能优化手段
  • Fedora Linux未来五年规划
  • 【C++之容器篇】map和set常见函数接口的使用与剖析
  • 虚拟DOM是什么
  • 进程通信方式
  • 强化学习基础知识
  • LeetCode230218_148、654. 最大二叉树
  • WordPress 是什么?.com 和 .org 的 WordPress 有什么差异?
  • java8新特性【2023】
  • 刷题记录:牛客NC51101Lost Cows
  • 华为OD机试 - 不等式 | 备考思路,刷题要点,答疑 【新解法】
  • GuLi商城-SpringCloud-OpenFeign测试远程调用
  • 阿里云_山东鼎信短信的使用(云市场)
  • 基于虚拟机机的代码保护技术
  • Win10耳机有声音麦不能说话怎么办?麦克风说话别人听不到解决方法
  • The 22nd Japanese Olympiad in Informatics (JOI 2022/2023) Final Round 题解
  • openEuler RISC-V 成功适配 VisionFive 2 单板计算机
  • 2005-2022中国企业对外直接投资、OFDI海外投资明细、中国全球投资追踪数据CGIT(含非建筑施工类问题投资)