高效且非破坏性地获取没有第 k 个元素的列表

发布于 2024-08-19 18:17:57 字数 363 浏览 7 评论 0原文

我在 python 中有一个列表,我想迭代它,并有选择地构造一个包含除当前第 k 个元素之外的所有元素的列表。我可以做到的一种方法是:

l = [('a', 1), ('b', 2), ('c', 3)]
for num, elt in enumerate(l):
  # construct list without current element
  l_without_num = copy.deepcopy(l)
  l_without_num.remove(elt)

但这似乎效率低下且不优雅。有简单的方法吗?注意我本质上想获取原始列表的一部分,不包括当前元素。似乎应该有一种更简单的方法来做到这一点。

感谢您的帮助。

I have a list in python and I'd like to iterate through it, and selectively construct a list that contains all the elements except the current k'th element. one way I can do it is this:

l = [('a', 1), ('b', 2), ('c', 3)]
for num, elt in enumerate(l):
  # construct list without current element
  l_without_num = copy.deepcopy(l)
  l_without_num.remove(elt)

but this seems inefficient and inelegant. is there an easy way to do it? note I want to get essentially a slice of the original list that excludes the current element. seems like there should be an easier way to do this.

thank you for your help.

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

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

发布评论

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

评论(7

梦境 2024-08-26 18:17:57
l = [('a', 1), ('b', 2), ('c', 3)]
k = 1
l_without_num = l[:k] + l[(k + 1):]

这是你想要的吗?

l = [('a', 1), ('b', 2), ('c', 3)]
k = 1
l_without_num = l[:k] + l[(k + 1):]

Is this what you want?

等待圉鍢 2024-08-26 18:17:57

如果您详细解释一下您想如何使用它,将会有所帮助。但是您可以对列表理解执行相同的操作。

l = [('a', 1), ('b', 2), ('c', 3)]
k = 1
l_without_num = [elt for num, elt in enumerate(l) if not num == k]

如果您不必将其存储在 l_without_num 中,那么迭代的内存效率也会更高。

It would help if you explained more how you wanted to use it. But you can do the same with list comprehension.

l = [('a', 1), ('b', 2), ('c', 3)]
k = 1
l_without_num = [elt for num, elt in enumerate(l) if not num == k]

This is also more memory efficient to iterate over if you don't have to store it in l_without_num.

迟到的我 2024-08-26 18:17:57
l=[('a', 1), ('b', 2), ('c', 3)]
k=1
l_without_num=l[:]   # or list(l) if you prefer
l_without_num.pop(k)
l=[('a', 1), ('b', 2), ('c', 3)]
k=1
l_without_num=l[:]   # or list(l) if you prefer
l_without_num.pop(k)
万劫不复 2024-08-26 18:17:57
new = [l[i] for i in range(len(l)) if i != k]
new = [l[i] for i in range(len(l)) if i != k]
花之痕靓丽 2024-08-26 18:17:57
#!/bin/bash
`python -c "'\n'.join(mylist[:])" 2>NULL | sed '/mybadelement/d'`

哈哈

#!/bin/bash
`python -c "'\n'.join(mylist[:])" 2>NULL | sed '/mybadelement/d'`

lol

一身软味 2024-08-26 18:17:57

在集合上使用差分运算符:

list(set(l).difference([l[k]])

l=[('a', 1), ('b', 2), ('c', 3)]
list(set(l).difference([l[1]]))
[('a', 1), ('c', 3)]

Using difference operator on sets:

list(set(l).difference([l[k]])

l=[('a', 1), ('b', 2), ('c', 3)]
list(set(l).difference([l[1]]))
[('a', 1), ('c', 3)]
若有似无的小暗淡 2024-08-26 18:17:57

可能不是最有效的,但我的函数式程序员可能会写这个。

import operator
from itertools import *
def inits(list):
    for i in range(0, len(list)):
        yield list[:i]
def tails(list):
    for i in range(0, len(list)):
        yield list[i+1:]
def withouts(list):
    return imap(operator.add, inits(list), tails(list))

for elt, without in izip(l, withouts(l)):
    ...

import functools, operator
for elt in l:
    without = filter(functools.partial(operator.ne, elt), l)

我认为这不是正确的做法,但它很短。 :-)

Probably not the most efficient, but the functional programmer in me would probably write this.

import operator
from itertools import *
def inits(list):
    for i in range(0, len(list)):
        yield list[:i]
def tails(list):
    for i in range(0, len(list)):
        yield list[i+1:]
def withouts(list):
    return imap(operator.add, inits(list), tails(list))

for elt, without in izip(l, withouts(l)):
    ...

import functools, operator
for elt in l:
    without = filter(functools.partial(operator.ne, elt), l)

I don't think it's the right thing to do, but it's short. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文