练习 : 数据类型之元组

1、不用count函数,统计出列表中每个元素出现的次数

list1 = [1, 3, 0, 9, 6, 3, 0, 5, 1, 3]
n = str(input('请输入想知道重复次数的元素:'))
t = 0
for ele in list1:
    if n == str(ele):
        t += 1
print(n,'元素出现的次数是',t,'次')

2、不用extend函数,一次性添加一个序列的所有值到列表

list1 = [2,5,'hello',True,-3]
tuple1 = (0,4,90,'杨辉三角')
print(list1 + list(tuple1))

3、不用index函数,获取列表中指定元素的下标

list3 = [2, 5, 'hello', True, -3, 0, 4, 90, '杨辉三角']
while True:
    n = str(input('请输入想知道下标的元素(如果输入减号,退出程序):'))
    for index1 in range(len(list3)):
        if str(list3[index1]) == n:
            print(n,'的下标是:',index1)
    if n == str('-'):
         break

4、不用revers函数,在不生成新列表的前提下反向列表中的元素

list3 = [2, 5, 'hello', True, -3, 0, 4, 90, '杨辉三角']
t = 0
while t <= int(len(list3)):
    list3.insert(t, list3.pop())
    t += 1
print(list3)

猜你喜欢

转载自www.cnblogs.com/anjhon/p/11892616.html
今日推荐