Loading

结构化数据_字典

一、字典数据类型:

>>> myCat = {"size":"fat","color":"gray","disposition":"loud"}
>>> myCat["size"]
'fat'
>>> "my cat has " + myCat["color"] + " fur"
'my cat has gray fur'

像列表一样,“字典”是许多只的集合。但不想列表的下标,字典的索引可以使用许多不同数据类型,不只是整数。字典的索引被称为“键”,键即其关联的值称为“键-值”对。

 

>>> spam = {1234:"lanuage combination",42:"The answer"}
>>> spam[1234]
'lanuage combination'

字典可以使用整数值作为键。

 

  • 列表与字典
>>> spam = ["cat","dogs","moose"]
>>> bacon = ["dogs","cat","moose"]
>>> spam == bacon
False
>>> eggs = {"name":"zophie","species":"cat","age":8}
>>> ham = {"species":"cat","age":8,"name":"zophie"}
>>> eggs == ham
True

与列表不同,字典中的表项是不排序的。

 

  • keys()、values()和items()方法
复制代码
>>> birthday = {"Alice":"Apr 1","Bob":"Dec 12","Carol":"Mar 4"}
>>> for k in birthday.keys():
...     print(k)
...
Alice
Bob
Carol
>>> for v in birthday.values():
...     print(v)
...
Apr 1
Dec 12
Mar 4
>>> for item in birthday.items():
...     print(item)
...
('Alice', 'Apr 1')
('Bob', 'Dec 12')
('Carol', 'Mar 4')
复制代码

 

  • 检查字典中是否存在键或值
复制代码
>>> birthday = {"Alice":"Apr 1","Bob":"Dec 12","Carol":"Mar 4"}
>>> "Alice" in birthday.keys()
True
>>> "Apr 1" in birthday.values()
True
>>> "sss" not in birthday.keys()
True
>>> "sss" in birthday.keys()
False
复制代码

 

  • get()方法

get()方法,它有两个参数:取得其值得键,以及如果该键不存在是,返回的备用值。

>>> picnicItems = {"apples":5,"cups":2}
>>> "I am bringing " + str(picnicItems.get("cups",0)) + " cups"
'I am bringing 2 cups'
>>> "I am bringing " + str(picnicItems.get("eggs",0)) + " eggs."
'I am bringing 0 eggs.'

 

  •  setdefault()方法

传递给该方法的第一个参数,是要检查的键。第二个参数,是如果该键不存在时要设置的值。如果该键确实存在,方法就会返回键的值。

>>> spam = {"name":"Pooka","age":5}
>>> spam.setdefault("color","black")
'black'
>>> spam
{'name': 'Pooka', 'age': 5, 'color': 'black'}
>>> spam.setdefault("color","white")
'black'

 

复制代码
import pprint
message = "It was a bright cold day in April, and the clocks were strking thirteen."
count = {}

for character in message:
    count.setdefault(character,0)
    count[character] = count[character] + 1

pprint.pprint(count)

----------------------------------------
{' ': 13,
 ',': 1,
 '.': 1,
 'A': 1,
 'I': 1,
 'a': 4,
 'b': 1,
 'c': 3,
 'd': 3,
 'e': 5,
 'g': 2,
 'h': 3,
 'i': 5,
 'k': 2,
 'l': 3,
 'n': 4,
 'o': 2,
 'p': 1,
 'r': 5,
 's': 3,
 't': 6,
 'w': 2,
 'y': 1}
复制代码

 

  • 嵌套的字典和列表
复制代码
allGuests = {
    "Alice":{"apples":5,"pretzels":12},
    "Bob":{"ham sandwiches":3,"apples":2},
    "Carol":{"cups":3,"apple pies":1}
}

def totalBrought(guests,item):
    numBrought = 0
    for k,v in guests.items():
        numBrought = numBrought + v.get(item,0)
    return numBrought
print("Number of things being brought:")
print("- Apples " + str(totalBrought(allGuests,"apples")))
print("- Cups " + str(totalBrought(allGuests,"cups")))
print("- Cakes " + str(totalBrought(allGuests,"cakes")))
print("- Ham sandwiches " + str(totalBrought(allGuests,"ham sandwiches")))
print("- Apples Pies " + str(totalBrought(allGuests,"apple pies")))


----------------------
Number of things being brought:
- Apples 7
- Cups 3
- Cakes 0
- Ham sandwiches 3
- Apples Pies 1
复制代码

 

作者:solomon-zj

出处:https://www.cnblogs.com/solomon-zj/p/16655544.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   solomon-zj  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示