Python 递增地增加列表中的非零元素
如果我得到这份名单Python 递增地增加列表中的非零元素,python,list,Python,List,如果我得到这份名单 a = [1,0,0,1,0,0,0,1] 我想把它变成 a = [1,0,0,2,0,0,0,3] 为此使用列表理解: print([a[i]+a[:i].count(1) if a[i]==1 else a[i] for i in range(len(a))]) 输出: [1, 0, 0, 2, 0, 0, 0, 3] 循环版本: for i in range(len(a)): if a[i]==1: a[i]=a[i]+a[:i].cou
a = [1,0,0,1,0,0,0,1]
我想把它变成
a = [1,0,0,2,0,0,0,3]
为此使用列表理解:
print([a[i]+a[:i].count(1) if a[i]==1 else a[i] for i in range(len(a))])
输出:
[1, 0, 0, 2, 0, 0, 0, 3]
循环版本:
for i in range(len(a)):
if a[i]==1:
a[i]=a[i]+a[:i].count(1)
为此使用列表理解:
print([a[i]+a[:i].count(1) if a[i]==1 else a[i] for i in range(len(a))])
输出:
[1, 0, 0, 2, 0, 0, 0, 3]
循环版本:
for i in range(len(a)):
if a[i]==1:
a[i]=a[i]+a[:i].count(1)
所以,除了第一个,你想增加每个
1
,对吗
那么:
a = [1,0,0,1,0,0,0,1]
current_number = 0
for i, num in enumerate(a):
if num == 1:
a[i] = current_number + 1
current_number += 1
print(a)
>>> [1, 0, 0, 2, 0, 0, 0, 3]
或者,如果您愿意:
current_number = 1
for i, num in enumerate(a):
if num == 1:
a[i] = current_number
current_number += 1
所以,除了第一个,你想增加每个
1
,对吗
那么:
a = [1,0,0,1,0,0,0,1]
current_number = 0
for i, num in enumerate(a):
if num == 1:
a[i] = current_number + 1
current_number += 1
print(a)
>>> [1, 0, 0, 2, 0, 0, 0, 3]
或者,如果您愿意:
current_number = 1
for i, num in enumerate(a):
if num == 1:
a[i] = current_number
current_number += 1
我会这样做:
def increase(l):
count = 0
for num in l:
if num == 1:
yield num + count
count += 1
else:
yield num
c = list(increase(a))
c
[1, 0, 0, 2, 0, 0, 0, 3]
我会这样做:
def increase(l):
count = 0
for num in l:
if num == 1:
yield num + count
count += 1
else:
yield num
c = list(increase(a))
c
[1, 0, 0, 2, 0, 0, 0, 3]
解决方案#1和#2的设置
解决方案#1
解决方案#2,老套但有趣
>>> [x and x + next(to_add) for x in a]
[1, 0, 0, 2, 0, 0, 0, 3]
解决方案3和4的设置
解决方案#3
解决方案#4(我最喜欢的一个)
所有cumsum
解决方案都假定a
的非零元素都是1
时间:
# setup
>>> a = [1, 0, 0, 1, 0, 0, 0, 1]*1000
>>> arr = np.array(a)
>>> to_add1, to_add2 = count(), count()
# IPython timings @ i5-6200U CPU @ 2.30GHz (though only relative times are of interest)
>>> %timeit [x + next(to_add1) if x else x for x in a] # solution 1
669 µs ± 3.59 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
>>> %timeit [x and x + next(to_add2) for x in a] # solution 2
673 µs ± 15.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
>>> %timeit np.where(arr == 0, 0, arr.cumsum()) # solution 3
34.7 µs ± 94.2 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> %timeit arr = np.array(a); np.where(arr == 0, 0, arr.cumsum()) # solution 3 with array creation
474 µs ± 14.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
>>> %timeit arr*arr.cumsum() # solution 4
23.6 µs ± 131 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> %timeit arr = np.array(a); arr*arr.cumsum() # solution 4 with array creation
465 µs ± 6.82 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
解决方案#1和#2的设置
解决方案#1
解决方案#2,老套但有趣
>>> [x and x + next(to_add) for x in a]
[1, 0, 0, 2, 0, 0, 0, 3]
解决方案3和4的设置
解决方案#3
解决方案#4(我最喜欢的一个)
所有cumsum
解决方案都假定a
的非零元素都是1
时间:
# setup
>>> a = [1, 0, 0, 1, 0, 0, 0, 1]*1000
>>> arr = np.array(a)
>>> to_add1, to_add2 = count(), count()
# IPython timings @ i5-6200U CPU @ 2.30GHz (though only relative times are of interest)
>>> %timeit [x + next(to_add1) if x else x for x in a] # solution 1
669 µs ± 3.59 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
>>> %timeit [x and x + next(to_add2) for x in a] # solution 2
673 µs ± 15.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
>>> %timeit np.where(arr == 0, 0, arr.cumsum()) # solution 3
34.7 µs ± 94.2 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> %timeit arr = np.array(a); np.where(arr == 0, 0, arr.cumsum()) # solution 3 with array creation
474 µs ± 14.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
>>> %timeit arr*arr.cumsum() # solution 4
23.6 µs ± 131 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> %timeit arr = np.array(a); arr*arr.cumsum() # solution 4 with array creation
465 µs ± 6.82 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
使用numpy cumsum或累计sum将1替换为1之和
In [4]: import numpy as np
In [5]: [i if i == 0 else j for i, j in zip(a, np.cumsum(a))]
Out[5]: [1, 0, 0, 2, 0, 0, 0, 3]
使用numpy cumsum或累计sum将1替换为1之和
In [4]: import numpy as np
In [5]: [i if i == 0 else j for i, j in zip(a, np.cumsum(a))]
Out[5]: [1, 0, 0, 2, 0, 0, 0, 3]
其他选项:一行列表理解,没有依赖项
[ 0 if e == 0 else sum(a[:i+1]) for i, e in enumerate(a) ]
#=> [1, 0, 0, 2, 0, 0, 0, 3]
其他选项:一行列表理解,没有依赖项
[ 0 if e == 0 else sum(a[:i+1]) for i, e in enumerate(a) ]
#=> [1, 0, 0, 2, 0, 0, 0, 3]
你从你的研究中尝试了什么?出了什么问题?“这意味着在索引的基础上增加每一个数字1?”这不是你真正想做的我知道,我的英语有点糟糕。你从你的研究中尝试了什么?出了什么问题?“这意味着在索引的基础上增加每个数字1?”这并不是你想要做的。我知道,我的英语糟透了。请不要鼓励像这样的一行,它非常难读。@JohnSzakmeister好的,我现在添加一个循环版本。@JohnSzakmeister现在?那肯定更好,虽然对于一个大的列表,
.count()
调用效率很低。哦,伙计,请不要鼓励像这样的一行程序,它非常难阅读。@JohnSzakmeister好的,我现在就添加一个循环版本。@JohnSzakmeister现在?这肯定更好,尽管对于一个大的列表,.count()
调用效率很低。我真的很好奇为什么你会在函数中使用yield
来稍后对其调用list()
。我真的很好奇为什么你会在函数中使用yield
来稍后对其调用list()
。我喜欢这样,尽管我会做得有点不同:我认为它有点太聪明了。我喜欢这个,尽管我会做得有点不同:我认为它有点太聪明了。通常来说,将纯Python理解与cumsum混合在一起不是最佳选择。当你发现自己在这样做的时候,看看掩蔽或何处。一般来说,将纯Python理解与cumsum混合使用不是最佳选择。当你发现自己在做这件事时,看看掩蔽或在哪里。