python之zip函数和sorted函数

# 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‘)]
时间: 2024-08-24 23:34:30

python之zip函数和sorted函数的相关文章

python笔记-lambda函数、sorted函数、map函数

1.lambda函数:又称匿名函数,示例如下: def f(x): return x**2 print f(4)  #16 g = lambda x:x**2 print g(4)  #16 2.map函数 print map(lambda x:x**2,range(10)) #[0, 1, 4, 9, 16, 25, 36, 49, 64, 81] 3.sorted函数 dict = {9:2,4:3,6:9,'a':'test','e':'fff','*':'$'} print sorted

zip函数和sorted函数

###zip函数 如果处理两个列表的话就以列表的形式输出 比如 list_a = [1,2,3,4,5] list_b = ['a','b','c','d','e'] list_c = zip(list_b,list_a) 输出结果为:[('a', 1), ('b', 2), ('c', 3), ('d', 4),('e',5)] # 合并两个字符串,以字典类型输出str_a = "123456"str_b = "abcdef"str_c = zip(str_a,s

python基础——sorted()函数

python基础——sorted()函数 排序算法 排序也是在程序中经常用到的算法.无论使用冒泡排序还是快速排序,排序的核心是比较两个元素的大小.如果是数字,我们可以直接比较,但如果是字符串或者两个dict呢?直接比较数学上的大小是没有意义的,因此,比较的过程必须通过函数抽象出来. Python内置的sorted()函数就可以对list进行排序: >>> sorted([36, 5, -12, 9, -21]) [-21, -12, 5, 9, 36] 此外,sorted()函数也是一个

Python中的sorted函数以及operator.itemgetter函数

operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [1,2,3] >>> b=operator.itemgetter(1)      //定义函数b,获取对象的第1个域的值>>> b(a) 2 >>> b=operator.itemgetter(1,0)  //定义函数b,获取对象的第1个域和第0个的值>&

python函数之sorted与sort

Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列. sorted(iterable,key=None,reverse=False),返回新的列表,对所有可迭代的对象均有效 sort(key=None,reverse=False) 就地改变列表  reverse:True反序:False 正序 实例: >>>sorted([1,5,3,2,9]) [1,2,3,5,9] >>>a=[5,

Python中的sorted函数以及operator.itemgetter函数 【转载】

operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [1,2,3] >>> b=operator.itemgetter(1)      //定义函数b,获取对象的第1个域的值>>> b(a) 2 >>> b=operator.itemgetter(1,0)  //定义函数b,获取对象的第1个域和第0个的值>&

Python中sort以及sorted函数初探

sorted(...) Help on built-in function sorted in module __builtin__: sorted(...) sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list sort(...) Help on built-in function sort: sort(...) L.sort(cmp=None, key=None, reverse=False) -- s

python的zip函数

Python的zip函数 转自http://www.cnblogs.com/frydsh/archive/2012/07/10/2585370.html zip函数接受任意多个(包括0个和1个)序列作为参数,返回一个tuple列表.具体意思不好用文字来表述,直接看示例: 1.示例1: x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] xyz = zip(x, y, z) print xyz 运行的结果是: [(1, 4, 7), (2, 5, 8), (3, 6

python几个排序函数 sort sorted argsort

Python中排序常用到的sort .sorted和argsort函数 [摘要:Python中sort 战 sorted函数 一 .先容 sort函数是list列表中的函数,而 sorted能够对list或iterator举行排序 2.sort战sorted的比拟 1.用sort函数对列表排序时会影响列表自身,而sorted没有会 举例] Python中sort 和 sorted函数    一.介绍 sort函数是list列表中的函数,而sorted可以对list或者iterator进行排序 二