zoukankan      html  css  js  c++  java
  • python的列表(二)

    1、遍历整个列表

     #for 循环 #

    >>> name_list
    ['faker', 'dopa', 'gogoing', 'uzi']  
    >>> for LOL_Player in name_list:
    ... print(LOL_Player)
    faker
    dopa
    gogoing
    uzi

    第一步,定义一个列表*name_list*

    第二步,通过for语句遍历列表,结果存放到字符串LOL_Player

    第三步,打印字符串

    for 循环遍历的结果是把整个list中的所有元素读取出来,并且按元素换行。

    2,在for循环中执行更多的操作

    #在for循环中可以对每一个元素都可以执行任何操作#

    >>> name_list
    ['faker', 'dopa', 'gogoing', 'uzi']
    >>> for LOL_Player in name_list:
    ... print(LOL_Player.title() + ", They are great players")
    ...
    Faker, They are great players
    Dopa, They are great players
    Gogoing, They are great players
    Uzi, They are great players

    3,在for循环结束后执行一些操作

    在for循环后面,没有缩进的代码都只执行一次,而且不会重复执行。

    name_list=['faker','dade','gogoing','clearlove7','cool']
    for LOL_Player in name_list:
    print(LOL_Player.title() +", They are great players")
    print("Thank you, everyone.")

    [root@albenlinux Learn-python]# ./for.py
    Faker, They are great players
    Dade, They are great players
    Gogoing, They are great players
    Clearlove7, They are great players
    Cool, They are great players
    Thank you, everyone.

    4,创建数值列表

     函数range()

    >>> for value in range(1,5):
    ... print(value)
    ...
    1
    2
    3
    4

    要想创建数字列表,只需要通过list()把range()的结果直接转换为列表

    例如:

    设置一个列表,叫做numbers,其中包含元素[1,2,3,4,5]

    >>> numbers=list(range(1,6))
    >>> print(numbers)
    [1, 2, 3, 4, 5]

    使用range()的时候,还可是设置步长:

    >>> numbers_even=list(range(0,11,2))
    >>> numbers_even
    [0, 2, 4, 6, 8, 10]

    将指定整数的平方赋值到变量中

    >>> squares=[]
    >>> for value in range(1,11):
              squares.append(value**2)
    >>> squares
    [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

    #这里使用了for遍历,让每一个元素都进行**2(在python中**2代表平方),把每个元素的平方追加到列表中

    对列表执行简单的统计:

    >>> squares
    [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
    >>> min(squares)
    1
    >>> max(squares)
    100
    >>> sum(squares)
    385

    列表解析:

    通过一行代码,生成上面的squares.py文件

    >>> squares=[value**2 for value in range(1,11)]
    >>> squares
    [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

    使用列表的一部分:

    1,切片

    >>> lol_players
    ['faker', 'dopa', 'cool', 'dade', 'clearlove7', 'amazingj', 'smlz']
    >>> print(lol_players[0:3])
    ['faker', 'dopa', 'cool']

    #切片其实和range()函数相同,指定索引(0:3)其实就是切了元素0,1,2(记得python的index是从0开始的)#

    切片也可以指定步长,不如给一个列表包含数字0-10,进行切片,步长=3

    >>> number=list(range(0,11))
    >>> number
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> print(number[0:11:3])
    [0, 3, 6, 9]


    遍历切片:

    lol_players
    ['faker', 'dopa', 'cool', 'dade', 'clearlove7', 'amazingj', 'smlz']

    >>> for head_3_players in lol_players[0:3]:
    ... print(head_3_players.title())
    ...
    Faker
    Dopa
    Cool

  • 相关阅读:
    printf,wprintf与setlocale,char与wchar_t区别
    C++常量表达式、const、constexpr(C++11新增)的区别
    珍珠项链 Beads
    A Horrible Poem
    三个朋友
    Seek the Name, Seek the Fame
    Power Strings
    图书管理
    子串查找
    山峰和山谷 Ridges and Valleys
  • 原文地址:https://www.cnblogs.com/alben-cisco/p/6806236.html
Copyright © 2011-2022 走看看