python中url解析及排序函数sort ()与 sorted() 区别、字典排序及高级用法
最近在工作中需要将url的host和path路径分开存储,python内置的urlparse, 支持我们优雅的解决这个问题,参考地址
>>> from urlparse import urlparse
>>> url = 'https://docs.google.com/spreadsheet/ccc?key=blah-blah-blah-blah#gid=1'
>>> parsed_uri = urlparse(url)
>>> print parsed_uri
ParseResult(scheme='https', netloc='docs.google.com', path='/spreadsheet/ccc', params='', query='key=blah-blah-blah-blah', fragment='gid=1')
由上文我们可以看到,url被拆分的很细致,提取的方法也很简单,只需要给属性取一个别名,后面跟上解析的key,即可匹配出解析文本。
>>> from urlparse import urlparse
>>> pp=u'http://java.aaaaaaa.cn/java/course/javaeeadvanced.shtml'
>>> parsed_uri = urlparse(pp)
>>> parsed_uri
ParseResult(scheme=u'http', netloc=u'java.aaaaaaa.cn', path=u'/java/course/javaeeadvanced.shtml', params='', query='', fragment='')
>>> parsed_uri.netloc
u'java.aaaaaaa.cn'
>>> print parsed_uri.scheme
http
>>> print parsed_uri.netloc
java.aaaaaaa.cn
>>> print parsed_uri.path
/java/course/javaeeadvanced.shtml
对于一个指定的列表,如果想要对它进行排序,python内置了sort和sorted方法,那么这两者又有什么区别呢?
描述
sort 与 sorted 区别:
>>> p=(11,333,22,3,5,6,10)
>>> p.sort()
Traceback (most recent call last):
File "
", line 1, in AttributeError: 'tuple' object has no attribute 'sort'
>>> sorted(p)
[3, 5, 6, 10, 11, 22, 333]
>>> sorted(p,reverse=True)
[333, 22, 11, 10, 6, 5, 3]
语法
准确的说,sorted是个函数,sort是方法。
def sorted(iterable, cmp=None, key=None, reverse=False): # real signature unknown; restored from __doc__
""" sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list """
pass
>>> b = [1,4,5,6,7,123,4,5]
>>> print(sorted(b))
[1, 4, 4, 5, 5, 6, 7, 123]
>>> print(b)
[1, 4, 5, 6, 7, 123, 4, 5]
def sort(self, cmp=None, key=None, reverse=False): # real signature unknown; restored from __doc__
"""
L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
cmp(x, y) -> -1, 0, 1
"""
pass
>>> a = [1,4,5,6,7,123,4,5]
>>> print(a)
[1, 4, 5, 6, 7, 123, 4, 5]
>>> a.sort()
>>> print(a)
[1, 4, 4, 5, 5, 6, 7, 123]
由此可知,sorted(b)的排序是临时的,如果不用变量接受就会被系统回收掉。而a.sort()则是永久的,此时的sort()是对象a的一个方法。它们都有个默认的参数,reverse=False,如果使用reverse=True则会实现倒序输出。
sorted 语法:
sorted(iterable[, cmp[, key[, reverse]]])
参数说明:
返回值
返回重新排序的列表。
以下实例展示了 sorted 的使用方法:
>>> a = [5,7,6,3,4,1,2]
>>> b = sorted(a) # 保留原列表
>>> a
[5, 7, 6, 3, 4, 1, 2]
>>> b
[1, 2, 3, 4, 5, 6, 7]
>>> b = sorted(a,reverse=True)
>>> b
[7, 6, 5, 4, 3, 2, 1]
>>> L=[('b',2),('a',1),('c',3),('d',4)]
>>> sorted(L, cmp=lambda x,y:cmp(x[1],y[1])) # 利用cmp函数
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
>>> sorted(L, key=lambda x:x[1]) # 利用key
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
>>> students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
>>> sorted(students, key=lambda s: s[2]) # 按年龄排序
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
>>> sorted(students, key=lambda s: s[2], reverse=True) # 按降序
[('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
以下实例展示了 sort 的使用方法:
>>> l=[('a', 1), ('b', 2), ('e', 3), ('d', 4), ('c', 6)]
>>> l
[('a', 1), ('b', 2), ('e', 3), ('d', 4), ('c', 6)]
>>> l.sort(key=lambda x : x[1], reverse=True)
>>> l
[('c', 6), ('d', 4), ('e', 3), ('b', 2), ('a', 1)]
参考:https://blog.csdn.net/u013759354/article/details/80243705
1、最简单的排序
l = [5,2,3,1,4 ]
l .sort()
print(l) ##输出:[1, 2, 3, 4, 5]
l.sort(reverse=True)#反序
print(l) ##输出:[5, 4, 3, 2, 1]
2、字符串排序
StrList = ['Fast', 'fast', 'Is', 'is', 'Smooth', 'smooth']
2.1 一般字典序排列,但是大写在前,小写在后!!
StrList.sort()
print(StrList) ##字符串列表是按照第一个字符的大小排序的
##输出:['Fast', 'Is', 'Smooth', 'fast', 'is', 'smooth']
>>> StrList = ['Fast', 'fast', 'Is', 'is', 'Smooth', 'smooth']
>>> StrList.sort()
>>> print StrList
['Fast', 'Is', 'Smooth', 'fast', 'is', 'smooth']
2.2忽略大小写,按abcd顺序
StrList.sort(key=str.lower)
print(StrList) ##输出:['Fast', 'fast', 'Is', 'is', 'Smooth', 'smooth']
>>> StrList.sort(key=str.lower)
>>> print StrList
['Fast', 'fast', 'Is', 'is', 'Smooth', 'smooth']
2.3按照字符串长度排序
StrList.sort(key=len)
print(StrList)##输出:['Is', 'is', 'Fast', 'fast', 'Smooth', 'smooth']
>>> StrList.sort(key=len)
>>> print StrList
['Is', 'is', 'Fast', 'fast', 'Smooth', 'smooth']
StrList.sort(key=len, reverse=True)#反序
print(StrList) ##输出:['Smooth', 'smooth', 'Fast', 'fast', 'Is', 'is']
>>> StrList.sort(key=len, reverse=True)
>>> print StrList
['Smooth', 'smooth', 'Fast', 'fast', 'Is', 'is']
3、sorted配合lambda进行排序
>>> l = [[2, 2, 3], [1, 10, 3],[1, 4, 5], [5, 4, 9]]
>>> sorted(l)
[[1, 4, 5], [1, 10, 3], [2, 2, 3], [5, 4, 9]]
>>> sorted(l,key=lambda line:line[2])
[[2, 2, 3], [1, 10, 3], [1, 4, 5], [5, 4, 9]]
4、sort配合lambda进行排序
若是可迭代序列的成员元素是对象,那么同样可以用sorted方法进行排序: 对象.成员变量
>>> class Student:
def __init__(self, name, grade, age):
self.name = name
self.grade = grade
self.age = age
def __repr__(self):
return repr((self.name, self.grade, self.age))
>>> student_objects = [
Student('john', 'A', 15),
Student('jane', 'B', 12),
Student('dave', 'B', 10)]
>>> sorted(student_objects, key=lambda student: student.age) # sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
5、如果第一个排序条件相同,则按照第二个条件排序
>>> a = [[2,3],[4,1],(2,8),(2,1),(3,4)]
>>> b = sorted(a,key=lambda x: (x[0], -x[1]))
>>> print(b)
[(2, 8), [2, 3], (2, 1), (3, 4), [4, 1]]
b = sorted(a,key=lambda x:(条件a,条件b))
6、字典排序:
1)、按照key排序
d = {'d1':2, 'd2':4, 'd4':1,'d3':3,} for k in sorted(d): print(k,d[k])
d1 2
d2 4
d3 3
d4 1
2)、按照values排序
对dict_items进行排序
d = {'d1':2, 'd2':4, 'd4':1,'d3':3,} res = sorted(d.items(),key=lambda d:d[1],reverse=True) print(res)
[('d2', 4), ('d3', 3), ('d1', 2), ('d4', 1)]
3)、利用value排序:__getitem__
d = {'d1':2, 'd2':4, 'd4':1,'d3':3,} for k in sorted(d,key=d.__getitem__): print(k,d[k])
d4 1
d1 2
d3 3
d2 4
反序:reverse=True
d = {'d1':2, 'd2':4, 'd4':1,'d3':3,} for k in sorted(d,key=d.__getitem__,reverse=True): print(k,d[k])
d2 4
d3 3
d1 2
d4 1
参考:https://www.cnblogs.com/kilometerwine/p/9712329.html
当排序关键字多于1个时,我们使用lambda表达式来描述关键字key
arr=[(1,4,3),(1,3,3),(2,1,4),(3,5,1)] arr.sort(key=lambda s:(s[0],s[1])) #两个关键字排序 print(arr) # 可以看到输出结果是根据列表中元组的第一项和第二项排序
[(1, 3, 3), (1, 4, 3), (2, 1, 4), (3, 5, 1)]
若我想让第一个关键字正序,第二个关键字倒序,该怎么办呢?
arr=[(1,4,3),(1,3,3),(2,1,4),(3,5,1)]
arr.sort(key=lambda s:(s[0],-s[1])) #两个关键字排序,在需要倒序排列的关键字前加`-`号
print(arr)
[(1, 4, 3), (1, 3, 3), (2, 1, 4), (3, 5, 1)]
>>> arr.sort(key=lambda s:(s[0],-s[1]))
>>> print arr
[(1, 4, 3), (1, 3, 3), (2, 1, 4), (3, 5, 1)]
>>> arr.sort(key=lambda s:(-s[0],-s[1]))
>>> print arr
[(3, 5, 1), (2, 1, 4), (1, 4, 3), (1, 3, 3)]
>>> arr.sort(key=lambda s:(-s[0],s[1]))
>>> print arr
[(3, 5, 1), (2, 1, 4), (1, 3, 3), (1, 4, 3)]
同理sorted()函数
>>> l = [[2, 2, 3], [1, 10, 3],[1, 4, 5], [5, 4, 9]]
>>> sorted(l,key=lambda line:(-line[0],line[1]))
[[5, 4, 9], [2, 2, 3], [1, 4, 5], [1, 10, 3]]
>>> sorted(l,key=lambda line:(-line[0],-line[1]))
[[5, 4, 9], [2, 2, 3], [1, 10, 3], [1, 4, 5]]
参考:https://blog.csdn.net/qq_28120673/article/details/98993207
交换字典的key与value的两种方法:
方法一: 使用dict.items()方式-字典生成式
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
dict_ori = {'A':1, 'B':2, 'C':3}
dict_new = {value:key for key,value in dict_ori.items()}
print(dict_new)
运行结果:
{1: 'A', 2: 'B', 3: 'C'}
方法二: 使用zip方法
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
dict_ori = {'A':1, 'B':2, 'C':3}
dict_new2= dict(zip(dict_ori.values(), dict_ori.keys()))
print(dict_new2)
运行结果:
{1: 'A', 2: 'B', 3: 'C'}
参考:https://www.cnblogs.com/dyl01/p/8563550.html