zoukankan      html  css  js  c++  java
  • 第四章 操作列表

    4.1 遍历整个列表:for循环

    注意:使用单数和复数式名称, 可帮助你判断代码段处理的是单个列表元素还是整个列表。

    循环语句的冒号不可省略,循环体部分需要统一缩进

    01 # ex1

    02 magicians = ['alice', 'david', 'carolina']

    03 for magician in magicians:

    04 print(magician)

    05 print(" ")

    06

    07 # ex2

    08 for magician in magicians:

    09 print(magician.title()+",that was a great trick!");

    10 print("I can't wait to see you next trick,"+magician.title()+". ");

    >>>

    alice

    david

    carolina

       

       

    Alice,that was a great trick!

    I can't wait to see you next trick,Alice.

       

    David,that was a great trick!

    I can't wait to see you next trick,David.

       

    Carolina,that was a great trick!

    I can't wait to see you next trick,Carolina.

       

    4.2 创建数值列表

    • 利用range(num1,num2)函数

      函数range() 让Python从你指定的第一个值开始数, 并在到达你指定的第二个值后停止, 因此输出不包含第二个值

    • 使用range()创建数字列表:

      使用函数list() 将range() 的结果直接转换为列表。 如果将range() 作为list() 的参数, 输出将为一个数字列表

    • 利用range()函数创建任意列表

    01 # ex3

    02 for value in range(1,6):

    03 print(value);

    04

    05 number=list(range(1,6));

    06 print(number);

    07

    08

    09 number1=list(range(2,11,2));

    10 print(number1);

    11

    12 # squares.py

    13 squares=[];

    14 for value in range(1,11):

    15 square=value**2

    16 squares.append(square)

    17 print(squares)

    18

    19 # squares.py/ another tips

    20 squares=[]

    21 for value in range(1,11):

    22 squares.append(value**2);

    23 print(squares);

       

    >>>

    1

    2

    3

    4

    5

    [1, 2, 3, 4, 5]

    [2, 4, 6, 8, 10]

    [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

    [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

    4.3 对数字列表进行简单的统计计算

    • min()函数、max()函数以及sum()函数
    • 列表解析(快速创建序列)

    01 # ex4.3

    02 num=list(range(0,10));

    03 print(num);#test

    04 del num[0];

    05 num.append(0);

    06 print(num);#over test

    07 a=min(num);

    08 b=max(num);

    09 c=sum(num);

    10 print("the minmum number is "+str(a)+" the maxmum number is "+str(b));

    11 print("the sumarise number is "+str(c));

    12

    13 #ex 4.3.3

    14 squares1=[value**2 for value in range(1,11)];

    15 print(squares1);

    >>>

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

    [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

    the minmum number is 0

    the maxmum number is 9

    the sumarise number is 45

    [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

    4.4 使用列表的一部分

    • 切片:通过在序列引用后面加上":"
    • 复制列表:确定是否使用切片复制列表,如果是非切片形式创建的列表,则实际上在共用一个列表存储,改变一个其中一个随之改变;反之以切片形式创建的列表,则会创建两个不同列表。

    01 # foods.py

    02 my_foods=["pizza","falafel","carrot cake"];

    03 friend_foods=my_foods[:];

    04 my_foods.append("cannoli");

    05 friend_foods.append("ice cream");

    06 print("my favorite foods are ");

    07 print(my_foods);

    08 print("my favorite foods are ");

    09 print(friend_foods);

    10

    11 # 非切片

    12 print("

    ");

    14 my_foods=["pizza","falafel","carrot cake"];

    15 friend_foods=my_foods;

    16 my_foods.append("cannoli");

    17 friend_foods.append("ice cream");

    18 print("my favorite foods are ");

    19 print(my_foods);

    20 print("my favorite foods are ");

    21 print(friend_foods);

    >>>

    my favorite foods are

    ['pizza', 'falafel', 'carrot cake', 'cannoli']

    my favorite foods are

    ['pizza', 'falafel', 'carrot cake', 'ice cream']

       

       

    my favorite foods are

    ['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']

    my favorite foods are

    ['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']

       

    4.5 元组

    目的:创建一系列不可修改的元素,但是可以对整个元组重新进行定义

    创建方式:各个元素用()包括取代列表的[]。

    01 dimentions=(200,50);

    02 print("orginal dimention");

    03 for dimention in dimentions:

    04 print(dimention);

    >>>

    orginal dimention

    200

    50

    4.6 设置代码格式:PEP8

    为更美好的明天而战!!!
  • 相关阅读:
    自动化部署之jenkins及简介
    gitlab的备份与恢复与迁移
    P2561 [AHOI2002]黑白瓷砖
    P2042 [NOI2005]维护数列
    P2156 [SDOI2009]细胞探索
    P2154 [SDOI2009]虔诚的墓主人
    P2148 [SDOI2009]E&D
    2019.2.26考试T2 矩阵快速幂加速DP
    loj #6485. LJJ 学二项式定理 (模板qwq)
    P3224 [HNOI2012]永无乡
  • 原文地址:https://www.cnblogs.com/lovely-bones/p/10976745.html
Copyright © 2011-2022 走看看