python各种类型数据格式的索引小结

本文详细介绍了Python中列表、NumPy数组和元组的索引原理,包括默认索引、负数索引、多维数组索引以及numpy.where的使用。重点讲解了不同数据结构的索引特性及类型转换,并演示了如何在各种数据结构间灵活应用索引。
摘要由CSDN通过智能技术生成

索引通用的结论和经验

1.索引起始的位置默认情况下均为0。
2.索引为-1时,则表示最后一个位置上的索引。
3.不管什么类型的数据格式,索引均用[ ]
4.对于多维数组索引,最好采用X[ ][ ]的形式来写

列表list

A = [1,2,3,4,5]
x = A[-1]
print('索引结果为:{},数据类型为:{}'.format(x, type(x)))
# 输出结果:
# 索引结果为:5,数据类型为:<class 'int'>

数组numpy

一维数组

import numpy as np
B = np.array([1, 2, 3, 4, 5])
x = B[0]
print('索引结果为:{},数据类型为:{}'.format(x,type(x)))
# 输出结果:
# 索引结果为:1,数据类型为:<class 'numpy.int32'>
y = A[B[0]]
print('索引结果为:{},数据类型为:{}'.format(y,type(y)))
# 输出结果:
# 索引结果为:2,数据类型为:<class 'int'>

注:
1.一维数组索引方式类似于列表list的索引方式。
2.通过numpy索引得到的结果数据类型为numpy.int32,不同于int类型。但是可以再应用于列表的索引中。

多维数组

C = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(C)
print(C.shape)
# 输出结果:
# [[ 1  2  3  4  5]
# [ 6  7  8  9 10]]
# (2, 5)
x = C[0]
print('索引结果为:{},数据类型为:{}'.format(x, type(x)))
# 输出结果:
# 索引结果为:[1 2 3 4 5],数据类型为:<class 'numpy.ndarray'>
y = C[1][1]
print('索引结果为:{},数据类型为:{}'.format(y, type(y)))
# 输出结果:
# 索引结果为:7,数据类型为:<class 'numpy.int32'>
z = C[1, 1]
print('索引结果为:{},数据类型为:{}'.format(z, type(z)))
# 输出结果:
# 索引结果为:7,数据类型为:<class 'numpy.int32'>

注:
1.对于多维数组索引方式不同于一维数组
2.对于多维数组的单个索引值有两种索引方式,[1][1]和[1,1],结果和效果相同。

numpy模块中where类索引

D = np.array([1, 2, 2, 3, 3])
index_D1 = np.where(D == 1)
print(index_D1)
# 输出结果:
# (array([0], dtype=int64),)
print(type(index_D1))
# 输出结果:
# <class 'tuple'>
x = index_D1[0]
print('索引结果为:{},数据类型为:{}'.format(x, type(x)))
# 输出结果:
# 索引结果为:[0],数据类型为:<class 'numpy.ndarray'>
index_D2 = np.where(D == 2)
print(index_D2)
# 输出结果:
# (array([1, 2], dtype=int64),)
print(type(index_D2))
# 输出结果:
# <class 'tuple'>
y = index_D2[0][1]
print(y)
# 输出结果:
# 2
print(D[np.where(D == 1)])
# 输出结果:
# [1]
###############################
y1 = index_D2[0]
print('索引结果为:{},数据类型为:{}'.format(y1, type(y1)))
# 输出结果:
# 索引结果为:[1 2],数据类型为:<class 'numpy.ndarray'>
y2 = index_D2[0, 0]
print('索引结果为:{},数据类型为:{}'.format(y2, type(y2)))
# 输出结果:
# 索引结果为:TypeError: tuple indices must be integers or slices, not tuple
y3 = index_D2[0][0]
print('索引结果为:{},数据类型为:{}'.format(y3, type(y3)))
# 输出结果:
# 索引结果为:1,数据类型为:<class 'numpy.int64'>
print(A[y3])
# 输出结果:
# 2

注:
1.对于np.where返回的查找结果为元组tuple。
2.对于元组没有.shape查询。
3.对于元组切片不存在index[0,0]的方式。
4.元组中的数据类型numpy.int64可以直接应用于列表中。

元组tuple

查找某个元素的索引位置,X.count()统计出现的次数;X.index()返回第一次出现的位置索引,返回类型为int。若是想返回全部索引需要加入循环。

E = (1, 2, 3, 3, 4)
e3 = E.index(3)
print('索引结果为:{},数据类型为:{}'.format(e3, type(e3)))
# 输出结果:
# 索引结果为:2,数据类型为:<class 'int'>
元组具有不可更改性,而且若内部存在列表list则无法具体查找到list内部是否包含查询的元素。
EE = (1, 2, [1, 2, 3], 3, 4)
e3 = EE.index(3)
print('索引结果为:{},数据类型为:{}'.format(e3, type(e3)))
# 输出结果:
# 索引结果为:3,数据类型为:<class 'int'>

注意:元组是不支持修改的,但是可以对元组中列表元素进行修改

字典dict

node_incident = {'1': [2, 6, 7], '2': [1, 3, 6], '3': [2, 4, 5, 6], '4': [3, 5], '5': [3, 4, 6, 7], '6': [1, 2, 3, 5, 7], '7': [1, 5, 6]}
x = node_incident.get('1')
print(x)
# 输出结果:
# [2, 6, 7]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值