python中的index函数要如何使用?我们一起来了解一下吧!
如果要对数据库表中一列或多列的值进行排序,使用索引可快速访问数据库表中的特定信息,在python中index() 函数是用来对列表做索引的函数。以下是index函数的具体使用方法。
index()函数语法:list.index(x[, start[, end]])。
语法中的x代表查找的对象;start表示可选、查找的起始位置;end表示 可选、查找的结束位置。
使用这一语法可以从列表中找出某个值第一个匹配项的索引位置,它的返回值就是查找对象的索引位置,如果没有找到对象则抛出异常。
具体示例:
str1 = "this is string example....wow!!!"
str2 = "exam"
print(str1.index(str2))
关于python中的index函数使用方法,我们就了解到这啦!
python复习笔记(列表的基本操作)
今天整理了一下列表的相关知识内容如下:
一、列表 list[]1:列表是[ ]所包裹的,是python中的一种数据类型,其中的数据可以是任意类型示例如下:# 如何定义一个列表数据如下:any_list = ['a', '你好', 3.1415916, True, 30, 'hello chinese boy']print(any_list, type(any_list))输出结果:['a', '你好', 3.1415916, True, 30, 'hello chinese boy'] <class 'list'>----------------------------------------------------------------------2、列表长度,用len()函数示例如下:any_list = ['a', '你好', 3.1415916, True, 30, 'hello chinese boy']length_any_list = len(any_list)print(length_any_list)输出结果:6----------------------------------------------------------------------3:列表支持切片和下标的操作示例如下:any_list = ['a', '你好', 3.1415916, True, 30, 'hello chinese boy']print(any_list[1]) # 下标从左——右是在0开始算起print(any_list[-1]) # 下标从右到左是从-1开始算起print(any_list[-6])print(any_list[2:5]) # 注意中间使用“:”,不要写成“,”运行结果:你好hello chinese boya[3.1415916, True, 30]----------------------------------------------------------------------注意:下标和字符串不同点:字符串不能使用下标修改其中的数据,列表可以使用下标修改列表中的数据;示例如下:any_list = ['a', '你好', 3.1415916, True, 30, 'hello chinese boy']any_list[0] = '经典'print(any_list)print('-'*30)str_temp = 'hello word'print(len(str_temp))str_temp[0] = 'e' # 系统会报错print(str_temp)运行结果:10TypeError: 'str' object does not support item assignment----------------------------------------------------------------------4、列表中添加数据,可以用三个函数,append(),insert(),extend()# 列表名称.append(数据),将数据添加到列表尾部,不会返回新列表,只能添加单个数据示例如下:any_list = ['a', '你好', 3.1415916, True, 30, 'hello chinese boy']any_list.append('晴天')temp_list = any_list.append('中原') # 一般不这么书写,只是本人测试用print(any_list)print(temp_list)运行结果:['a', '你好', 3.1415916, True, 30, 'hello chinese boy']['a', '你好', 3.1415916, True, 30, 'hello chinese boy', '晴天', '中原']None----------------------------------------------------------------------# 列表名称.insert(下标,数据)# 在指定的下标位置添加数据示例如下:any_list = ['a', '你好', 3.1415916, True, 30, 'hello chinese boy']any_list.insert(1, '你好么')print(any_list)运行结果:['a', '你好', 3.1415916, True, 30, 'hello chinese boy']['a', '你好么', '你好', 3.1415916, True, 30, 'hello chinese boy']----------------------------------------------------------------------# 列表.extend(可迭代对象),会将可迭代对象中的数据都逐个添加到原列表的末尾# 可迭代对象可以是 str ,列表any_list = ['a', '你好', 3.1415916, True, 30, 'hello chinese boy']print(any_list)any_list.extend('你好')print(any_list)any_list.extend([1, '兄弟', 'a'])print(any_list)运行结果:['a', '你好', 3.1415916, True, 30, 'hello chinese boy']['a', '你好', 3.1415916, True, 30, 'hello chinese boy', '你', '好']['a', '你好', 3.1415916, True, 30, 'hello chinese boy', '你', '好', 1, '兄弟', 'a']----------------------------------------------------------------------5、列表中的查询,有三个函数index()、count 、in /not in# 列表名称.index() 根据数据值来查找元素所在的下标,找到返回数据下标,没有找到数据,程序会报错示例如下:any_list = ['a', '你好', 3.1415916, True, 30, 'hello chinese boy']print(any_list)print(any_list.index(30))运行结果:['a', '你好', 3.1415916, True, 30, 'hello chinese boy']4----------------------------------------------------------------------# 列表.count(数据) 统计出现的次数示例如下:any_list = ['a', '你好', 3.1415916, True, 30, 'hello chinese boy']print(any_list)print(any_list.count('你好'))运行结果:['a', '你好', 3.1415916, True, 30, 'hello chinese boy']1----------------------------------------------------------------------# in /not in 判断是否存在,一般配合判断语句一起使用any_list = ['a', '你好', 3.1415916, True, 30, 'hello chinese boy']print(any_list)temp = '你好' in any_list # 存在结果为True,没有结果为Falseprint(temp)运行结果:['a', '你好', 3.1415916, True, 30, 'hello chinese boy']True----------------------------------------------------------------------6、列表中删除, 有del(),pop(),remove()# remove(数据值), 根据元素的数值删除# 注意:当填写删除的数据没有时,程序会报错。示例如下:any_list = ['a', '你好', 3.1415916, True, 30, 'hello chinese boy']print(any_list)any_list.remove('你好')print(any_list)运行结果:['a', '你好', 3.1415916, True, 30, 'hello chinese boy']['a', 3.1415916, True, 30, 'hello chinese boy']----------------------------------------------------------------------# 列表名称.pop(下标) 默认删除最后一个数据,返回删除的内容# 注意:当删除的下标不存在的时候程序会报错示例如下:any_list = ['a', '你好', 3.1415916, True, 30, 'hello chinese boy']print(any_list)temp = any_list.pop()print(any_list)print(temp)# 填写一个下标来删除temp1 = any_list.pop(2)print(any_list)print(temp1)运行结果:['a', '你好', 3.1415916, True, 30, 'hello chinese boy']['a', '你好', 3.1415916, True, 30]hello chinese boy['a', '你好', True, 30]3.1415916----------------------------------------------------------------------# del 列表[下标]根据下标来删除,如填写不存在下标程序会报错示例如下:any_list = ['a', '你好', 3.1415916, True, 30, 'hello chinese boy']print(any_list)del any_list[3]print(any_list)运行结果:['a', '你好', 3.1415916, True, 30, 'hello chinese boy']['a', '你好', 3.1415916, 30, 'hello chinese boy']----------------------------------------------------------------------7、列表.sort() 排序函数指令# 注意这个排序是更改原列表中的排序# 默认排序是从小到大来排序。示例如下:any_list = [1, 3, 9, 4, 2]any_list.sort()print(any_list)# 将sort(reverse=True),列表中的数据将从大到小排序any_list.sort(reverse=True)print(any_list)# sorted(列表,reverse=" ")在新的列表中进行排序,原有的列表不改变any_list1 = [1, 3, 9, 4, 2, 10]temp1 = sorted(any_list1, reverse=True)print(any_list1)print(temp1)运行结果:[1, 2, 3, 4, 9][9, 4, 3, 2, 1][1, 3, 9, 4, 2, 10][10, 9, 4, 3, 2, 1]----------------------------------------------------------------------