- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
给定
a = [None,1,2,3,None,4,None,None]
我愿意
a = [None,1,2,3,3,4,4,4]
目前我用暴力破解它:
def replaceNoneWithLeftmost(val):
last = None
ret = []
for x in val:
if x is not None:
ret.append(x)
last = x
else:
ret.append(last)
return ret
最后,我想去
a = [1,1,2,3,3,4,4,4]
从右向左运行。目前我有
def replaceNoneWithRightmost(val):
return replaceNoneWithLeftmost(val[::-1])[::-1]
我对就地或创建新列表并不挑剔,但现在这对我来说很奇怪。我看不到存储临时“最后”值和使用 map/lambda 的方法,并且没有想到其他任何东西。
最佳答案
IIUC,您可以使用 itertools.accumulate
生成前向填充:
>>> from itertools import accumulate
>>> a = [None,1,2,3,None,4,None,None]
>>> list(accumulate(a, lambda x,y: y if y is not None else x))
[None, 1, 2, 3, 3, 4, 4, 4]
关于python - 用最左边的非无值替换列表中的无,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31256159/
我正在开发一个需要在屏幕上查找对象的程序,到目前为止工作正常,但我遇到了多显示器配置问题。 GraphicsEnvironment.getLocalGraphicsEnvironment().getS
使用 mySql,我想列出客户对特定产品类别进行的所有购买。 所以,我有 3 个表:客户 (idCustomer, Name)、类别 (idCategory, CategoryName) 和订单 (i
我的网站上有一个关于 background-size:cover 的小问题我一直在 Firefox 中测试它,但是当我在谷歌浏览器中加载页面时,我在左边得到 1px 的白色。当我使用 backgrou
我已经搜索了几个小时来找到解决我的问题的方法,但没有成功。我遇到的问题是两个按钮的垂直堆叠。 这就是我想要做的:它说按钮在这里两次是我试图放置按钮的地方,但我所能做的就是让它们水平排列而不是垂直排列。
我有一个包含多个元素的导航栏。 Left1 Left2 Left3 Right1 Right2 Right3 我不知道如何将“fixedLef
您好,我正在尝试让 2 个 div 在左侧与右侧对齐。 #div1 #div2 #div1 #div2 #div3 #div2 #div3 #div3 诀窍是当浏览器窗口变小时,我希望#div2 位于
body { font-family: Arial, Helvetica, sans-serif; font-size: 13px;
这个问题在这里已经有了答案: In CSS Flexbox, why are there no "justify-items" and "justify-self" properties? (6
这是我的代码的 jsfiddle 链接: https://jsfiddle.net/Krisalay/zvxxeagh/ 我的 HTML 代码是: MESSAGE 1
所以我有 10 个复选框,每个标签都取自数组中相应的索引。我正在使用 ng-repeat 来展示它们: {{entity}}
我是一名优秀的程序员,十分优秀!