Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have 2 lists like these

x = [None,[1, 15, 175, 20],
    [150, 175, 18, 20],
    [150, 175, 18],
    [192, 150, 177],...]


y = [None,[12, 43, 55, 231],
    [243, 334, 44, 12],
    [656, 145, 138],
    [12, 150, 177],
    [150, 177, 188],...]

Now I want to erase the x values lower than 30 and y values that correspond with the erased x values. (For example, (x,y) = (1,12) in x[1] and y[1])

In order to do that, I tried to use np.where in Numpy.

I converted the x and y lists into arrays by using np.array and got this for x

array([None, list([11]), list([12, 11]), ..., list([12, 13]),list([13, 13]), list([13, 15])], dtype=object)

Then I used np.where(a<30) and got this error

TypeError: '>' not supported between instances of 'NoneType' and 'int'

I thought the None values in the first lists were the problem so I implemented

np.where(a[1:]>30)

Then I got TypeError: '>' not supported between instances of 'list' and 'int'

I'm a beginner and want to know what caused this errors.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
334 views
Welcome To Ask or Share your Answers For Others

1 Answer

Using list comprehensions:

In [161]: x = [None,[1, 15, 175, 20],
     ...:     [150, 175, 18, 20],
     ...:     [150, 175, 18],
     ...:     [192, 150, 177]]
     ...:     

Indices of items that we want to remove from x (and y?). I use x[1:] to skip the None which needs an extra test:

In [163]: [(i,j) for i,v1 in enumerate(x[1:]) for j,v2 in enumerate(v1) if v2<30]
Out[163]: [(0, 0), (0, 1), (0, 3), (1, 2), (1, 3), (2, 2)]

The values in x that are >=30:

In [164]: [[v2 for v2 in v1 if v2>=30] for v1 in x[1:]]
Out[164]: [[175], [150, 175], [150, 175], [192, 150, 177]]

We could use the Out[163] values to remove items from y. Alternatively we could iterate through x and y together (zip(x,y), etc).

If the list comprehensions get too messy they can be rewritten as loops, and possibly functions.

With an irregular nested list structure like this I don't see any point to using numpy. Object dtype arrays are basically lists (but without useful list methods).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...