python基础实战(五)-dictionary,set

python中常用的Contailer Types(类型)有四个:

  • List
  • Tuple
  • Dictionary
    A dictionary is similar to a list, but the order of items doesn’t matter and they aren’t selected by their offset (index). Instead, you specify a unique key to associate with each value. The key is often a string, but it can be any of Python’s immutable types.
  • Set

dictionary和list是相似的,但是没办法通过index去访问,可以通过键查找值。其实用符号为{}。其key的查找相比list的index查找而言,是非常非常快的。

create

# 创建空dictionary
empty_dict={}
empty_dict

# 创建有值dictionary,key-value形式
pizza={
    "size":"medium",
    "type":"one",
    "crust":"cheap",
    "boolean":"true",
}
pizza

//输出结果,注意:dictionary是无顺序的。
{}

{'size': 'medium', 'type': 'one', 'crust': 'cheap', 'boolean': 'true'}

access

# 通过key获取value
pizza['type']
print(pizza.get('boolean'))

# 打印出dictionary所有的key
pizza.keys()
# 打印出dictionary所有的value
pizza.values()
# 打印出所有值
pizza.items()

//输出结果
'one'
true

dict_keys(['size', 'type', 'crust', 'boolean'])
dict_values(['medium', 'one', 'cheap', 'true'])
dict_items([('size', 'medium'), ('type', 'one'), ('crust', 'cheap'), ('boolean', 'true')])

update

# 新增加一个key值为name
pizza['name']=['huo','hu']
pizza

# 更新某个存在的key值对应的value
pizza['boolean']=['false']
pizza

//输出结果
{'size': 'medium',
 'type': 'one',
 'crust': 'cheap',
 'boolean': 'true',
 'name': ['huo', 'hu']}
{'size': 'medium',
 'type': 'one',
 'crust': 'cheap',
 'boolean': ['false'],
 'name': ['huo', 'hu']}

remove

# 删除dictionary中固定的一个key-value信息
del pizza['boolean']
pizza

# 将dictionary清空
pizza.clear()
pizza

//输出结果
{'size': 'medium', 'type': 'one', 'crust': 'cheap', 'name': ['huo', 'hu']}

{}

convert into list

# 将dictionary转成list形式,保存key-value形式
list({"name":'huohuo',"gender":20})
# 将dictionary的value值转成list形式
list({"name":'huohuo',"gender":20}.values())

//输出结果
['name', 'gender']

['huohuo', 20]

python中常用的Contailer Types(类型)有四个:

  • List
  • Tuple
  • Dictionary
  • Set
    A set is like a dictionary with its values thrown away, leaving only the keys.

set和dictionary是非常相像的,但是只是存在key,无value。因为dictionary的key是唯一的,set中的每一个元素也是唯一的,所以一个set,其里边肯定不会存在重复值。其实用符号和dictionary是一致的,所以create需要用set().

create

empty_set=set()
empty_set

# 无重复值,虽然输入了两次4,但是结果是唯一的。empty_set=set()
even_set={2,3,4,4,5}
even_set

//输出结果
set()

{2, 3, 4, 5}

operations

num_set={3,4,6,7,8,9}
num_set
even_set={2,3,4,4,5}
even_set
# and符号,既出现在A又出现在B中
num_set & even_set
# or符号,要么出现在A中要么出现在B中
num_set | even_set
# 相减,出现在A中,未出现在B中
num_set - even_set
# 异或,两个东西不同的时候异或,相同则为0.
num_set ^ even_set

//输出结果
{3, 4, 6, 7, 8, 9}
{2, 3, 4, 5}
{3, 4}
{2, 3, 4, 5, 6, 7, 8, 9}
{6, 7, 8, 9}
{2, 5, 6, 7, 8, 9}

convert into list

# 将set转成list,是无序的。
list({5,6,7,8})
# 可通过sorted()对转成的list排序
sorted(list({5,6,4,7}))

//输出结果
[8, 5, 6, 7]
[4, 5, 6, 7]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值