Python字典, 一次选中多个键名
  X5zJxoD00Cah 2023年11月02日 63 0

Python字典, 一次选中多个键名

简单实现 operator.itemgetter(*列表)(字典)
import operator
list_ = [1,4,5,7]
dict_ = {}
for i in list_:
    dict_[i] = i+0.1
operator.itemgetter(*[1,7])(dict_)
# (1.1, 7.1) # 输出键值 类型为元组
list_ = ['a','b','c']
dict_ = {}
for i in list_:
    dict_[i] = 1
operator.itemgetter(*['a', 'b'])(dict_)
# (1, 1) # 输出键值 类型为元组
高级应用
import operator
list_ = [1,4,5,8]
dict_ = {}

# 根据列表 生成键名
# 生成 布尔值的键值
for i in list_:
    dict_[i] = bool( False + (1+ (-1)**i)/2 )
print(dict_, '\n')


# 根据列表 一次选中多个键名 输出键值 类型为元组 operator.itemgetter(*列表)(字典)
# 根据列表 从字典中提取子集 {key: value for key, value in 字典.items() if key in 列表}
def dict_sub(字典= {1:1, 2:2, 3:3}, 列表= [1, 2]):
    dict_sub = {key: value for key, value in 字典.items() if key in 列表}
    return dict_sub

# 存在... = 至少有一个是... if True in
tupl_ = operator.itemgetter(*[1,4])(dict_)
if True in tupl_:
    print(dict_sub(dict_, [1,4]), "至少有一个是True")
if False in tupl_:
    print(dict_sub(dict_, [1,4]), "至少有一个是False", '\n')

# 判断列表或元组, 集合或字典中的元素 全部都不是... if not True in
tupl_ = operator.itemgetter(*[1,5])(dict_)
if not True in tupl_:
    print(dict_sub(dict_, [1,5]), "全部都不是True")
tupl_ = operator.itemgetter(*[4,8])(dict_)
if not False in tupl_:
    print(dict_sub(dict_, [4,8]), "全部都不是False", '\n')

# 判断列表或元组, 集合或字典中的元素 全部都是... if all() is True
tupl_ = operator.itemgetter(*[4,8])(dict_)
if all(tupl_):
    print(dict_sub(dict_, [4,8]), "全部都是True")
tupl_ = operator.itemgetter(*[1,5])(dict_)
if all(tupl_) is False:
    print(dict_sub(dict_, [1,5]), "全部都是False")
'''
{1: False, 4: True, 5: False, 8: True} 

{1: False, 4: True} 至少有一个是True
{1: False, 4: True} 至少有一个是False 

{1: False, 5: False} 全部都不是True
{4: True, 8: True} 全部都不是False 

{4: True, 8: True} 全部都是True
{1: False, 5: False} 全部都是False
'''
引申学习
'''从字典中提取键名'''
dict_ = {1:True, 2:False, 3:True}

# 常规方法
list(dict_.keys())

# 提取方法
[ key for key in dict_ ]

# 注意区分字典 字典.items() 字典.keys() 字典.values()
'''从字典中提取符合条件的子集的键名'''
dict_ = {1:True, 2:False, 3:True}
# 常规方法
list( {key: value for key, value in dict_.items() if value == True}.keys() )

# 直接提取
[ key for key in dict_ if dict_[key] == True ]

'''多条件提取'''
[ key for key in dict_ if (dict_[key] == True) & (key in [1,2]) & (key in [1,3]) ]

'''
尽量用 & | 代替 and or 因为
DataFrame的切片中, and or会报错
如果是数值变量 & | 表示位运算, and or则依据是否非0来决定输出
如果是逻辑变量, 则无区别
'''
'''选择字典键值最大 输出对应键名'''
dict_ = {1:1, 2:2}
max(dict_, key= dict_.get)
max(dict_.items(), key= lambda x: x[1])[0] # 输出(键, 值)元组列表 通过[0]指定键值

'''if 字典 非空 输出 空 等价于False 但是不is False'''
# python 如何判断字典是否为空?
# https://cloud.tencent.com/developer/article/1569560

dict_1 = {1:1, 2:2}
dict_2 = {}
if dict_1:
    print(1)
if dict_2:
    print(2.1)
else:
    print(2.2)

'''根据字典键值排序 输出对应键名排序后列表'''
dict_ = {'a':1, 'b':2}
sorted(dict_, key= dict_.get, reverse= True)

list_tuple = sorted(dict_.items(), key= lambda x: x[1], reverse= True) # 输出(键, 值)元组列表
[x for x,_ in list_tuple]
[x[0] for x in list_tuple]
from operator import itemgetter
list(map(itemgetter(0), list_tuple))
参考文献

从字典类型类型中一次性取多个key的value

https://blog.csdn.net/ningyanggege/article/details/125547768

从字典中提取子集

https://python3-cookbook.readthedocs.io/zh_CN/latest/c01/p17_extract_subset_of_dict.html

Python字典切片

https://blog.csdn.net/irober/article/details/113650213

def 字典切片(字典, 开始索引, 结束索引):
    键名 = 字典.keys()
    字典切片 = {}
    for k in list(键名)[开始索引: 结束索引]:
        字典切片[k] = 字典[k]
    return 字典切片

Python 中 (&,|)和(and,or)之间的区别

https://blog.csdn.net/weixin_40041218/article/details/80868521

获取Python元组列表中的第一个元素

https://blog.csdn.net/weixin_44227389/article/details/113872781

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  X5zJxoD00Cah   2023年12月11日   26   0   0 知乎Python迭代器
  X5zJxoD00Cah   2023年12月12日   34   0   0 Python.net
X5zJxoD00Cah