zoukankan      html  css  js  c++  java
  • 004: 基本数据类型-List

    List 也就是数组,但是这个数组是动态的,用中括号与逗号创建。

    Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.

    1) Like strings (and all other built-in sequence type), lists can be indexed and sliced.

    2) All slice operations return a new list containing the requested elements.

    3) Lists also support operations like concatenation.用 + 进行连接

    4) Unlike strings, which are immutable, lists are a mutable type.

    5) You can also add new items at the end of the list, by using the append() method.

    6Assignment to slices is also possible, and this can even change the size of the list or clear it entirely.

    7) The built-in function len() also applies to lists.

    8) It is possible to nest lists (create lists containing other lists).

    9) There is a way to remove an item from a list given its index instead of its value: the del statement.

    del list_example[index]
    del list_example[start:stop]

    以下是今天做的一些小练习

    关于list的slice:

    1,一共可以接受三个参数,start, stop, step. 其中start与stop是必选参数, step是可选参数, 默认为1。

    start可以接受负数,如果为负数,则意思为从倒数第几个开始。

    stop也可以接受负数,如果为负数,则意思为倒数几个结束。

    如果step为正数,需要 stop>start 才有意义, 如果step为负数, 需要stop<start才有意义。

    a = [1,2,3,4,5]
    a[:] # [1, 2, 3, 4, 5]
    a[0:3] # [1, 2, 3]
    a[2:5] # [3, 4, 5]
    a[-3:-1] # [3, 4]
    
    a[::-1] # [5, 4, 3, 2, 1]
    a[-1:-5:-1] # [5, 4, 3, 2] 
  • 相关阅读:
    Spyder汉化问题
    Python配置环境变量
    Python、Spyder的环境搭建
    自然语言处理(六)词向量
    自然语言处理(五)深度学习
    自然语言处理(三)主题模型
    自然语言处理(四)统计机器翻译SMT
    自然语言处理(二) 语言模型
    自然语言处理(一)基础知识
    机器学习(九)隐马尔可夫模型HMM
  • 原文地址:https://www.cnblogs.com/jcsz/p/5089276.html
Copyright © 2011-2022 走看看