作业005:列表不用方法实现
2019-11-11 本文已影响0人
yydafx
实现count的功能
nums = [2, 4, 3, 5, 6, 3, 6, 6, 8, 1, 3, 9]
n = int(input('请输入你需要统计个数的数:'))
count = 0
for i in nums:
if n == i:
count += 1
print(count)
实现extend的功能
list1 = ['李白', '凯', '狄仁杰']
list2 = ('老夫子', '小短腿')
for i in list2:
list1.append(i)
print(list1)
实现index的功能
list1 = [3, 2, 5, 8, 6, 1, 9, 4, 0, 7, '大帅']
num = input('请输入0需要获取的对应下标:')
while n < len(list1):
if num == str(list1[n]):
print(n)
break
else:
print('暂无您输入东东,列表在完善中')
break
实现reverse的功能(不适用reverse,不产生新列表,使其反转)
list1 = ['李白', '凯', '狄仁杰', '老夫子', '小短腿', '张三']
length = len(list1)
a = 0
if length & 1 == 0:
while a < (length / 2) + 1:
list1[a], list1[length - 1] = list1[length - 1], list1[a]
a += 1
length -= 1
print(list1)
else:
while a < length/2:
list1[a], list1[length-1] = list1[length-1], list1[a]
a += 1
length -= 1
print(list1)