在numpy数组中切片与在Python中切片列表之间有什么区别? [英] What is the difference between slicing in numpy arrays and slicing a list in Python?

查看:269
本文介绍了在numpy数组中切片与在Python中切片列表之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果curr_frames是一个numpy数组,那么最后一行是什么意思?

If curr_frames is a numpy array, what does the last line mean?

curr_frames = np.array(curr_frames)

idx = map(int,np.linspace(0,len(curr_frames)-1,80))

curr_frames = curr_frames[idx,:,:,:,]

推荐答案

从Python的内置列表到numpy数组的重要区别:

An important distinction from Python’s built-in lists to numpy arrays:

  • 在内置列表中切片时会创建一个副本.

X=[1,2,3,4,5,6]
Y=X[:3]   #[1,2,3]

通过将X从0-3切片,我们创建了一个副本并将其存储在变量Y中.

by slicing X from 0-3 we have created a copy and stored it in the variable Y.

我们可以通过更改Y来验证,即使更改Y也不影响X.

we can verify that by changing the Y and even if we change Y it does not effect X.

    Y[0]=20
    print(Y) # [20,2,3]
    print(X) # [1,2,3,4,5,6]

  • 在numpy中切片时不会创建新副本,但仍引用原始数组

    A=np.array([1,2,3,4,5,6])
    B=A[:3]
    

  • 通过在此处切片A并将其分配给B,仍然B引用原始数组A.

    By slicing A here and assigning it to B, still B referring to original array A.

    我们可以通过更改B中的元素来验证这一点,它也会更改A中的值.

    We can verify that by changing an element in B and it will change the value in A as well.

        B[0]=20
        print(B) # [20,2,3]
        print(A) # [20,2,3,4,5,6]
    

    这篇关于在numpy数组中切片与在Python中切片列表之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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