一、统计次数list.count():
def count():
list1 = [1,5,3,6,4,[9,7,8,9],4,8,6,(9,7),4,1.21,2,3,4,6,'1']
x =input('请输入需要的元素(请勿倒回输入,有错误直接删除):')
count = 0
point = '.'
# 整数判断
if x.isdigit() == True:
print('整数')
x = int(x)
for i in list1:
if x == i:
count+=1
# 浮点判断
elif x[0]!='(' and x[0]!='[' and x[0]!='{' and x[0]!="\'" and x[0]!='\"' and x[0]!='{':
print('浮点')
if point in x :
x = float(x)
for i in list1:
if x == i:
count+=1
# 字符串判断
elif x[0] == "\'" or x[0]=='\"':
print('字符串')
for i in list1:
if x[1:-1] == i:
# print(x)
count += 1
# 序列判断
else:
print('序列',x)
for i in list1:
# print(x, str(i).replace(', ',','))
if x ==str(i).replace(', ',','):
print(x)
count+=1
print(count)
count()
二、序列添加list.extend():
def extend(list1,list2):
if type(list2)!=type(list1):
temp = list(list2)
return list1+temp
#
list1 = [1,2,3]
# list2 = (4,5,6)
list2 = {1,2,3}
list3 = extend(list1,list2)
print(list3)
三、获取下标list.index():
list1 = [1,5,3,6,4,[9,7,8,9],4,8,6,(9,7),4,1.21,2,3,4,6,'1']
def get_index(my_str):
index = -1
# 传入本身就是字符串
if type(my_str)==type('asd'):
for i in list1:
index += 1
if i== my_str:
return index
# 传入是其他类型的元素
else:
my_str_a = str(my_str)
for i in list1:
index += 1
if str(i) == my_str_a:
print(str(i),my_str_a)
return index
num = get_index('1')
print(num)
四、顺序反转list.reverse():
def reverse(listx):
index = len(listx)
temp = listx[:]
for i in range(index):
listx[i]=temp[-i-1]
del temp
return listx
list1 = [1,2,3,4,5,6]
print(reverse(list1))