# zip()函数和sorted()函数
# zip()函数:将两个序列合并,返回zip对象,可强制转换为列表或字典
# sorted()函数:对序列进行排序,返回一个排序后的新列表,原数据不改变
# 合并两个列表,以列表类型输出
list_str = ['a', 'b', 'c', 'd']
list_num = [1, 2, 3, 4]
list_new = zip(list_str, list_num)
print("zip结果(列表):", list(list_new))
# 合并两个字符串,以字典类型输出
str = 'abcd'
str2 = '1234'
list_new = zip(str, str2)
print("zip结果(字典):", dict(list_new))
# 使用zip()和sorted()对字典排序
dict_data = {'a': '4', 'b': '1', 'c': '3', 'd': '2'}
print("直接取字典最小值:", min(dict_data.items()))
print("直接对字典排序:", sorted(dict_data.items()))
list_temp = zip(dict_data.values(), dict_data.keys())
print("zip处理后的最小值:", min(list_temp))
list_temp = zip(dict_data.values(), dict_data.keys())
list_temp = sorted(list_temp)
print("zip处理后的排序:", list_temp)
print("zip处理后的最小两个:", list_temp[0:2])
运行结果:
zip结果(列表): [('a', 1), ('b', 2), ('c', 3), ('d', 4)]
zip结果(字典): {'a': '1', 'b': '2', 'c': '3', 'd': '4'}
直接取字典最小值: ('a', '4')
直接对字典排序: [('a', '4'), ('b', '1'), ('c', '3'), ('d', '2')]
zip处理后的最小值: ('1', 'b')
zip处理后的排序: [('1', 'b'), ('2', 'd'), ('3', 'c'), ('4', 'a')]
zip处理后的最小两个: [('1', 'b'), ('2', 'd')]
python的reduce,map,zip,filter和sorted函数
一. reduce(function,Iterable),它的形式和map()函数一样.不过参数function必须有两个参数. reduce()函数作用是:把结果继续和序列的下一个元素做累积计 ...
Python的operator.itemgetter函数和sorted函数
写这篇文章的目的是之前在用Python3实现KNN算法时用到的几个函数不太懂, 地址: 1- https://github.com/hitergelei/Self-Lear ...
Python【day 14】sorted函数、filter函数和map函数的区别
sorted函数.filter函数和map函数的区别1.作用 前者用于排序, 中者用于筛选, 后者用于返回值(不是特定的筛选或者排序)2.写法 前者 sorted(iterable,key=自定义函数 ...
reversed()函数和sorted()函数
#reversed()反转排序,可对列表.元组.区间等进行排序 #练习1 a = range(10) a_list = [x for x in reversed(a)] print(a_list) # ...
Python3 sorted() 函数
Python3 sorted() 函数 Python3 内置函数 描述 sorted() 函数对所有可迭代的对象进行排序操作. sort 与 sorted 区别: sort 是应用在 list 上的 ...
python 小技巧(glob,guid,序列化,压缩字符,有序字典,sorted函数,分片)
1.glob模块 glob模块是最简单的模块之一,内容非常少.用它可以查找符合特定规则的文件路径名.跟使用windows下的文件搜索差不多.查找文件只用到三个匹配符:”*”, “?”, “[]”.”* ...
python基础——sorted()函数
python基础——sorted()函数 排序算法 排序也是在程序中经常用到的算法.无论使用冒泡排序还是快速排序,排序的核心是比较两个元素的大小.如果是数字,我们可以直接比较,但如果是字符串或者两个d ...
Python中的sorted函数以及operator.itemgetter函数 【转载】
operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [1,2 ...
Python中的sorted函数以及operator.itemgetter函数
operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [1,2 ...
随机推荐
关于 Pragma 的使用总结
注意:此文乃是本人阅读多个博客文章后,记下的个人认为重点的地方. 参考文章: 参考1 参考2 #Pragma mark - 用于分离类中的不同功能的方法.(例如,一个 viewController ...
sdut 2831 Euclid (几何)
题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2831 题意:给a, b, c, d, ...
解读JavaScript代码 var ie = !-[1,]
var ie = !-[1,]: 这句代码在IE9之前曾被称为世界上最短的IE判定代码.代码虽短但确包含了不少javascript基础知识在里面.在这个例子中代码执行时会先调用数组的toString( ...
iOS中静态库-.a文件生成和使用
最近在使用使用一个网上的Demo的时候. 出现另一令人烦恼的问题 . 就是它里面有嵌套的工程. 如下图所示. 工程里面还嵌套有一个工程. 真的是让人煞费苦心 …其实这个问题看起来并不是很难, 如果是一 ...
java SWT嵌入IE,SafeArray .
java SWT嵌入IE,SafeArray ); // Create a by ref variant Variant variantByRef = new Variant(pVaria ...
让Unity的Inspector面板支持字符限制(restrict)功能
今天在优化红点组件,笔者打算将红点id由10进制改为16进制处理,就打算将红点id字段由uint类型改成string类型,用于填写16进制的字符(因为在Inspector面板里,uint/int类型字 ...
Two Sum IV - Input is a BST
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
python3 三级菜单-基础版
# -*- coding:utf-8 -*- data = { "北京":{ "东城区":{ "安定门":["国子监", ...
Cocos Creator 获得设备分辨率
var b = cc.director.getWinSizeInPixels() var bx = b.width var by = b.height
Android Studio主题设置、颜色背景配置
打开http://color-themes.com/有很多样式可供选择 导入方式 下载主题—xxx.jar 注意:如果我们下载下来的jar名字如果有空格,一定要把空格去掉,同时文件路径中不要含有中文 ...