zoukankan      html  css  js  c++  java
  • TensorFlow-Python:创建空列表list与append的用法

    1.空list的创建:

    l = list()
    或者:
    l = []

    2.list中元素的创建和表达

    fruits = ['apple', 'banana', 'pear', 'grapes', 'pineapple', 'watermelon']
    fruits[2] #从0开始数起,第三个元素
    pear

    3.list中元素的更改

    fruits[2] = 'tomato'
    print(fruits)
    ['apple', 'banana', 'tomato', 'grapes', 'pineapple', 'watermelon']

    4.在list末尾增加更多元素

    fruits.append('eggplant') 
    print(fruits) 
    ['apple', 'banana', 'tomato', 'grapes', 'pineapple', 'watermelon', 'eggplant'] 
    

    5.如何截取list中的某一段

    print(fruit[: 2]) #从list的首元素开始截取,截取到位置'3',但不包括第3个元素 
    ['apple', 'banana']  

    6. 修改改list中连续的元素 

    fruits[:2] = ['a', 'b'] 
    print(fruits) 
    ['a', 'b', 'tomato', 'grapes', 'pineapple', 'watermelon', 'eggplant']
    

    7.如何删除list中某段元素,或者全部

    list fruits[:2] = [] #删除前两个元素 
    print(fruits) 
    ['tomato', 'grapes', 'pineapple', 'watermelon', 'eggplant'] 
    fruits[:] = [] #删除全部list元素
    []
  • 相关阅读:
    具有快表的地址变换机构
    npm更换淘宝镜像
    内存扩充技术
    内存管理的概念
    内存的基础知识
    102. 二叉树的层序遍历
    104. 二叉树的最大深度
    206. 反转链表
    mysql 多字段查询,全局搜素
    java 处理html转义字符
  • 原文地址:https://www.cnblogs.com/chamie/p/8762649.html
Copyright © 2011-2022 走看看