从特定值之后的列表中删除所有元素
Remove all elements from a list after a particular value
给定一个列表 l1 = ['apple', 'pear', 'grapes, 'banana']
如何删除 'pear'
之后的所有项目
使用列表切片方法
>>> l1 = ['apple', 'pear', 'grapes', 'banana']
>>> target_ibdex = l1.index('pear')
>>> target_ibdex
1
>>> l1[:target_ibdex+1]
['apple', 'pear']
>>>
当列表中不存在元素时进行异常处理。
>>> l1 = ['apple', 'pear', 'grapes', 'banana']
>>> target_element = "mango"
>>> try:
... target_index = l1.index(target_element) + 1
... except ValueError, e:
... target_index = None
...
>>> l1[:target_index]
['apple', 'pear', 'grapes', 'banana']
当列表中存在元素时
>>> l1 = ['apple', 'pear', 'grapes', 'banana']
>>> target_element = "pear"
>>> try:
... target_index = l1.index(target_element) + 1
... except ValueError, e:
... target_index = None
...
>>> l1[:target_index]
['apple', 'pear']
l1 = ['apple', 'pear', 'grapes', 'banana']
if "pear" in l1:
l2 = l1[:l1.index("pear")+1]
print l2
输出:
['apple', 'pear']
您可以构建一个自定义生成器函数,它适用于任何可迭代对象,而不仅仅是列表 - 尽管对于您的示例,list.index
,异常处理和切片很好...
def takewhile_including(iterable, value):
for it in iterable:
yield it
if it == value:
return
l1 = ['apple', 'pear', 'grapes', 'banana']
print('Until pear', list(takewhile_including(l1, 'pear')))
# Until pear ['apple', 'pear']
print('Until blah', list(takewhile_including(l1, 'blah')))
# Until blah ['apple', 'pear', 'grapes', 'banana']
l1 = ['apple', 'pear', 'grapes', 'banana']
l1 = [x for x in l1[0:l1.index(<target>)+1]]
将其抽象为一个函数,以实现模块化和重用是最理想的。
>>> li
['apple', 'orange', 'pear', 'tomato']
>>>
>>> def slice_and_dice(fruit, fruits):
...
if fruit in fruits:
... slice = [fr for fr in l1[:fruits.index(fruit)+1]]
... return slice
...
>>> slice_and_dice('pear', li)
['apple', 'orange', 'pear']
好吧,我对每个解决方案的速度有多快很感兴趣。这是代码和估计:
setup = """
from itertools import takewhile, dropwhile
def dropwhile_v1(iterable, sentinel):
return reversed(list(dropwhile(lambda x: x != sentinel, reversed(iterable))))
def dropwhile_v2(iterable, sentinel):
return list(dropwhile(lambda x: x != sentinel, iterable[::-1]))[::-1]
def dropwhile_jon(iterable, sentinel):
for item in iterable:
yield item
if item == sentinel:
return
def dropwhile_vivek(iterable, sentinel):
try:
target_index = iterable.index(sentinel) + 1
except ValueError:
target_index = None
return iterable[:target_index]
def dropwhile_autonomou(iterable, sentinel):
if sentinel in iterable:
slice = [fr for fr in iterable[:fruits.index(sentinel)+1]]
return slice
from random import uniform
seq = [uniform(1,100) for _ in range(100)]
def test(callable):
sentinel = uniform(1,100)
callable(seq, sentinel)
"""
import timeit
for method in ['dropwhile_v1', 'dropwhile_v2', 'dropwhile_vivek', 'dropwhile_jon', 'dropwhile_autonomou']:
print ('%s: %fs' % (method, timeit.timeit('test(%s)' % method, setup=setup, number=1000000)))
输出:
dropwhile_v1: 12.979626s
dropwhile_v2: 13.234087s
dropwhile_vivek: 3.883617s
dropwhile_jon: 0.622481s
dropwhile_autonomou: 2.100633s
itertools
中有一个dropwhile
,你可以用它来过滤你选择之后的元素,然后你可以减去两者你会得到你想要的:
from itertools import dropwhile
dictionary = ['apple', 'pear', 'grapes', 'banana']
filtered = dropwhile(lambda t: 'pear' not in t, dictionary)
next(filtered)
print(list(set(dictionary)-set(list(filtered)))
它将o/p喜欢:
['apple', 'pear']
l1=(["apple","pear","banana"])
del l1[1:]
删除列表项 1 以外的任何内容(苹果为 0)
给定一个列表 l1 = ['apple', 'pear', 'grapes, 'banana']
如何删除 'pear'
使用列表切片方法
>>> l1 = ['apple', 'pear', 'grapes', 'banana']
>>> target_ibdex = l1.index('pear')
>>> target_ibdex
1
>>> l1[:target_ibdex+1]
['apple', 'pear']
>>>
当列表中不存在元素时进行异常处理。
>>> l1 = ['apple', 'pear', 'grapes', 'banana']
>>> target_element = "mango"
>>> try:
... target_index = l1.index(target_element) + 1
... except ValueError, e:
... target_index = None
...
>>> l1[:target_index]
['apple', 'pear', 'grapes', 'banana']
当列表中存在元素时
>>> l1 = ['apple', 'pear', 'grapes', 'banana']
>>> target_element = "pear"
>>> try:
... target_index = l1.index(target_element) + 1
... except ValueError, e:
... target_index = None
...
>>> l1[:target_index]
['apple', 'pear']
l1 = ['apple', 'pear', 'grapes', 'banana']
if "pear" in l1:
l2 = l1[:l1.index("pear")+1]
print l2
输出:
['apple', 'pear']
您可以构建一个自定义生成器函数,它适用于任何可迭代对象,而不仅仅是列表 - 尽管对于您的示例,list.index
,异常处理和切片很好...
def takewhile_including(iterable, value):
for it in iterable:
yield it
if it == value:
return
l1 = ['apple', 'pear', 'grapes', 'banana']
print('Until pear', list(takewhile_including(l1, 'pear')))
# Until pear ['apple', 'pear']
print('Until blah', list(takewhile_including(l1, 'blah')))
# Until blah ['apple', 'pear', 'grapes', 'banana']
l1 = ['apple', 'pear', 'grapes', 'banana']
l1 = [x for x in l1[0:l1.index(<target>)+1]]
将其抽象为一个函数,以实现模块化和重用是最理想的。
>>> li
['apple', 'orange', 'pear', 'tomato']
>>>
>>> def slice_and_dice(fruit, fruits):
...
if fruit in fruits:
... slice = [fr for fr in l1[:fruits.index(fruit)+1]]
... return slice
...
>>> slice_and_dice('pear', li)
['apple', 'orange', 'pear']
好吧,我对每个解决方案的速度有多快很感兴趣。这是代码和估计:
setup = """
from itertools import takewhile, dropwhile
def dropwhile_v1(iterable, sentinel):
return reversed(list(dropwhile(lambda x: x != sentinel, reversed(iterable))))
def dropwhile_v2(iterable, sentinel):
return list(dropwhile(lambda x: x != sentinel, iterable[::-1]))[::-1]
def dropwhile_jon(iterable, sentinel):
for item in iterable:
yield item
if item == sentinel:
return
def dropwhile_vivek(iterable, sentinel):
try:
target_index = iterable.index(sentinel) + 1
except ValueError:
target_index = None
return iterable[:target_index]
def dropwhile_autonomou(iterable, sentinel):
if sentinel in iterable:
slice = [fr for fr in iterable[:fruits.index(sentinel)+1]]
return slice
from random import uniform
seq = [uniform(1,100) for _ in range(100)]
def test(callable):
sentinel = uniform(1,100)
callable(seq, sentinel)
"""
import timeit
for method in ['dropwhile_v1', 'dropwhile_v2', 'dropwhile_vivek', 'dropwhile_jon', 'dropwhile_autonomou']:
print ('%s: %fs' % (method, timeit.timeit('test(%s)' % method, setup=setup, number=1000000)))
输出:
dropwhile_v1: 12.979626s
dropwhile_v2: 13.234087s
dropwhile_vivek: 3.883617s
dropwhile_jon: 0.622481s
dropwhile_autonomou: 2.100633s
itertools
中有一个dropwhile
,你可以用它来过滤你选择之后的元素,然后你可以减去两者你会得到你想要的:
from itertools import dropwhile
dictionary = ['apple', 'pear', 'grapes', 'banana']
filtered = dropwhile(lambda t: 'pear' not in t, dictionary)
next(filtered)
print(list(set(dictionary)-set(list(filtered)))
它将o/p喜欢:
['apple', 'pear']
l1=(["apple","pear","banana"])
del l1[1:]
删除列表项 1 以外的任何内容(苹果为 0)