删除仅包含空的订阅者仅在特定位置的订订者

发布于 2025-01-21 05:38:33 字数 557 浏览 0 评论 0原文

考虑列表:

list_1 = [
    [
        [1, 2, 3], [], [3, 4, 5], [], [4, 5, 6], [7, 8, 9], [9, 1, 0]
    ],
    [
        [0, 1, 2], [9, 0, 1], [], [0, 1, 3], [1, 1, 1], [9, 5, 6], [3, 6, 7]
    ],
    [
        [1, 2, 4], [4, 5, 6], [], [1, 0, 1], [0, 1, 1], [3, 9, 7], []
    ],
    [
        [1, 3, 4], [], [3, 5, 6], [], [], [], [0, 7, 0]
    ]
]

如果其中包含第1,第三或第7位的空列表,我想删除该行。

例如,必须删除第二行和第三行的上方。 我尝试了一下:

list_2 = [sublist for sublist in list_1 if all(x for x in sublist)]

如何称呼索引1、3、7?

Consider the list:

list_1 = [
    [
        [1, 2, 3], [], [3, 4, 5], [], [4, 5, 6], [7, 8, 9], [9, 1, 0]
    ],
    [
        [0, 1, 2], [9, 0, 1], [], [0, 1, 3], [1, 1, 1], [9, 5, 6], [3, 6, 7]
    ],
    [
        [1, 2, 4], [4, 5, 6], [], [1, 0, 1], [0, 1, 1], [3, 9, 7], []
    ],
    [
        [1, 3, 4], [], [3, 5, 6], [], [], [], [0, 7, 0]
    ]
]

I want to remove a row if it contains empty list either at 1st , 3rd or 7th position.

For example above the 2nd and 3rd rows must be removed.
I tried this:

list_2 = [sublist for sublist in list_1 if all(x for x in sublist)]

How I call the index 1, 3, 7 in this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

So要识趣 2025-01-28 05:38:33

使用 filter a href =“ https://docs.python.org/3/library/operator.html” rel =“ nofollow noreferrer”> operator.itemgetter.itemgetter

nb。由于您的列表有7个元素,而Python索引从0开始,我以为您是指元素0、2、6

from operator import itemgetter
list_2 = list(filter(lambda l: [] not in itemgetter(0,2,6)(l), list_1))

输出:

[[[1, 2, 3], [], [3, 4, 5], [], [4, 5, 6], [7, 8, 9], [9, 1, 0]],
 [[1, 3, 4], [], [3, 5, 6], [], [], [], [0, 7, 0]]]

Using filter and list slicing with operator.itemgetter.

NB. as your lists have 7 elements and python indexing starts at 0, I assumed you mean elements 0, 2, 6

from operator import itemgetter
list_2 = list(filter(lambda l: [] not in itemgetter(0,2,6)(l), list_1))

output:

[[[1, 2, 3], [], [3, 4, 5], [], [4, 5, 6], [7, 8, 9], [9, 1, 0]],
 [[1, 3, 4], [], [3, 5, 6], [], [], [], [0, 7, 0]]]
眼角的笑意。 2025-01-28 05:38:33

这应该起作用。

方法1

list_2的元素('row's)上的外循环循环,而内循环,对于list_2循环的某些固定元素list_2的该元素的元素,直到它在所需位置找到一个空列表,然后从内部循环中折断,以便我们移至list_2 。休息之前,list_2索引存储在delete_indices list中。循环完成后,我们使用delete_indices来确定要保留哪个行。

indices = (0, 2, 6)
delete_indices = []
for idx_x, x in enumerate(list_1):
    for idx_y, y in enumerate(x):
        if idx_y in indices and not y:
            delete_indices.append(idx_x)
            break

list_2 = [x for idx, x in enumerate(list_1) if idx not in delete_indices]

输出

[[[1, 2, 3], [], [3, 4, 5], [], [4, 5, 6], [7, 8, 9], [9, 1, 0]],
 [[1, 3, 4], [], [3, 5, 6], [], [], [], [0, 7, 0]]]

方法2 遵循与方法1相似的逻辑,并且比方法1快(见下文)。

def pred(l: list, indices = (0, 2, 6)):
    for idx, item in enumerate(l):
        if not item and idx in indices:
            return True
    return False

list_2 = [item for item in list_1 if not pred(item)]

比较

从效率方面,您应该更喜欢 @Mozway的方式。

# mozway's way (no rhymes intended)
908 ns ± 2.87 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
# Method 2 
1.4 µs ± 2.53 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
# Method 1 
1.78 µs ± 20.5 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

This should work.

Method 1

The outer loop loops over the elements ('row's) of list_2 whereas the inner loop, for some fixed element of list_2 loops over the elements of that element of list_2 until it finds an empty list in the required position and then breaks from the inner loop so that we move onto another element ('row') of list_2. Before the break, the list_2 index that is in place is stored in the delete_indices list. After the loop is done, we use delete_indices to decide which rows to keep.

indices = (0, 2, 6)
delete_indices = []
for idx_x, x in enumerate(list_1):
    for idx_y, y in enumerate(x):
        if idx_y in indices and not y:
            delete_indices.append(idx_x)
            break

list_2 = [x for idx, x in enumerate(list_1) if idx not in delete_indices]

Output

[[[1, 2, 3], [], [3, 4, 5], [], [4, 5, 6], [7, 8, 9], [9, 1, 0]],
 [[1, 3, 4], [], [3, 5, 6], [], [], [], [0, 7, 0]]]

Method 2 follows a similar logic to method 1 and is faster than method 1 (see below).

def pred(l: list, indices = (0, 2, 6)):
    for idx, item in enumerate(l):
        if not item and idx in indices:
            return True
    return False

list_2 = [item for item in list_1 if not pred(item)]

Comparison

Efficiency-wise, you should prefer @mozway's way.

# mozway's way (no rhymes intended)
908 ns ± 2.87 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
# Method 2 
1.4 µs ± 2.53 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
# Method 1 
1.78 µs ± 20.5 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文