Python 如何在指定索引之前和之后获取列表中的项
假设我有列表Python 如何在指定索引之前和之后获取列表中的项,python,python-2.7,list-comprehension,Python,Python 2.7,List Comprehension,假设我有列表f=[1,2,3]和索引I——我想迭代f,不包括I。有没有一种方法可以使用I来拆分列表,比如f[:I:,当使用I=1运行时,我会得到一个新的[1,3]列表 我正试图将其融入其中的代码: # permutations, excluding self addition # <something here> would be f excluding f[x] f = [1,2,3] r = [x + y for x in f for y in <something her
f=[1,2,3]
和索引I
——我想迭代f
,不包括I
。有没有一种方法可以使用I
来拆分列表,比如f[:I:
,当使用I=1
运行时,我会得到一个新的[1,3]
列表
我正试图将其融入其中的代码:
# permutations, excluding self addition
# <something here> would be f excluding f[x]
f = [1,2,3]
r = [x + y for x in f for y in <something here>]
# Expected Output (notice absence of any f[i]+f[i])
[3, 4, 3, 5, 4, 5]
#置换,不包括自加
#将是f,不包括f[x]
f=[1,2,3]
r=[x+y代表x英寸f代表y英寸]
#预期输出(注意没有任何f[i]+f[i])
[3, 4, 3, 5, 4, 5]
在索引上迭代y:
f = [10,20,30,40,50,60]
r = [x + f[y] for x in f for y in range(len(f)) if f[y] != x]
在索引上迭代y:
f = [10,20,30,40,50,60]
r = [x + f[y] for x in f for y in range(len(f)) if f[y] != x]
用于在迭代时访问索引
[item for i, item in enumerate(f) if i != 3]
在这种情况下,您可以转义预期的索引,或者如果您有一组索引,您可以使用中的检查成员资格:
[item for i, item in enumerate(f) if i not in {3, 4, 5}]
如果要删除某个索引中的项目,可以使用del
语句:
>>> l = ['a', 'b', 'c', 'd', 'e']
>>>
>>> del l[3]
>>> l
['a', 'b', 'c', 'e']
>>>
如果要通过删除该项并保留主列表来创建新列表,可以使用简单的切片:
>>> new = l[:3] + l[4:]
>>> new
['a', 'b', 'c', 'e']
用于在迭代时访问索引
[item for i, item in enumerate(f) if i != 3]
在这种情况下,您可以转义预期的索引,或者如果您有一组索引,您可以使用
中的检查成员资格:
[item for i, item in enumerate(f) if i not in {3, 4, 5}]
如果要删除某个索引中的项目,可以使用del
语句:
>>> l = ['a', 'b', 'c', 'd', 'e']
>>>
>>> del l[3]
>>> l
['a', 'b', 'c', 'e']
>>>
如果要通过删除该项并保留主列表来创建新列表,可以使用简单的切片:
>>> new = l[:3] + l[4:]
>>> new
['a', 'b', 'c', 'e']
可能不是最优雅的解决方案,但这可能有效:
f = [1,2,3,4,5]
for i, x in enumerate(f):
if i == 0:
new_list = f[1:]
elif i == len(f) -1:
new_list = f[:-1]
else:
new_list = f[:i]+f[i+1:]
print i, new_list
印刷品:
0 [2, 3, 4, 5]
1 [1, 3, 4, 5]
2 [1, 2, 4, 5]
3 [1, 2, 3, 5]
4 [1, 2, 3, 4]
可能不是最优雅的解决方案,但这可能有效:
f = [1,2,3,4,5]
for i, x in enumerate(f):
if i == 0:
new_list = f[1:]
elif i == len(f) -1:
new_list = f[:-1]
else:
new_list = f[:i]+f[i+1:]
print i, new_list
印刷品:
0 [2, 3, 4, 5]
1 [1, 3, 4, 5]
2 [1, 2, 4, 5]
3 [1, 2, 3, 5]
4 [1, 2, 3, 4]
好吧,这可能看起来很吓人,但这是一个能起作用的单行线:
>>> from numpy import array
>>> import itertools
>>> list(itertools.chain(*(i+array(l) for i,l in zip(reversed(f), itertools.combinations(f, len(f)-1)))))
[3, 4, 3, 5, 4, 5]
如果你慢慢看,事情就没那么复杂了:
itertools.composition
为len(f)-1
组合提供了所有可能的选项:
>>> list(itertools.combinations(f, len(f)-1))
[(1, 2), (1, 3), (2, 3)]
您可以使用zip
和reversed(f)
将其包装起来,这样就可以将每个组合与缺少的值组合在一起:
>>> [(i,l) for i,l in zip(reversed(f), itertools.combinations(f, len(f)-1))]
[(3, (1, 2)), (2, (1, 3)), (1, (2, 3))]
>>> list((i+array(l) for i,l in zip(reversed(f), itertools.combinations(f, len(f)-1))))
[array([4, 5]), array([3, 5]), array([3, 4])]
然后将l
转换为numpy.array
以便添加缺少的值:
>>> [(i,l) for i,l in zip(reversed(f), itertools.combinations(f, len(f)-1))]
[(3, (1, 2)), (2, (1, 3)), (1, (2, 3))]
>>> list((i+array(l) for i,l in zip(reversed(f), itertools.combinations(f, len(f)-1))))
[array([4, 5]), array([3, 5]), array([3, 4])]
最后,您可以使用itertools.chain
获得所需的结果
好吧,这可能看起来很吓人,但这是一个能起作用的单行线:
>>> from numpy import array
>>> import itertools
>>> list(itertools.chain(*(i+array(l) for i,l in zip(reversed(f), itertools.combinations(f, len(f)-1)))))
[3, 4, 3, 5, 4, 5]
如果你慢慢看,事情就没那么复杂了:
itertools.composition
为len(f)-1
组合提供了所有可能的选项:
>>> list(itertools.combinations(f, len(f)-1))
[(1, 2), (1, 3), (2, 3)]
您可以使用zip
和reversed(f)
将其包装起来,这样就可以将每个组合与缺少的值组合在一起:
>>> [(i,l) for i,l in zip(reversed(f), itertools.combinations(f, len(f)-1))]
[(3, (1, 2)), (2, (1, 3)), (1, (2, 3))]
>>> list((i+array(l) for i,l in zip(reversed(f), itertools.combinations(f, len(f)-1))))
[array([4, 5]), array([3, 5]), array([3, 4])]
然后将l
转换为numpy.array
以便添加缺少的值:
>>> [(i,l) for i,l in zip(reversed(f), itertools.combinations(f, len(f)-1))]
[(3, (1, 2)), (2, (1, 3)), (1, (2, 3))]
>>> list((i+array(l) for i,l in zip(reversed(f), itertools.combinations(f, len(f)-1))))
[array([4, 5]), array([3, 5]), array([3, 4])]
最后,您可以使用itertools.chain
获得所需的结果
Isf=[10,20,30,40,50,60];ind=3;f_new=f[:ind]+f[ind+1:];打印(f_新)代码>你说的自我添加是什么意思?我已经更新了我的问题clarity@MrDuk... 那么,您试图循环遍历一个列表,并在每次迭代中创建一个新的列表,其中不包括当前索引?从你的问题中不太清楚你想要达到什么。是的,这正是我想要做的。是f=[10,20,30,40,50,60];ind=3;f_new=f[:ind]+f[ind+1:];打印(f_新)代码>你说的自我添加是什么意思?我已经更新了我的问题clarity@MrDuk... 那么,您试图循环遍历一个列表,并在每次迭代中创建一个新的列表,其中不包括当前索引?你的问题不太清楚你想达到什么目的。是的,这正是我想做的。谢谢,但问题中I
的赋值只是一个例子——我想将一个列表f
拆分为一个新列表,不包括I
,每次迭代谢谢,但是问题中i
的赋值只是一个例子——我想将一个列表f
拆分成一个新列表,不包括i
,每次迭代