Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_Python 3.x_Nested Lists - Fatal编程技术网

Python 如何在一个大的嵌套列表中选择所有以相同值开头的列表?

Python 如何在一个大的嵌套列表中选择所有以相同值开头的列表?,python,list,python-3.x,nested-lists,Python,List,Python 3.x,Nested Lists,我有一个大量的有序对列表,我想选择所有第一个值相同的嵌套列表。我该怎么做?例如,在下面的代码中,我想选择所有第一个值为19的列表 import numpy as np radius = 10 origin=(10,10) def circle(radius): #init vars switch = 3 - (2 * radius) points = set() x = 0 y = radius while x <= y: #first oc

我有一个大量的有序对列表,我想选择所有第一个值相同的嵌套列表。我该怎么做?例如,在下面的代码中,我想选择所有第一个值为19的列表

import numpy as np

radius = 10
origin=(10,10)

def circle(radius): #init vars
    switch = 3 - (2 * radius)
    points = set()
    x = 0
    y = radius  
    while x <= y: #first octant starts clockwise at 12 o'clock
        points.add((x,-y)) #1st oct
        points.add((y,-x)) #2nd oct
        points.add((y,x)) #3rd oct
        points.add((x,y)) #4th oct
        points.add((-x,y)) #5th oct
        points.add((-y,x)) #6th oct
        points.add((-y,-x)) #7th oct
        points.add((-x,-y)) #8th oct
        if switch < 0:
            switch=switch+(4*x)+6
        else:
            switch=switch+(4*(x-y))+10
            y=y-1
        x=x+1
    return points

cp = list(circle(radius))
cp1=np.array(cp)
center=list(origin)
cp1=cp1+center
cp2=cp1.tolist()
cp2.sort()
desmos=list(tuple(x) for x in cp2)

print(cp2)

@coldspeed和@Thomas Kuhn提供了合理的解决方案,为了清晰起见,让我们详细说明它们,使用变量名并将其转换为列表:

value_19 = [item for item in cp2 if item[0] == 19]
这是干什么用的

列表理解是生成通常包含相关列表或数据源中的筛选项或转换项的列表的机制。列表理解是高度优化的性能,一旦你习惯了,很容易阅读

本质上,上述列表理解在一行中完成了以下四件事:

value_19 = list()
for item in cp2:
    if item[0] == 19:
        value_19.append(item)
在本例中,您的cp2生成此输出

[[0, 7], [0, 8], [0, 9], [0, 10], [0, 11], [0, 12], [0, 13], [1, 5],
 [1, 6], [1, 14], [1, 15], [2, 4], [2, 16], [3, 3], [3, 17], [4, 2],
 [4, 18], [5, 1], [5, 19], [6, 1], [6, 19], [7, 0], [7, 20], [8, 0],
 [8, 20], [9, 0], [9, 20], [10, 0], [10, 20], [11, 0], [11, 20], 
 [12, 0], [12, 20], [13, 0], [13, 20], [14, 1], [14, 19], [15, 1],
 [15, 19], [16, 2], [16, 18], [17, 3], [17, 17], [18, 4], [18, 16], 
 [19, 5], [19, 6], [19, 14], [19, 15], [20, 7], [20, 8], [20, 9],
 [20, 10], [20, 11], [20, 12], [20, 13]]
以及上面的代码:

value_19 = [item for item in cp2 if item[0] == 19]
产生以下结果:

[[19, 5], [19, 6], [19, 14], [19, 15]]

我同意上面评论中的COLDSPEED

listfilterlambda x:x[0]==19,cp2是我解决这个问题的方法。这是相当有效的。让我们分析一下这行代码的作用

listfilterlambda x:x[0]==19的说明,cp2:

名单:

返回一个列表,其项目与 iterable的项目。iterable可以是序列,也可以是 支持迭代或迭代器对象

它将筛选器返回的项目作为列表返回

filterfunction,iterable:

Sp Python支持函数式编程,这意味着您可以将函数传递给其他函数

如果函数返回true,则filter函数从iterable的元素构造一个列表。iterable可以是支持迭代的序列或容器,也可以是迭代器

λx:x[0]==19,cp2:

这是这行代码的主要部分。lambda x:x[0]==19是lambda函数。可以将其视为lambda参数:表达式,其中x是lambda参数,x[0]==19是lambda表达式。filter函数在cp2中迭代,对于您的案例中的每个项目,该项目是cp2中的一对,将lambda函数应用于它。然后lambda函数对该对上的lambda表达式x[0]==19求值,如果其求值为true,则lambda函数将向过滤器返回true。然后,filter函数将把这个itempair保存在新构造的列表中

-我建议你多了解一些

如果执行该行代码,则会得到以下结果:


希望这有帮助

listfilterlambda x:x[0]==19,nestedList[x代表nestedList中的x如果x[0]==19]哇,这太棒了!谢谢,来自初学者。解释得很好。@rb12345如果答案合理合适,请随意接受=
>print(list(filter(lambda x: x[0] == 19, cp2)))
[[19, 5], [19, 6], [19, 14], [19, 15]]