基本索引

In [4]: sentence = 'You are a nice girl'In [5]: L = sentence.split()In [6]: LOut[6]: ['You', 'are', 'a', 'nice', 'girl']

# 从0开始索引In [7]: L[2]Out[7]: 'a'

# 负数索引,从列表右侧开始计数In [8]: L[-2]Out[8]: 'nice'

# -1表示列表最后一项In [9]: L[-1]Out[9]: 'girl'

# 当正整数索引超过返回时In [10]: L[100]---------------------------------------------------------------------------IndexError                                Traceback (most recent call last)
<ipython-input-10-78da2f882365> in <module>()----> 1 L[100]IndexError: list index out of range# 当负整数索引超过返回时In [11]: L[-100]---------------------------------------------------------------------------IndexError                                Traceback (most recent call last)
<ipython-input-11-46b47b0ecb55> in <module>()----> 1 L[-100]IndexError: list index out of range# slice 索引In [193]: sl = slice(0,-1,1)In [194]: L[sl]Out[194]: ['You', 'are', 'a', 'nice']In [199]: sl = slice(0,100)In [200]: L[sl]Out[200]: ['You', 'are', 'a', 'nice', 'girl']

嵌套索引

In [14]: L = [[1,2,3],{'I':'You are a nice girl','She':'Thank you!'},(11,22),'My name is Kyles']

In [15]: L
Out[15]:
[[1, 2, 3],
 {'I': 'You are a nice girl', 'She': 'Thank you!'},
 (11, 22),
 'My name is Kyles']# 索引第1项,索引为0In [16]: L[0]
Out[16]: [1, 2, 3]# 索引第1项的第2子项In [17]: L[0][1]
Out[17]: 2# 索引第2项词典In [18]: L[1]
Out[18]: {'I': 'You are a nice girl', 'She': 'Thank you!'}# 索引第2项词典的 “She”In [19]: L[1]['She']
Out[19]: 'Thank you!'# 索引第3项In [20]: L[2]
Out[20]: (11, 22)# 索引第3项,第一个元组In [22]: L[2][0]
Out[22]: 11# 索引第4项In [23]: L[3]
Out[23]: 'My name is Kyles'# 索引第4项,前3个字符In [24]: L[3][:3]
Out[24]: 'My '

切片

# 切片选择,从1到列表末尾In [13]: L[1:]Out[13]: ['are', 'a', 'nice', 'girl']# 负数索引,选取列表后两项In [28]: L[-2:]Out[28]: ['nice', 'girl']# 异常测试,这里没有报错!In [29]: L[-100:]Out[29]: ['You', 'are', 'a', 'nice', 'girl']# 返回空In [30]: L[-100:-200]Out[30]: []# 正向索引In [32]: L[-100:3]Out[32]: ['You', 'are', 'a']# 返回空In [33]: L[-1:3]Out[33]: []# 返回空In [41]: L[0:0]Out[41]: []

看似简单的索引,有的人不以为然,我们这里采用精准的数字索引,很容易排查错误。若索引是经过计算出的一个变量,就千万要小心了,否则失之毫厘差之千里。

numpy.array 索引 一维

In [34]: import numpy as npIn [35]: arr = np.arange(10)In [36]: arrOut[36]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])In [40]: arr.shapeOut[40]: (10,)# [0,1) In [37]: arr[0:1]Out[37]: array([0])# [0,0) In [38]: arr[0:0]Out[38]: array([], dtype=int32)# 右侧超出范围之后In [42]: arr[:1000]Out[42]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])# 左侧超出之后In [43]: arr[-100:1000]Out[43]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])# 两侧都超出In [44]: arr[100:101]Out[44]: array([], dtype=int32)# []In [45]: arr[-100:-2]Out[45]: array([0, 1, 2, 3, 4, 5, 6, 7])# []In [46]: arr[-100:-50]Out[46]: array([], dtype=int32)

numpy.array 索引 二维

In [49]: arr = np.arange(15).reshape(3,5)

In [50]: arr
Out[50]:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])

In [51]: arr.shape
Out[51]: (3, 5)

# axis = 0 增长的方向
In [52]: arr[0]
Out[52]: array([0, 1, 2, 3, 4])

# 选取第2行
In [53]: arr[1]
Out[53]: array([5, 6, 7, 8, 9])

# axis = 1 增长的方向,选取每一行的第1列
In [54]: arr[:,0]
Out[54]: array([ 0,  5, 10])

# axis = 1 增长的方向,选取每一行的第2列
In [55]: arr[:,1]
Out[55]: array([ 1,  6, 11])


# 选取每一行的第1,2列
In [56]: arr[:,0:2]
Out[56]:
array([[ 0,  1],
       [ 5,  6],
       [10, 11]])

# 右侧超出范围之后
In [57]: arr[:,0:100]
Out[57]:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])

# 左侧超出范围之后
In [62]: arr[:,-10:2]
Out[62]:
array([[ 0,  1],
       [ 5,  6],
       [10, 11]])

# []
In [58]: arr[:,0:0]
Out[58]: array([], shape=(3, 0), dtype=int32)

# []
In [59]: arr[0:0,0:1]
Out[59]: array([], shape=(0, 1), dtype=int32)

# 异常
In [63]: arr[:,-10]---------------------------------------------------------------------------IndexError                                Traceback (most recent call last)
<ipython-input-63-2ffa6627dc7f> in <module>()----> 1 arr[:,-10]IndexError: index -10 is out of bounds for axis 1 with size 5

numpy.array 索引 三维…N维

In [67]: import numpy as np

In [68]: arr = np.arange(30).reshape(2,3,5)

In [69]: arr
Out[69]:
array([[[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9],
        [10, 11, 12, 13, 14]],       [[15, 16, 17, 18, 19],
        [20, 21, 22, 23, 24],
        [25, 26, 27, 28, 29]]])

# 根据 axis = 0 选取
In [70]: arr[0]
Out[70]:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])

In [71]: arr[1]
Out[71]:
array([[15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24],
       [25, 26, 27, 28, 29]])

# 根据 axis = 1 选取
In [72]: arr[:,0]
Out[72]:
array([[ 0,  1,  2,  3,  4],
       [15, 16, 17, 18, 19]])

In [73]: arr[:,1]
Out[73]:
array([[ 5,  6,  7,  8,  9],
       [20, 21, 22, 23, 24]])

# 异常指出 axis = 1 超出范围
In [74]: arr[:,4]---------------------------------------------------------------------------IndexError                                Traceback (most recent call last)
<ipython-input-74-9d489478e7c7> in <module>()----> 1 arr[:,4]IndexError: index 4 is out of bounds for axis 1 with size 3  # 根据 axis = 2 选取
In [75]: arr[:,:,0]
Out[75]:
array([[ 0,  5, 10],
       [15, 20, 25]])

# 降维
In [76]: arr[:,:,0].shape
Out[76]: (2, 3)

In [78]: arr[:,:,0:2]
Out[78]:
array([[[ 0,  1],
        [ 5,  6],
        [10, 11]],       [[15, 16],
        [20, 21],
        [25, 26]]])

In [79]: arr[:,:,0:2].shape
Out[79]: (2, 3, 2)

# 左/右侧超出范围
In [81]: arr[:,:,0:100]
Out[81]:
array([[[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9],
        [10, 11, 12, 13, 14]],       [[15, 16, 17, 18, 19],
        [20, 21, 22, 23, 24],
        [25, 26, 27, 28, 29]]])

# 异常 axis = 0In [82]: arr[100,:,0:100]---------------------------------------------------------------------------IndexError                                Traceback (most recent call last)
<ipython-input-82-21efcc74439d> in <module>()----> 1 arr[100,:,0:100]IndexError: index 100 is out of bounds for axis 0 with size 2

pandas Series 索引

In [84]: s = pd.Series(['You','are','a','nice','girl'])In [85]: sOut[85]:0     You1     are2       a3    nice4    girl
dtype: object# 按照索引选择In [86]: s[0]Out[86]: 'You'# []In [87]: s[0:0]Out[87]: Series([], dtype: object)In [88]: s[0:-1]Out[88]:0     You1     are2       a3    nice
dtype: object# 易错点,ix包含区间为 []In [91]: s.ix[0:0]Out[91]:0    You
dtype: objectIn [92]: s.ix[0:1]Out[92]:0    You1    are
dtype: object# ix索引不存在indexIn [95]: s.ix[400]
KeyError: 400# 按照从0开始的索引In [95]: s.iloc[0]Out[95]: 'You'In [96]: s.iloc[1]Out[96]: 'are'In [97]: s.iloc[100]
IndexError: single positional indexer is out-of-boundsIn [98]: s = pd.Series(['You','are','a','nice','girl'], index=list('abcde'))In [99]: sOut[99]:
a     You
b     are
c       a
d    nice
e    girl
dtype: objectIn [100]: s.iloc[0]Out[100]: 'You'In [101]: s.iloc[1]Out[101]: 'are'# 按照 label 索引In [103]: s.loc['a']Out[103]: 'You'In [104]: s.loc['b']Out[104]: 'are'In [105]: s.loc[['b','a']]Out[105]:
b    are
a    You
dtype: object# loc切片索引In [106]: s.loc['a':'c']Out[106]:
a    You
b    are
c      a
dtype: objectIn [108]: s.indexOut[108]: Index(['a', 'b', 'c', 'd', 'e'], dtype='object')

pandas DataFrame 索引

In [114]: import pandas as pdIn [115]: df = pd.DataFrame({'open':[1,2,3],'high':[4,5,6],'low':[6,3,1]}, index=pd.period_range('30/12/2017',perio
     ...: ds=3,freq='H'))In [116]: dfOut[116]:
                  high  low  open2017-12-30 00:00     4    6     12017-12-30 01:00     5    3     22017-12-30 02:00     6    1     3# 按列索引In [117]: df['high']Out[117]:2017-12-30 00:00    42017-12-30 01:00    52017-12-30 02:00    6Freq: H, Name: high, dtype: int64In [118]: df.highOut[118]:2017-12-30 00:00    42017-12-30 01:00    52017-12-30 02:00    6Freq: H, Name: high, dtype: int64In [120]: df[['high','open']]Out[120]:
                  high  open2017-12-30 00:00     4     12017-12-30 01:00     5     22017-12-30 02:00     6     3In [122]: df.ix[:]
D:\CodeTool\Python\Python36\Scripts\ipython:1: DeprecationWarning:
.ix is deprecated. Please use
.loc for label based indexing or.iloc for positional indexingIn [123]: df.iloc[0:0]Out[123]:Empty DataFrame
Columns: [high, low, open]Index: []In [124]: df.ix[0:0]Out[124]:Empty DataFrame
Columns: [high, low, open]Index: []

# 按照 label 索引In [127]: df.indexOut[127]: PeriodIndex(['2017-12-30 00:00', '2017-12-30 01:00', '2017-12-30 02:00'], dtype='period[H]', freq='H')In [128]: df.loc['2017-12-30 00:00']Out[128]:
high    4low     6open    1Name: 2017-12-30 00:00, dtype: int64

# 检查参数In [155]: df.loc['2017-12-30 00:00:11']Out[155]:
high    4low     6open    1Name: 2017-12-30 00:00, dtype: int64In [156]: df.loc['2017-12-30 00:00:66']
KeyError: 'the label [2017-12-30 00:00:66] is not in the [index]'

填坑

In [158]: df = pd.DataFrame({'a':[1,2,3],'b':[4,5,6]}, index=[2,3,4])In [159]: dfOut[159]:
   a  b2  1  43  2  54  3  6# iloc 取第一行正确用法In [160]: df.iloc[0]Out[160]:
a    1b    4Name: 2, dtype: int64

# loc 正确用法In [165]: df.loc[[2,3]]Out[165]:
   a  b2  1  43  2  5# 注意此处 index 是什么类型In [167]: df.loc['2']
KeyError: 'the label [2] is not in the [index]'# 索引 Int64IndexOut[172]: Int64Index([2, 3, 4], dtype='int64')

# 索引为字符串In [168]: df = pd.DataFrame({'a':[1,2,3],'b':[4,5,6]}, index=list('234'))In [169]: dfOut[169]:
   a  b2  1  43  2  54  3  6In [170]: df.indexOut[170]: Index(['2', '3', '4'], dtype='object')

# 此处没有报错,千万注意 index 类型In [176]: df.loc['2']Out[176]:
a    1b    4Name: 2, dtype: int64

# ix 是一个功能强大的函数,但是争议却很大,往往是错误之源
# 咦,怎么输出与预想不一致!In [177]: df.ix[2]
D:\CodeTool\Python\Python36\Scripts\ipython:1: DeprecationWarning:
.ix is deprecated. Please use
.loc for label based indexing or.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecatedOut[177]:
a    3b    6Name: 4, dtype: int64

# 注意开闭区间In [180]: df.loc['2':'3']Out[180]:
   a  b2  1  43  2  5

总结

pandas中ix是错误之源,大型项目大量使用它时,往往造成不可预料的后果。0.20.x版本也标记为抛弃该函数,二义性 和 []区间,违背 “Explicit is better than implicit.” 原则。建议使用意义明确的 iloc和loc 函数。

当使用字符串时切片时是 []区间 ,一般是 [)区间

当在numpy.ndarry、list、tuple、pandas.Series、pandas.DataFrame 混合使用时,采用变量进行索引或者切割,取值或赋值时,别太自信了,千万小心错误,需要大量的测试。

我在工程中使用matlab的矩阵和python混合使用以上对象,出现最多就是shape不对应,index,columns 错误。

最好不要混用不同数据结构,容易出错,更增加转化的性能开销

到此这篇关于python基础知识之索引与切片的文章就介绍到这了,更多相关python索引与切片内容请搜索Devmax以前的文章或继续浏览下面的相关文章希望大家以后多多支持Devmax!

python基础知识之索引与切片详解的更多相关文章

  1. ios – 如何在swift3中增加String.Index?

    在swift2.3中运算符用于string.index增加例如.一世我改为swift3代码发生了“一元运算符”不能应用于’@valueString.Index’类型的操作数(又名’@lvalueString.CharacterView.Index’)“在swift3中我改写了例如.i=1但是这段代码无法解决.请帮我.解决方法String.Index是String.CharacterView.Ind

  2. ios – CoreData有序关系 – 使用NSFetchRequest批量取消

    或者,是否存在批量不支持的API,它不是私有的?解决方法目前我有一个解决方案,但不是一个干净的解决方案:我希望按照有序关系中的20个小组进行批量修改.所以,每次我索引一个索引,它的索引除以20,我为接下来的20使用新的NSFetchRequest,并通过调用公共字段名称来解除它们.

  3. XCode 3.2 Ruby和Python模板

    在xcode3.2下,我的ObjectiveCPython/Ruby项目仍然可以打开更新和编译,但是你无法创建新项目.鉴于xcode3.2中缺少ruby和python的所有痕迹(即创建项目并添加新的ruby/python文件),是否有一种简单的方法可以再次安装模板?我发现了一些关于将它们复制到某个文件夹的信息,但我似乎无法让它工作,我怀疑文件夹的位置已经改变为3.2.解决方法3.2中的应用程序模板

  4. ios – Swift中的PageViewController当前页面索引

    我想获取一个pageViewController的当前索引,我不知道我如何获取可见页索引.解决方法您可以使用didFinishAnimating,并将标签设置为查看控制器.尝试这个

  5. ios – OpenGL – 为什么GL_ELEMENT_ARRAY_BUFFER的索引?

    我目前是OpenGLES2.0领域的新手,希望尽可能地了解绑定,缓冲区,着色器等.截至目前,我只是想了解GL_ELEMENT_ARRAY_BUFFER和GL_ARRAY_BUFFER之间的差异,以及何时使用每个注释的预设.我目前的理解使我相信GL_ELEMENT_ARRAY_BUFFER是专门用于所述三角形的索引,而另一个则是其他的.有人可以详细说明为什么,如果这是正确的?GL_ELEMENT_A

  6. 如何恢复索引功能? (Xcode中)

    我的一个项目刚刚开始干扰索引过程.索引过程在中途冻结,然后突然停止,导致SourceKitService崩溃.我根本无法找到错误的代码;因为似乎没有!)–但它无法被索引.最初,我以为它是一个Xcode7.2的问题,所以升级到最新的beta(7.3);但是问题依然存在.我无法恢复到我的旧代码,因为太多的工作将被撤销,我无法发现特定的文件.崩溃报告是here.为了澄清,Xcode本身不会崩溃,只有索引过程.关于如何解决这个问题的任何想法?

  7. ios – Swift:通过索引移动数组中的元素

    给定n个元素的阵列,即vararray=[1,2,3,4,5]我可以写一个扩展到Array,所以我可以修改数组来实现这个输出:[2,5,1]:有没有办法实现这样的功能,可以通过任何索引(正或负)来移动数组.我可以用if-else子句强制执行这个功能,但是我正在寻找的是功能实现.算法很简单:>按提供的索引将数组拆分成两个>将第一个数组追加到第二个数组的末尾有没有什么办法实现它的功能风格?

  8. ios – 从imageview点击手势获取索引或标签值

    这是来自应用商店的图像,只要我们搜索任何应用程序.我也想添加相同的scrollview概念,它显示当前图像与上一个和下一个图像的小预览.我可以在Samplecode的帮助下做出这个观点.但是当用户点击任何图像时,没有找到任何解决方案来获取索引或标签值.所以我可以打开每个图像的详细页面.如果有人有这个想法,请帮助我.提前致谢.解决方法将手势识别器添加到必要的图像视图中:然后在手势处理程序中访问附加到的视图手势识别器:

  9. ios – 不能下标'[NSObject:AnyObject]类型的值?具有“String”类型的索引

    意味着一个可选的类型,这意味着你试图在本质上是一个枚举上调用一个下标.当你尝试这样做时,没有下标声明,所以系统阻塞.通过添加?我们在说,如果可能,打开这个值,然后调用下标.这样一来,系统就会推测出下面的声明类型[NSObject:AnyObject],一切都可以.你也可以使用!强制解开,但如果苹果没有,这将会崩溃.写另一种可能的方式是:这样,苹果不再是可选的,它将始终具有下标语法.不需要解开包装

  10. iOS DeepLinking中是否需要Google App Indexing SDK?

    我想在我的网页和iOS应用中使用GoogleAppIndexing.我确实支持使用ApplesSearch的UniversalLinks(或Googlelingo中的深层链接)并相应地设置我的网页.从Googlesdocumentation开始,我无法确定是否真的需要添加GoogleAppIndexingSDK.SDK没有给我任何必需的功能,我宁愿跳过它–但谷歌是否依靠SDK才能做到这一点?我没有

随机推荐

  1. 10 个Python中Pip的使用技巧分享

    众所周知,pip 可以安装、更新、卸载 Python 的第三方库,非常方便。本文小编为大家总结了Python中Pip的使用技巧,需要的可以参考一下

  2. python数学建模之三大模型与十大常用算法详情

    这篇文章主要介绍了python数学建模之三大模型与十大常用算法详情,文章围绕主题展开详细的内容介绍,具有一定的参考价值,感想取得小伙伴可以参考一下

  3. Python爬取奶茶店数据分析哪家最好喝以及性价比

    这篇文章主要介绍了用Python告诉你奶茶哪家最好喝性价比最高,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧

  4. 使用pyinstaller打包.exe文件的详细教程

    PyInstaller是一个跨平台的Python应用打包工具,能够把 Python 脚本及其所在的 Python 解释器打包成可执行文件,下面这篇文章主要给大家介绍了关于使用pyinstaller打包.exe文件的相关资料,需要的朋友可以参考下

  5. 基于Python实现射击小游戏的制作

    这篇文章主要介绍了如何利用Python制作一个自己专属的第一人称射击小游戏,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起动手试一试

  6. Python list append方法之给列表追加元素

    这篇文章主要介绍了Python list append方法如何给列表追加元素,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  7. Pytest+Request+Allure+Jenkins实现接口自动化

    这篇文章介绍了Pytest+Request+Allure+Jenkins实现接口自动化的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  8. 利用python实现简单的情感分析实例教程

    商品评论挖掘、电影推荐、股市预测……情感分析大有用武之地,下面这篇文章主要给大家介绍了关于利用python实现简单的情感分析的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下

  9. 利用Python上传日志并监控告警的方法详解

    这篇文章将详细为大家介绍如何通过阿里云日志服务搭建一套通过Python上传日志、配置日志告警的监控服务,感兴趣的小伙伴可以了解一下

  10. Pycharm中运行程序在Python console中执行,不是直接Run问题

    这篇文章主要介绍了Pycharm中运行程序在Python console中执行,不是直接Run问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

返回
顶部