zoukankan      html  css  js  c++  java
  • python基础练习(四)列表_练习

      1 # 1 列表
      2 """
      3 列表简介
      4 什么是列表?
      5 我们去超市,首先会推一个购物车,像买什么东西,放进购物车,等选完后就可以去结账,然后把东西带回家~
      6 而这个购物车就可以理解为列表,里面放的各种各样的物品则代表是列表内的元素
      7 """
      8 t_list = [1,"a"]
      9 for li in t_list:
     10     print(li)
     11     print("输出元素的类型:{}".format(type(li)))
     12 
     13 # 使用 while 替代 for 循环实现上述问题
     14 t_list = [1,"a"]
     15 i = 0
     16 while i < len(t_list):
     17     print(t_list[i])
     18     i += 1
     19 
     20 # 2 列表的相关操作【增、改、查、删、排序】
     21 # 2.1 增加元素【append、extend、insert】
     22 # append【增加单个元素,添加一个列表】
     23 """
     24 注释可以缩进,但代码不能,必须顶头写;
     25 而且像类似的代码在一个py文件里,缩进的代码不只是代表自己的缩进,
     26 也代表了其在整个文件中代码的缩进
     27 如我下面的代码如果缩进了,它就会把缩进的代码当成上面的 while 的内部代码来运行 (以此注释来告警自己)
     28 """
     29 t_list = [1,"a"]
     30 print(t_list)           # [1, 'a']
     31 t_list.append("b")
     32 print(t_list)           # [1, 'a', 'b']
     33 
     34 # extend【将另一个列表中的元素逐一添加到当前列表中】
     35 t_list2 = [2,"c","d"]
     36 t_list.extend(t_list2)
     37 print(t_list)           # [1, 'a', 'b', 2, 'c', 'd']
     38 
     39 # insert【insert(index,item)在指定位置index前插入元素item】
     40 t_list = [1, 'a', 'b', [3, 'd']]
     41 t_list.insert(1,"item") #   在下标为1的位置a前面插入元素 item
     42 print(t_list)           # [1, 'item', 'a', 'b', [3, 'd']]
     43 
     44 # 2.2 修改元素【修改元素内容】
     45 # 修改元素的时候,要通过下标来确定要修改的是哪个元素,然后才能进行修改
     46 t1 = ["","",""]
     47 print(t1)           # ['我', '爱', '你']
     48 t1[1] = "不爱"
     49 print(t1)           # ['我', '不爱', '你']
     50 
     51 # 2.3 查找元素【in、not in、(index、count,上一篇已经讲过不再赘述)】
     52 # 所谓查找,就是看看指定的元素师傅存在
     53 t1 = ["","",""]
     54 str1 = ""
     55 str2 = "不爱"
     56 if str1 in t1:
     57     print("str1 存在于 t1 中")
     58 # 输出结果:str1 存在于 t1 中
     59 if str2 not in t1:
     60     print("str2 不存在于 t1 中")
     61 # 输出结果:str2 不存在于 t1 中
     62 
     63 # 2.4 删除元素【del、pop、remove】
     64 """
     65 del:根据下标删除
     66 remove:根据元素的值删除
     67 pop:删除最后一个元素
     68 """
     69 list_2 = ["a","b","c","d","e"]
     70 del list_2[2]
     71 print(list_2)
     72 
     73 list_2.remove("b")
     74 print(list_2)
     75 
     76 print(list_2.pop())
     77 print(list_2)
     78 
     79 # 2.5 排序【sort、reverse】
     80 """
     81 sort 方法是将list 按特定的顺序重新排序,默认为由小到大,参数 reverse=True 可改为倒序,由大到小
     82 reverse方法是将list逆序
     83 """
     84 num = [2,3,1,5,4,6,7]
     85 num.sort()          # 正序 [1, 2, 3, 4, 5, 6, 7]
     86 print(num)
     87 num.sort(reverse=True)
     88 print(num)          # 将原有的列表逆序,因为上面是1...7所以这个是 [7, 6, 5, 4, 3, 2, 1]
     89 num.reverse()
     90 print(num)          # 将原有的列表逆序,因为上面是7...1所以这个是 [1, 2, 3, 4, 5, 6, 7]
     91 
     92 # 列表的嵌套
     93 """
     94 一个列表中的元素,可以是单个字符、数字,也可以是一个列表,那么这时候就这是列表的嵌套
     95 """
     96 list_3 = [[1,2],["a","b"],["3","f"]]
     97 print(list_3)
     98 """
     99 在程序中,列表内不仅仅可以是列表,也可以是元组,又可以是字典,还可以是单个的字符、数字等等
    100 在今后的工作中会遇见各种各样的数据结构
    101 """
    1 # 总结
    2 """
    3 列表的定义【由一对中括号组成】
    4 列表的操作【增删改查排序】
    5 列表是可变类型
    6 """
    1 # 思考
    2 """
    3 有三个货架,每个货架上有4个位置可以放物品,现在有12 种物品需要摆放进去,
    4 问怎么实现:随机摆放品切均匀分配完所有物品
    5 """
     1 import random
     2 list_1 = [[], [], []]
     3 list_2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
     4 
     5 for i in list_2:
     6     index = random.randint(0, 2)
     7     if len(list_1[index]) < 4:
     8         list_1[index].append(i)
     9     elif len(list_1[index - 1])<4:
    10         list_1[index-1].append(i)
    11     elif len(list_1[index - 2])<4:
    12         list_1[index - 2].append(i)
    13 print(list_1)
    View Code
  • 相关阅读:
    SpringBoot实现原理
    常见Http状态码大全
    forward(转发)和redirect(重定向)有什么区别
    1094. Car Pooling (M)
    0980. Unique Paths III (H)
    1291. Sequential Digits (M)
    0121. Best Time to Buy and Sell Stock (E)
    1041. Robot Bounded In Circle (M)
    0421. Maximum XOR of Two Numbers in an Array (M)
    0216. Combination Sum III (M)
  • 原文地址:https://www.cnblogs.com/singleYao/p/13456579.html
Copyright © 2011-2022 走看看