Python列表或字典排序
2017-11-13 22:30
357 查看
1.列表排序
s = [‘Chr1-10.txt’,’Chr1-1.txt’,’Chr1-2.txt’,’Chr1-14.txt’,’Chr1-3.txt’,’Chr1-20.txt’,’Chr1-5.txt’]s_1 = sorted(s, key=lambda d : int(d.split(‘-‘)[-1].split(‘.’)[0]))
print (s_1)
sort三种方式
print(sorted([5, 2, 3, 1, 4]))
“”“第一:只需要调用sorted()方法”“”
a = [5, 2, 3, 1, 4]
a.sort()
print(a)
“”“第二:使用list.sort()方法来排序”“”
student_tuples = [(‘john’, ‘A’, 15),(‘jane’, ‘B’, 12),(‘dave’, ‘B’, 10)]
print(sorted(student_tuples, key=lambda student: student[2]))
“”“第三:多层list,使用sorted(…)方法”“”
2.字典排序
dic = {‘a’:3 , ‘b’:2 , ‘c’: 1}print (dic[‘c’])
print (dic.items())
sortDic = sorted(dic.items(), key=lambda item:item[1], reverse=False)
sortDic = sorted(dic.items(), key=lambda asd:asd[1], reverse=True)
print(sortDic)
函数说明
“””函数原型
sorted(dic,value,reverse)
dic为比较函数,value 为排序的对象(这里指键或键值),
reverse:注明升序还是降序,True–降序,False–升序(默认)
dic的迭代器
value指定key或value,此处用个lamda表达式,取key或value
“”“
相关文章推荐
- python的排序函数sort,sorted在列表排序和字典排序中的应用详解和举例
- Python中 字典排序、列表排序
- Python中 字典排序、列表排序
- python的排序函数sort,sorted在列表排序和字典排序中的应用详解和举例
- Python中 字典排序、列表排序
- python列表,字典排序
- Python中 字典排序、列表排序
- python 字典排序,列表排序详细
- Python中 字典排序、列表排序
- python列表字典排序
- python中列表和字典排序
- Python 列表解析和生成器表达式
- Python之列表的插入&替换修改方法
- 第33课 Python列表与函数行参返回值问题
- python的元组、列表、字典
- python 列表合并
- Python3 列表
- Python 列表函数
- 列表生成式 是Python内置的非常简单却强大的可以用来创建list的生成式。
- 零基础学python-19.4 为什么要在意列表解析与序列映射函数map