如何很好地处理[:-0]切片? [英] How to nicely handle [:-0] slicing?

查看:103
本文介绍了如何很好地处理[:-0]切片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实现自相关函数时,我有一个类似

In implementing an autocorrelation function I have a term like

for k in range(start,N):
    c[k] = np.sum(f[:-k] * f[k:])/(N-k)

现在,如果start = 1,一切都可以正常工作,但是我想很好地处理0情况下的开始,而没有条件.

Now everything works fine if start = 1 but I'd like to handle nicely the start at 0 case without a conditional.

很明显,它是不起作用的,因为f[:-0] == f[:0]并返回一个空数组,而在这种情况下,我需要完整的数组.

Obviously it doesn't work as it is because f[:-0] == f[:0] and returns an empty array, while I'd want the full array in that case.

推荐答案

在这种情况下请勿使用负索引

Don't use negative indices in this case

f[:len(f)-k]

对于k == 0,它返回整个数组.对于任何其他正数k,它等效于f[:-k]

For k == 0 it returns the whole array. For any other positive k it's equivalent to f[:-k]

这篇关于如何很好地处理[:-0]切片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆