Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中拆分列表_Python - Fatal编程技术网

在python中拆分列表

在python中拆分列表,python,Python,嘿,我是python新手。如何通过排序键的相对值获取列表的一部分 例如 list = [11,12,13,14,15,16,1,2,3,4,5,6,7,8,9,10] list.sort() newList = list.split("all numbers that are over 13") assert newList == [14,15,16] 列表=[11,12,13,14,15,16,1,2,3,4,5,6,7,8,9,10] list.sort() newList=list.spl

嘿,我是python新手。如何通过排序键的相对值获取列表的一部分

例如

list = [11,12,13,14,15,16,1,2,3,4,5,6,7,8,9,10] list.sort() newList = list.split("all numbers that are over 13") assert newList == [14,15,16] 列表=[11,12,13,14,15,16,1,2,3,4,5,6,7,8,9,10] list.sort() newList=list.split(“超过13的所有数字”) 断言新列表==[14,15,16] 或者使用过滤器(如果您有大列表,则会稍微慢一点,因为lambda)


使用
[如果项目>13,则对新列表中的项目使用项目]

这很有可能被生成器表达式
(如果item>13,则newList中的item对应item)
所取代,该表达式会进行惰性过滤,而不是将整个列表存储在内存中


您可能还对将代码稍微更改为以下内容感兴趣

all_numbers = [11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
filtered_sorted_numbers = sorted(number for number in all_numbers if number > 13)

它只对过滤后的值执行排序——最坏情况下的O(n log n)操作。

使用
lambda
filter
map
是愚蠢的。列表理解表单更好地满足了这一点。请注意,结果中的数字顺序与原始列表中的数字顺序相同。如果需要对它们进行排序,则应使用
sorted(如果x>13,则l中x代表x)
>>> sorted(filter(lambda x: x > 13, l))
[14, 15, 16]
all_numbers = [11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
filtered_sorted_numbers = sorted(number for number in all_numbers if number > 13)