zoukankan      html  css  js  c++  java
  • day 12 列表字典 补充

    1.列表list的遍历

    ##### while遍历    需要len(list)
    list = [11,22,33,44,55]
    len_list = len(list)
    
    i = 0
    while i<len_list:
        print(list[i])
        i += 1
        
    ######  for 遍历   (不用控制元素的个数,以及下标)推荐
    list = [1,2,3,4,5,6,7,8,9]
    
    for i in list:
        print(i)

    2.for循环的else  (flag)

    ####  执行else的语句 
    list = [11,22,33,44]
    #list = []
     for temp in list:
         print(temp)
     
     else:
         print("=-===")                       

         

    ####  不执行else的   break退出
     list = [11,22,33,44]
     #list = []
     for temp in list:
         print(temp)
         break
     
     else:
         print("=-===")

         

    ###  flag  版本 
    card_infors = [{"name":"alex","age":14},{"name":"jack","age":"22"}]
     
     find_name = input("请输入你要查找的姓名:")
     
     flag = 0  #默认没有找到
     for temp in card_infors:
         if find_name == temp["name"]:
             flag = 1
             print("找到了")
     if flag == 0:
         print("查无此人")
    #####   for else 版本
     card_infors = [{"name":"alex","age":14},{"name":"jack","age":"22"}]
     
     find_name = input("请输入你要查找的姓名:")
     
     for temp in card_infors:
         if find_name == temp["name"]:
             print("找到了")
             break                 #break,不执行else的语句
     else:
         print("查无此人")

    3.列表的append和extend

     list1 = [11,22,33,44]
     list2 = ["a","b"]
     
     list1.append(55)     
     print(list1)
     
     list1.extend(list2)          # extend 俩个 合并
     print(list1)  
     
     list1.append(list2)         #  append当成一个整体添加
     print(list1)

        

     4.list的append的注意点

     list1 = [11,22,33]
     list2 = [44]
     
     #list1.append(list2)
     list1 = list1.append(list2)    #list1=None
    
     print(list1)
    
    
    #执行结果
    None
    In [1]: list1 = [11,22,33]
    
    In [2]: list1.append(44)    #这条命令,执行返回结果 为None 
                                              #list1 = list1.append(44) 为None   
      
    In [3]: list1
    Out[3]: [11, 22, 33, 44]
  • 相关阅读:
    SQL学习笔记 where子句用法,like关键字 嵌套查询
    Mvc视图构建辅助方法
    EXtjs 解决ie9 不支持extjs对象的 “createContextualFragment”属性或方法
    认识MVC
    Mvc基本切入及controller认识
    Extjs4.0 下载文件的说明
    Extjs4 Panel中使用autoload无法加载页面文件
    Mvc视图表单辅助方法
    js倒计时方法
    web confing 处理全局编码方式
  • 原文地址:https://www.cnblogs.com/venicid/p/7851983.html
Copyright © 2011-2022 走看看