Python之列表的插入&替换修改方法

yipeiwu_com5年前Python基础

用例子说明

fruit = ['pineapple','grape','pear']

fruit[0:0] = ['Orange']  #在fruit集合中第一位插入字符串'Orange'

print(fruit)     #['Orange','pineapple','grape','pear']

fruit[0] = 'Grapefruit'  #将fruit集合的第一位元素替换为'Grapefruit'

print(fruit)     #['Grapefruit','pineapple','grape','pear']

fruit[0:0] = 'Grapefruit'      

print(fruit) #['G','r','a','p','e','f','r','u','i','t','Grapefruit','pineapple','grape','pear']

以上这篇Python之列表的插入&替换修改方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python如何查看系统网络流量的信息

前言 流量信息可以直接在/proc/net/dev中进行查看,笔者实现的程序使用命令: python net.py interface 其中interface为网卡名称,使用什么网...

Python线上环境使用日志的及配置文件

目录 瞎比比 与 print 相比 logging 有什么优势? 基础用法 保存到文件 多模块使用 logging 使用配置文件配置 logging 瞎...

celery4+django2定时任务的实现代码

网上有很多celery + django实现定时任务的教程,不过它们大多数是基于djcelery + celery3的; 或者是使用django_celery_beat配置较为繁琐的。...

用Python脚本生成Android SALT扰码的方法

复制代码 代码如下:#!/usr/bin/python   # Filename: gen_salt.py   import random&nbs...

解决DataFrame排序sort的问题

如下所示: result = result.T.sort(['confidence','support'], ascending = False) 报以下错误: Attribu...