我有以下列表:
lst= ['Jason', 999999999, '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97fdf6e4f8f9d7fbfee1f2b9f4f8fa" rel="noreferrer noopener nofollow">[email protected]</a>', 'Curt', 333333333, '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6407111610240e0b064a070b09" rel="noreferrer noopener nofollow">[email protected]</a>']
我想删除 Jason 和接下来的 2 个条目以及以下内容,所以我在考虑这个:
for i in range(len(lst)):
if "Jason" in lst:
del lst[0]
del lst[1]
del lst[2]
else:
print("Jason not in lst")
这是正确的吗?
感谢 Tigerhawk,我正在处理的内容如下:
原始列表:
lst = `[['Curt', 333333333, '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cfacbabdbb8fa5a0ade1aca0a2" rel="noreferrer noopener nofollow">[email protected]</a>'], ['Jason', 999999999, '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1f757e6c70715f7376697a317c7072" rel="noreferrer noopener nofollow">[email protected]</a>']]`
def clean_lst(lst):
name=str(input("Name you want to delete:")) #With this I get the lst on the 1st paragraph
lst = sum(lst, [])
if len(lst)==0:
print("Empty List")
elif name in lst:
idx = lst.index(name)
del lst[idx:idx+3]
else:
print("Name is not on the list")
最终结果应如下所示:
lst = `[['Curt', 333333333, '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ceff9fef8cce6e3eea2efe3e1" rel="noreferrer noopener nofollow">[email protected]</a>']]`
最佳答案
如果可以有多个,请从列表末尾开始,如果 l[i]
则将 i
删除到 i + 3
> 等于 Jason:
l = ['Jason', 999999999, '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="305a51435f5e705c5946551e535f5d" rel="noreferrer noopener nofollow">[email protected]</a>', 'Curt', 333333333, '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6d0e181f192d07020f430e0200" rel="noreferrer noopener nofollow">[email protected]</a>', "Jason", "foo", "bar"]
for i in range(len(l) - 1, -1, -1):
if l[i] == "Jason":
del l[i:i+3]
输出:
['Curt', 333333333, '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cba8beb9bf8ba1a4a9e5a8a4a6" rel="noreferrer noopener nofollow">[email protected]</a>']
就您自己的代码而言,它假定 "Jason"
始终是第一个元素,即使在删除任何先前的元素之后,这似乎不太可能,但只有您确定。
最有效的方法是创建一个新列表或使用生成器函数更新原始列表:
def rem_jas(l):
it = iter(l)
for ele in it:
if ele == "Jason":
# skip two elements
next(it,"")
next(it, "")
else:
yield ele
输出:
In [30]: l = ['Jason', 999999999, '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fd979c8e9293bd91948b98d39e9290" rel="noreferrer noopener nofollow">[email protected]</a>', 'Curt', 333333333, '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d8bbadaaac98b2b7baf6bbb7b5" rel="noreferrer noopener nofollow">[email protected]</a>', "Jason", "foo", "bar"]
In [31]: l[:] = rem_jas(l)
In [32]: l
Out[32]: ['Curt', 333333333, '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d9baacabad99b3b6bbf7bab6b4" rel="noreferrer noopener nofollow">[email protected]</a>']
如果您可能将 Jason
包含在另一个 Jason
的两个元素中,那么您需要决定做什么是适当的。如果总是至少有 3 个空格那就没问题了。
根据您的编辑以及您有一个列表列表而不是平面列表的事实,您似乎想要删除出现 name
的每个子列表,这使得代码丢失更简单:
lst = [['Curt', 333333333, '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cba8beb9bf8ba1a4a9e5a8a4a6" rel="noreferrer noopener nofollow">[email protected]</a>'], ['Jason', 999999999, '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="325853415d5c725e5b44571c515d5f" rel="noreferrer noopener nofollow">[email protected]</a>']]
from itertools import chain
lst[:] = chain(*(sub for sub in lst if "Jason" not in sub))
print(lst)
输出:
['Curt', 333333333, '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dab9afa8ae9ab0b5b8f4b9b5b7" rel="noreferrer noopener nofollow">[email protected]</a>']
sum
并不是压平列表的好方法,itertools.chain
效率更高。
如果您想保留子列表,则不要展平:
lst[:] = (sub for sub in lst if "Jason" not in sub)
print(lst)
或者如果您有多个 Jason 并且需要根据条件添加一些打印件,则可以使用混合版:
def rem_jas(l, name):
it = iter(l)
for ele in it:
if ele == name:
# skip two elements
next(it,"")
next(it, "")
else:
yield ele
def clean_lst(l):
name = "Jason"
for sub in l:
tmp = list(rem_jas(sub, name))
if tmp:
yield tmp
if len(tmp) == len(sub):
print("{} not in sublist".format(name))
lst[:] = clean_lst(lst)
print(lst)
演示:
In [5]: lst = [['Curt', 333333333, '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82e1f7f0f6c2e8ede0ace1edef" rel="noreferrer noopener nofollow">[email protected]</a>'], ['Jason', 999999999, '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e242f3d21200e2227382b602d2123" rel="noreferrer noopener nofollow">[email protected]</a>']]
In [6]: lst[:] = clean_lst(lst)
Jason not in sublist
In [7]: print(lst)
[['Curt', 333333333, '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6605131412260c09044805090b" rel="noreferrer noopener nofollow">[email protected]</a>']]
最后,如果您想让用户知道哪个子列表缺少名称:
def clean_lst(l):
name = "Jason"
for ind, sub in enumerate(l):
tmp = list(rem_jas(sub, name))
if tmp:
yield tmp
if len(tmp) == len(sub):
print("{} not in sublist {}".format(name, ind))
关于python - 如何删除列表中的项目及其信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34459232/