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]
  • 相关阅读:
    拷贝某文件至某位置
    Java对象的序列化和反序列
    常见的RuntimeException异常有哪些
    array数组增删元素
    失眠怎么办
    构造函数和函数区别(关键的new操作符)
    匿名函数递归(arguments.callee)和命名函数递归
    localeCompare方法在chrome浏览器取值问题
    random()方法
    iframe 父子页面之间取值
  • 原文地址:https://www.cnblogs.com/venicid/p/7851983.html
Copyright © 2011-2022 走看看