#如果查找列表中有N个相同元素,只返回相同元素中的第一个元素的索引
lst=['hello','world',98,'hello']print(lst.index('hello'))#如果查询的元素在列表中不存在,则会抛出ValueErrorprint(lst.index('python'))#还可以在指定的start和stop之间进行查找print(lst.index('hello',1,3))#ValueError: 'hello' is not in list "world",98print(lst.index("hello",1,4))#"world",98,"hello"
2、获取列表中的单个元素
#正向索引从0~N-1 逆向索引从-N到-1 指定索引不存在,抛出IndexError
lst2=['hello','world',98,'hello','world',234]#获取索引为2的元素print(lst[2])print(lst2[-4])#获取索引为10的元素print(lst2[10])#IndexError: list index out of range
lst4=[10,20,30,40,50,20]
lst4.remove(10)#[20, 30, 40, 50]
lst4.remove(100)#ValueError: list.remove(x): x not in list
lst4.remove(20)#[10, 30, 40, 50, 20]