首页 > 其他 > 详细

练习 : 数据类型之元组

时间:2019-11-19 22:19:22      阅读:88      评论:0      收藏:0      [点我收藏+]

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)

练习 : 数据类型之元组

原文:https://www.cnblogs.com/anjhon/p/11892616.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!