zoukankan      html  css  js  c++  java
  • python-列表练习

    #练习题
    #1,通过names.index()的方法返回第2ena的索引值
    names = ['金角大王','黑姑娘','rain','eva','狗蛋','银角大王','eva','eva','鸡头']
    print(names.index('eva'))
    """
    思路:先查询到第一个eva的索引值,在拿第一个索引值做起始位去往后查找,这样就可以实现查找多个相同的字符串的下标了
    """
    def select_list_value_index(list,select_value):
        index_num = 0
        if list.count(select_value) < 1:
            print('列表中没有这个字符串')
        if list.count(select_value) == 1:
            print('列表中只有一个与之相同的字符串,下标位置为:{0}'.format(list.index('eva')))
        else:
            print('列表中存在{0}个'.format(list.count(select_value)))
            print("第一个'{0}'的下标是:{1}".format(select_value,list.index(select_value)))
            first_index = list.index(select_value)
            print(type(first_index))
            for i in range(1,list.count(select_value)+1):
                try:
                    second_index = list.index(select_value,first_index+1)
                    first_index = list.index(select_value,second_index)
                    print('第{0}次的下标'.format(i + 1), second_index)
                except ValueError as e:
                    print('后面没有了',e.args)
    
    #select_list_value_index(list=names,select_value='eva')
    
    
    
     

    #2,把以上的列表通过切片的形式实现反转
    """
    切片都是新创建了一个列表,不是同一个内存
    reverse是在原列表基础上进行的操作
    """
    names_reverse = names[::-1]
    print(names_reverse)


    #3,打印列表中所有下标为奇数的值
    for i in range(len(names)):
    if i % 2 == 1:
    print(names[i])
  • 相关阅读:
    开通博客开心
    Kubernetes/K8s架构师实战集训营【中、高级班】-2020
    Kubernetes/K8s CKA认证全套实训视频教程下载
    centos7安装部署docker
    模拟器genymotion的安装与配置
    工欲善其事必先利其器---Android开发环境搭建
    监控利器---Zabbix(一)
    进击python第4篇:初探模块
    fuser命令小结
    进击python第三篇:基础
  • 原文地址:https://www.cnblogs.com/shanshan-test/p/12509368.html
Copyright © 2011-2022 走看看