简体   繁体   中英

How do I select lists that all start with the same value in a big nested list?

I have a massive list full of ordered pairs, and I want to select all the nested lists with the same first value. How would I do so? For example, in the code below, I would want to select all the lists that have 19 as their first value.

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 and @Thomas Kuhn provide reasonable solutions, let's elaborate on them for clarity, use your variable names and convert it to a list comprehension:

value_19 = [item for item in cp2 if item[0] == 19]

What does this do?:

List comprehensions are mechanisms to produce lists that typically contain filtered items or transformed items from a related list or data source. List comprehensions are highly optimized for performance and once you get used to them, are easy to read.

In essence, the above list comprehension does these four things in a single line:

value_19 = list()
for item in cp2:
    if item[0] == 19:
        value_19.append(item)

In this case, your cp2 produces this output.

[[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]]

And the code from above:

value_19 = [item for item in cp2 if item[0] == 19]

produces this result:

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

I agree with COLDSPEED above in the comment.

list(filter(lambda x: x[0] == 19, cp2)) is how I approach to solve this problem. It is quite efficient. Let us break down what this line of code is doing.

Explanation of list(filter(lambda x: x[0] == 19, cp2)) :

list() :

Return a list whose items are the same and in the same order as iterable's items. iterable may be either a sequence, a container that supports iteration, or an iterator object.

It is returning items returned by filter() as a list

filter(function, iterable) :

Sp Python supports functional programming which means you can pass functions to other functions.

The filter function constructs a list from the elements of iterable if the function returns true. iterable can be a sequence or a container which supports iteration, or an iterator.

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

This is the bread and butter of this line of code. lambda x: x[0] == 19 is the lambda function. You can see it as lambda arguments: expression where x is the lambda argument and x[0] == 19 is the lambda expression. The filter() function iterates through cp2 and for each of the items (in your case the item is the pair) in cp2 applies the lambda function to it. Then lambda function evaluates the lambda expression x[0] == 19 on the pair and if it evaluates to true then the lambda function returns true to the filter() . The filter function then will keep this item(pair) in the newly constructed list.

--I would suggest you learn more about lambda

Ff you execute that line of code you get the result of:

>print(list(filter(lambda x: x[0] == 19, cp2)))
[[19, 5], [19, 6], [19, 14], [19, 15]]

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM