Python - list (append, extend, split)

Python list 与 Java 数组

**Python list使用\[\]包裹,类似于Java 数组。**不同点在于Python list元素可以是任意类型,Java 数组元组只能是基本数据类型之一。

python 复制代码
>>> a = ['a', 2, [1,2]]
>>> type(a)
<class 'list'>

Python 声明了列表a,元素类型有:字符型、int、列表。

java 复制代码
int[] arr1;
char[] arr2

Java 声明数组时就确定了元素类型,基本数据类型【byte, short, int, long, float, double, char, boolean】的一种,不能既是int又是long。

列表扩展

append

list.append(x)

Add an item to the end of the list; equivalent to alen(a): = x

--- 官方文档描述

extend

list.extend(L)

Extend the list by appending all the items in the given list; equivalent to alen(a): = L.

将所有元素追加到已知list来扩充。

--- 官方文档描述

extend 对象是iterable

java 复制代码
>>> lst
['java', 'python', 'go', 'c++', 'c']
>>> lst.extend(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

0是int类型的对象,不是iterable的。

java 复制代码
>>> lst
['java', 'python', 'go', 'c++', 'c']
>>> lst.extend("hi")
>>> lst
['java', 'python', 'go', 'c++', 'c', 'javascript', 'h', 'i']

将字符串转为列表后,把列表作为参数传给extend,列表最终被塞入原来的列表中。

何为 iterable

迭代是重复反馈过程的活动,其目的通常是为了接近并到达所需的目标或结果。

--- 维基百科

Pyhton 判断一个对象是否可迭代

python 复制代码
>>> astr = "python"
>>> hasattr(astr, '__iter__')
True
>>> hasattr(18, '__iter__')
False

字符串类型是可迭代的,int是不可迭代的。

原地扩容

原地扩容,即内存地址不变,内容发生改变。

id() 获取内存地址

id()是Python内置函数,用来返回对象的内存地址。

python 复制代码
>>> lst = ['java', 'python']
>>> id(lst)
140624395511424
>>> new = ['go', 'c++']
>>> lst.extend(new)
>>> lst
['java', 'python', 'go', 'c++']
>>> id(lst)
140624395511424
>>> lst.append('c')
>>> lst
['java', 'python', 'go', 'c++', 'c']
>>> id(lst)
140624395511424

lst经过extend()方法,append()方法扩容后,内存地址不变,但内容增多。

原地扩容无返回值

python 复制代码
>>> lst
['java', 'python', 'go', 'c++', 'c']
>>> res = lst.append('javascript') # 没有返回值
>>> res # 打印内容为空

原地修改就没有返回值

字符串转list

Python使用split()将字符串转为list。

bash 复制代码
split(self, /, sep=None, maxsplit=-1)
  Return a list of the substrings in the string, using sep as the separator string. 
  
  sep
    The separator used to split the string.

    When set to None (the default value), will split on any whitespace
    character (including \n \r \t \f and spaces) and will discard
    empty strings from the result.
  maxsplit
    Maximum number of splits (starting from the left).
    -1 (the default value) means no limit.

Note, str.split() is mainly useful for data that has been intentionally
delimited.  With natural text that includes punctuation, consider using
the regular expression module.
bash 复制代码
>>> line = "Hello.I am Jack.Welcome you."
>>> line.split(".")
['Hello', 'I am Jack', 'Welcome you', '']
>>> line.split(".",1)
['Hello', 'I am Jack.Welcome you.']
>>> line.split(".",2)
['Hello', 'I am Jack', 'Welcome you.']
>>> line.split(".",3)
['Hello', 'I am Jack', 'Welcome you', '']
>>> line.split(".",4)
['Hello', 'I am Jack', 'Welcome you', '']

根据以上示例可知,maxsplit是使用的分割符的数量。

相关推荐
李少兄8 分钟前
Java 中只有值传递吗?
java·开发语言·python
名字还没想好☜20 分钟前
Spring @EventListener 事件驱动解耦实战:同步转异步、事务绑定与顺序控制
java·数据库·后端·python·spring
可乐鸡翅yeah_32 分钟前
hls.js 播放质量埋点实战,采集卡顿、起播、错误指标定位线上用户问题
开发语言·前端·javascript·ecmascript·音视频·m3u8·音视频在线播放
szarron1 小时前
RF Demo Kit|NanoVNA 射频演示测试板完整上手教程,滤波器、衰减器、SOLT 校准学习板
开发语言·人工智能·学习·php·射频工程·频谱仪
边境悍匪1 小时前
蜗牛学苑 Java 智能体学习 Day39|SpringAI 会话存储、Function‑Call 工具调用、MCP 思维导图复盘
java·开发语言·spring boot·学习·阿里云
ellenwan20261 小时前
看到“最新 AI 量化学习”时,先让表达变清楚
人工智能·python
万物智能信息科技1 小时前
RK3568 的多路显示移植—【万物智能之开源鸿蒙OpenHarmony系统实战开发系列教程】
linux·开发语言·华为·开源·harmonyos
蓝悦无人机1 小时前
Qt Design Studio(QDS)4.8.2项目文件结构详解
开发语言·qt·qt creator·qds·项目文件结构·qtc
yxlalm2 小时前
4.java秒杀项目第四课-后端商品后台接口
java·开发语言
海宇数据2 小时前
零信任架构实战:基于海宇柠檬查出险-登记证构建自动化残值评估网关
人工智能·python·架构·自动化