面试题之中的一个。

s=[

{"name":"Axx","score":"90"},

{"name":"Bxx","score":"91"},

{"name":"Cxx","score":"20"},

]

请用一行代码对上述list,依照score排序。

s=[
{"name":"Axx","score":"90"},
{"name":"Bxx","score":"91"},
{"name":"Cxx","score":"20"},
]
print "original s: ",s
new_s = sorted(s,key = lambda e:e.__getitem__('score'))
print "new s: ",new_s

结果:

original s:  [{'score': '90', 'name': 'Axx'}, {'score': '91', 'name': 'Bxx'}, {'score': '20', 'name': 'Cxx'}]
new s: [{'score': '20', 'name': 'Cxx'}, {'score': '90', 'name': 'Axx'}, {'score': '91', 'name': 'Bxx'}]

吐槽:print字典的排序和我s的定义顺序貌似不一样,我定义是name,score,print出来是score再name。貌似也无关紧要,dic本来就无序。

假设我想多级排序呢,先用score排序。再用name排序:

s=[
{"name":"Exx","score":"90"},
{"name":"Axx","score":"90"},
{"name":"Bxx","score":"91"},
{"name":"Cxx","score":"20"},
{"name":"Dxx","score":"90"},
]
print "original s: ",s
new_s = sorted(s,key = lambda e:e.__getitem__('score'))
print "new s: ",new_s new_s_2 = sorted(new_s,key = lambda e:(e.__getitem__('score'),e.__getitem__('name')))
print "new_s_2: ",new_s_2

结果:

original s:  [{'score': '90', 'name': 'Exx'}, {'score': '90', 'name': 'Axx'}, {'score': '91', 'name': 'Bxx'}, {'score': '20', 'name': 'Cxx'}, {'score': '90', 'name': 'Dxx'}]
new s: [{'score': '20', 'name': 'Cxx'}, {'score': '90', 'name': 'Exx'}, {'score': '90', 'name': 'Axx'}, {'score': '90', 'name': 'Dxx'}, {'score': '91', 'name': 'Bxx'}]
new_s_2: [{'score': '20', 'name': 'Cxx'}, {'score': '90', 'name': 'Axx'}, {'score': '90', 'name': 'Dxx'}, {'score': '90', 'name': 'Exx'}, {'score': '91', 'name': 'Bxx'}]

这里key = lambda e:(e.__getitem__('score'),e.__getitem__('name'))。lambda函数返回值就是个元组,元组的比較都是先比較第一个,第一个同样再比較第二个,以此类推。

元组的比較,有代码为证:

>>> aa = (1,3)
>>> bb = (1,2)
>>> cc = (2,3)
>>> aa > bb
True
>>> aa > cc
False
>>> bb < cc
True
>>>

面试题目消化完了,是时候表演下真正的技术了,额,不正确,是时候看下python sorted这个函数了。

sorted(iterable,cmp=None,key=None,reverse=False)

第一个參数那里放一个iterable对象。比方list。

第三个參数那里放一个keyword函数,让sorted()知道我们要比較元素的什么。

额,就仅仅用到这两个就够了。

尝试去读sorted源码。在C:\Python-2.7.2\Python\bltinmodule.c中:

static PyObject *
builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
{
PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
PyObject *callable;
static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
int reverse; /* args 1-4 should match listsort in Objects/listobject.c */
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
kwlist, &seq, &compare, &keyfunc, &reverse))
return NULL; newlist = PySequence_List(seq);
if (newlist == NULL)
return NULL; callable = PyObject_GetAttrString(newlist, "sort");//居然是用list.sort()
if (callable == NULL) {
Py_DECREF(newlist);
return NULL;
} newargs = PyTuple_GetSlice(args, 1, 4);
if (newargs == NULL) {
Py_DECREF(newlist);
Py_DECREF(callable);
return NULL;
} v = PyObject_Call(callable, newargs, kwds);
Py_DECREF(newargs);
Py_DECREF(callable);
if (v == NULL) {
Py_DECREF(newlist);
return NULL;
}
Py_DECREF(v);
return newlist;
}

不看了,那个sorted()代码事实上新建一个list,然后用list.sort()。所以我们看list.sort()就好了。

转战C:\Python-2.7.2\Objects\listobject.c中的list.sort(),受到一万点伤害,不看了。去死吧。手贱看什么源代码,直接会用即可了。

static PyObject *
listsort(PyListObject *self, PyObject *args, PyObject *kwds)
{
MergeState ms;
PyObject **lo, **hi;
Py_ssize_t nremaining;
Py_ssize_t minrun;
Py_ssize_t saved_ob_size, saved_allocated;
PyObject **saved_ob_item;
PyObject **final_ob_item;
PyObject *compare = NULL;
PyObject *result = NULL; /* guilty until proved innocent */
int reverse = 0;
PyObject *keyfunc = NULL;
Py_ssize_t i;
PyObject *key, *value, *kvpair;
static char *kwlist[] = {"cmp", "key", "reverse", 0}; assert(self != NULL);
assert (PyList_Check(self));
if (args != NULL) {
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOi:sort",
kwlist, &compare, &keyfunc, &reverse))
return NULL;
}
if (compare == Py_None)
compare = NULL;
if (compare != NULL &&
PyErr_WarnPy3k("the cmp argument is not supported in 3.x", 1) < 0)
return NULL;
if (keyfunc == Py_None)
keyfunc = NULL;
if (compare != NULL && keyfunc != NULL) {
compare = build_cmpwrapper(compare);
if (compare == NULL)
return NULL;
} else
Py_XINCREF(compare); /* The list is temporarily made empty, so that mutations performed
* by comparison functions can't affect the slice of memory we're
* sorting (allowing mutations during sorting is a core-dump
* factory, since ob_item may change).
*/
saved_ob_size = Py_SIZE(self);
saved_ob_item = self->ob_item;
saved_allocated = self->allocated;
Py_SIZE(self) = 0;
self->ob_item = NULL;
self->allocated = -1; /* any operation will reset it to >= 0 */ if (keyfunc != NULL) {
for (i=0 ; i < saved_ob_size ; i++) {
value = saved_ob_item[i];
key = PyObject_CallFunctionObjArgs(keyfunc, value,
NULL);
if (key == NULL) {
for (i=i-1 ; i>=0 ; i--) {
kvpair = saved_ob_item[i];
value = sortwrapper_getvalue(kvpair);
saved_ob_item[i] = value;
Py_DECREF(kvpair);
}
goto dsu_fail;
}
kvpair = build_sortwrapper(key, value);
if (kvpair == NULL)
goto dsu_fail;
saved_ob_item[i] = kvpair;
}
} /* Reverse sort stability achieved by initially reversing the list,
applying a stable forward sort, then reversing the final result. */
if (reverse && saved_ob_size > 1)
reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size); merge_init(&ms, compare); nremaining = saved_ob_size;
if (nremaining < 2)
goto succeed; /* March over the array once, left to right, finding natural runs,
* and extending short natural runs to minrun elements.
*/
lo = saved_ob_item;
hi = lo + nremaining;
minrun = merge_compute_minrun(nremaining);
do {
int descending;
Py_ssize_t n; /* Identify next run. */
n = count_run(lo, hi, compare, &descending);
if (n < 0)
goto fail;
if (descending)
reverse_slice(lo, lo + n);
/* If short, extend to min(minrun, nremaining). */
if (n < minrun) {
const Py_ssize_t force = nremaining <= minrun ?
nremaining : minrun;
if (binarysort(lo, lo + force, lo + n, compare) < 0)
goto fail;
n = force;
}
/* Push run onto pending-runs stack, and maybe merge. */
assert(ms.n < MAX_MERGE_PENDING);
ms.pending[ms.n].base = lo;
ms.pending[ms.n].len = n;
++ms.n;
if (merge_collapse(&ms) < 0)
goto fail;
/* Advance to find next run. */
lo += n;
nremaining -= n;
} while (nremaining);
assert(lo == hi); if (merge_force_collapse(&ms) < 0)
goto fail;
assert(ms.n == 1);
assert(ms.pending[0].base == saved_ob_item);
assert(ms.pending[0].len == saved_ob_size); succeed:
result = Py_None;
fail:
if (keyfunc != NULL) {
for (i=0 ; i < saved_ob_size ; i++) {
kvpair = saved_ob_item[i];
value = sortwrapper_getvalue(kvpair);
saved_ob_item[i] = value;
Py_DECREF(kvpair);
}
} if (self->allocated != -1 && result != NULL) {
/* The user mucked with the list during the sort,
* and we don't already have another error to report.
*/
PyErr_SetString(PyExc_ValueError, "list modified during sort");
result = NULL;
} if (reverse && saved_ob_size > 1)
reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size); merge_freemem(&ms); dsu_fail:
final_ob_item = self->ob_item;
i = Py_SIZE(self);
Py_SIZE(self) = saved_ob_size;
self->ob_item = saved_ob_item;
self->allocated = saved_allocated;
if (final_ob_item != NULL) {
/* we cannot use list_clear() for this because it does not
guarantee that the list is really empty when it returns */
while (--i >= 0) {
Py_XDECREF(final_ob_item[i]);
}
PyMem_FREE(final_ob_item);
}
Py_XDECREF(compare);
Py_XINCREF(result);
return result;
}

python 依照list中的dic的某key排序的更多相关文章

  1. python 实现元组中的的数据按照list排序, python查询mysql得到的数据是元组格式,按照list格式对他们排序

    需求: 需要用echart实现软件模块的统计分析,首先是对数据库的数据查询出来,然后给数据封装成列表(list)格式,数据传到前台,在echart实现绑定数据. 因为数据已经按照从大到小的顺序显示出来 ...

  2. python中字典dic详解-创建,遍历和排序

    原文地址:http://www.bugingcode.com/blog/python_dic_create_sort.html 在python的编程中,字典dic是最典型的数据结构,看看如下对字典的操 ...

  3. python学习之数据类型(dic)

    3.8 字典 3.8.1 字典的介绍 ​ 字典(dict)是python中唯一的一个映射类型,它是以{ }括起来的键值对组成,在dict中key是唯一的.在保存的时候,根据key来计算出一个内存地址, ...

  4. 使用Python将Excel中的数据导入到MySQL

    使用Python将Excel中的数据导入到MySQL 工具 Python 2.7 xlrd MySQLdb 安装 Python 对于不同的系统安装方式不同,Windows平台有exe安装包,Ubunt ...

  5. 【python】类中的self

    在python的类中,经常会写self,代表对象自己.如下例: #coding=utf-8 class Foo: def __init__(self, name): self.name = name ...

  6. paip.编程语言方法重载实现的原理及python,php,js中实现方法重载

    paip.编程语言方法重载实现的原理及python,php,js中实现方法重载 有些语言,在方法的重载上,形式上不支持函数重载,但可以通过模拟实现.. 主要原理:根据参数个数进行重载,或者使用默认值 ...

  7. 关于python多线程编程中join()和setDaemon()的一点儿探究

    关于python多线程编程中join()和setDaemon()的用法,这两天我看网上的资料看得头晕脑涨也没看懂,干脆就做一个实验来看看吧. 首先是编写实验的基础代码,创建一个名为MyThread的  ...

  8. Python工程文件中的名词解释---Module与Package的区别

    当我们在已有的Python工程文件中创建新的内容是,通常会有两种类型文件供你选择---Module和Package,对于初学者来说会搞不清楚这两种文件直接的关系.这里就来解释一下这两者之间的关系. M ...

  9. python 在mongo 中建立索引

    import pymongo mongo = pymongo.Connection('localhost') collection = mongo['database']['user'] collec ...

随机推荐

  1. python算法-汉诺塔问题

    汉诺塔问题   初始状态: 思考:当盘子的个数是3的时候,大家写出移动顺序 移动的步骤: 3个盘子,从a到c 1.前面两个盘子,从a到b 1)把前面一个盘子,从a到c a->c 2)把第二个盘子 ...

  2. Python学习-day8 socket进阶

    还是继续socket网络编程的学习. socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None) Socket Fami ...

  3. python学习-- Django传递数据给JS

    var List = {{ List|safe }};//safe 必须存在

  4. 第3章 jquery中的事件和动画

    一,jquery中的事件 (1).执行时机 $(document).ready()和window.onload方法有相似的功能,但是在执行时机方面有区别,windwo.onload在网页中所有的元素包 ...

  5. [Uiautomator篇][2] UiDeviceAPI介绍

    1 https://developer.android.com/training/testing/ui-testing/uiautomator-testing.html  http://www.cnb ...

  6. PHP 格式化显示时间 date()函数

    Y 4位数字年,y为2位数字,如99即1999年 m 数字月份,前面有前导0,如01.n 为无前导0数字月份 F 月份,完整的文本格式,例如 January 或者 March M 三个字母缩写表示的月 ...

  7. javascript学习笔记 - 引用类型 Date

    三 Date new Date() 在不传递参数的情况下,新创建的对象自动获得当前日期和时间.参数接收毫秒的timestamp Date.parse() 接收表示日期的字符串,返回相应的日期毫秒数ti ...

  8. PHP_命名空间

    namespace NS; define(__NAMESPACE__ .'\foo','111'); define('foo','222'); echo foo; // 111. echo \foo; ...

  9. Java容器jdk1.6 Array

    参考:https://www.cnblogs.com/tstd/p/5042087.html 1.定义 顶层接口collection public interface Collection<E& ...

  10. HDU——1874畅通工程续(Dijkstra与SPFA)

    畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...