程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Index and slice summary of various sequences in Python

編輯:Python

Catalog

One 、python What are the various sequences in ?

Two 、 How to access list Elements in list ?

3、 ... and 、 How to access tuple Elements in tuples ?

Four 、 How to access ndarray Elements in an array ?

5、 ... and 、 How to access dict Elements in the dictionary ?

6、 ... and 、 How to access pandas.Serise The elements in ?

7、 ... and 、 How to access pandas.Dataframe The elements in ?

8、 ... and 、 summary

Nine 、 Reference source



One 、python What are the various sequences in ?

list( list )、tuple( Tuples )、ndarray( Array )、dict( Dictionaries )、pandas.Series、pandas.Dataframe, wait .

This article emphasizes python Methods for fetching elements in all containers in , And emphasize multidimensional index , Yes 「 section 」 Less knowledge explanation .

Two 、 How to access list Elements in list ?

a=[[1,2,3],[4,5,6]]
# Take out the list a An element in
print(a[1][1]) # Index to multidimensional list , You should use multiple side-by-side brackets
print(a[1,1])
# Wrong. ,TypeError: list indices must be integers or slices, not tuple
print(' Sub contents of the list {0[1,1]}'.format([[1,2],[1,2]]))
# Wrong. ,TypeError: list indices must be integers or slices, not str

3、 ... and 、 How to access tuple Elements in tuples ?

a = ((1,2,3),(4,5,6))
a[1][1] # Just like the list , All use multiple brackets , A bracket can only put the index value of one dimension
# Output 5

Four 、 How to access ndarray Elements in an array ?

a = np.round(np.random.rand(3,3)*10)
print(a)
# a = [[3. 5. 3.]
# [5. 7. 9.]
# [9. 9. 6.]]
print(a[1,1,1]) # The general usage is to separate the indexes of each dimension with commas
print(a[1:3])
# a[1:3]=[[5. 7. 9.]
# [9. 9. 6.]]
a[1:3][0] # amount to (a[1:3])[0] , That is to say a[1:3] As a whole
# Output is [5. 7. 9.]
a[1:3][1]
# Output is [9. 9. 6.]

5、 ... and 、 How to access dict Elements in the dictionary ?

dict_2d = {'a': {'a': 1, 'b': 3}, 'b': {'a': 6}}
print(dict_2d['a']['b']) # To the dictionary , You can index them with keys value
# Output is 3

6、 ... and 、 How to access pandas.Serise The elements in ?

import numpy as np
import pandas as pd
e = pd.Series(data=[1,2,3,4,5],index=['zero','one','two','three','four'])
print(e[1]) # Use the serial number to take out Series The value in
print(e['one']) # Use the index string to fetch Series The value in
print(e[np.array([False,True,False,False,False])]) # Use elements that are Boolean ndarray Fetch target item
# Output results
# 2
# 2
# one 2
# dtype: int64

7、 ... and 、 How to access pandas.Dataframe The elements in ?

This part , I mainly refer to it  https://blog.csdn.net/wei_lin/article/details/93492252  The content of .

This table is provided by the author of the above link , Quite comprehensive !

Besides , Let me add one more thing .

If you use df It's a pandas.Dataframe object , that df[' Column index '] Yes, you can return this pandas.Dataframe Object , But the object type is not converted to ndarray, still pandas.Dataframe type .

8、 ... and 、 summary

so , except ndarray Is to separate the index data under each dimension with commas , Other similar list、tuple、dict The equal sequence is to use multiple brackets to load the index numbers of each dimension .

Probably because ,ndarray No python Native data type of .

Nine 、 Reference source



Python tuple Tuple details -C Chinese language network

pandas Detailed explanation (Series piece )_CHenXoo The blog of -CSDN Blog _pandas Medium series

Pandas DataFrame The basic properties of _wei_lin The blog of -CSDN Blog _pd.dataframe


 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved