Python数据类型内置函数

  - str(字符串)

  - list(列表)

  - tuple(元组)

  - dict(字典)

  - set(收集)


list(列表)的操作

  

- (append)在列表最后追加指定的元素,返回None

 # 在列表的后面追加一个元素,返回None
lst_1 = [1,2,3,4] # 实验追加是否在原内存地址或创建一个新的内存地址赋值列表
print(id(lst_1))
# 执行结果
1256965374216 lst_2 = lst_1.append(5)
print(id(lst_1))
#执行结果
1256965374216 print(lst_1)
# 执行结果
[1, 2, 3, 4, 5] print(lst_2)
# 执行结果
None

- (clear)清除指定列表中的所有内容,返回None

 # 把列表中的内容清空,返回None
lst_1 = [1,2,3,4] # 实验是否是在原内存地址清除或创建了一个新内存地址进行清除
print(id(lst_1))
# 执行结果
1256965377096 lst_2 = lst_1.clear()
print(id(lst_1))
# 执行结果
1256965377096 print(lst_1)
# 执行结果
[] print(lst_2)
# 执行结果
None

  

- (copy)复制列表中的内容,返回一个新列表

 # 复制一个新的列表,返回列表
lst_1 = [1,2,3,4]
# 实验室是否创建了一个新的内存地址进行赋值
print(id(lst_1))
# 执行结果
1256965374664 lst_2 = lst_1.copy()
print(id(lst_2))
# 执行结果
1256965377608 print(lst_1)
# 执行结果
[1, 2, 3, 4] print(lst_2)
# 执行结果
[1, 2, 3, 4]

- (count)统计列表中指定元素出现的次数,返回None

 # 统计列表中一个元素一共出现几次,返回值
lst_1 = [1,2,2,3,4,5,6]
lst_2 = lst_1.count(2) print(lst_1)
# 执行结果
[1, 2, 2, 3, 4, 5, 6] print(lst_2)
# 执行结果
2

- (extend)将列表进行合并,返回None

 # 将列表进行合并,返回None
lst_1 = [1,2,3,4,5]
lst_2 = [6,7,8,9,10]
# 合并lst_1和lst_2,在lst_1后追加lst_2
lst_3 = lst_1.extend(lst_2) print(lst_1)
# 执行结果
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(lst_2)
# 执行结果
[6, 7, 8, 9, 10] print(lst_3)
# 执行结果
None

- (index)指定一个值在列表中找出它的索引,返回索引的值

 # 指定列表的值找到它的索引,返回索引
lst_1 = [1,2,3,4,5,3,6]
lst_2 = lst_1.index(3)
lst_3 = lst_1.index(3,3,7) print(lst_1)
# 执行结果
[1, 2, 3, 4, 5, 3, 6] print(lst_2)
# 执行结果
2 print(lst_3)
# 执行结果
5

- (pop)指定列表中的索引的值进行删除,默认删除最后一个,返回被删除的索引的值

 # 删除列表中指定索引的值,默认是最后一个,返回删除 索引的值
lst_1 = [1,2,3,4,5,6]
lst_2 = lst_1.pop(0)
print(lst_1)
# 执行结果
[2, 3, 4, 5, 6] print(lst_2)
# 执行结果
1 lst_3 = lst_1.pop()
print(lst_1)
# 执行结果
[2, 3, 4, 5] print(lst_3)
# 执行结果
6

- (insert)指定列表中的索引进行值的插入,返回None

 # 指定列表中的索引进行值的插入,返回None
lst_1 = [1,3,4,5,6]
lst_2 = lst_1.insert(1,2) print(lst_1)
# 执行结果
[1, 2, 3, 4, 5, 6] print(lst_2)
# 执行结果
None

- (remove)删除列表中指定的值,返回None

 # 删除指定的值,返回None
lst_1 = [1,2,3,4,5]
lst_2 = lst_1.remove(3) print(lst_1)
# 执行结果
[1, 2, 4, 5] print(lst_2)
# 执行结果
None

- (reverse)将列表进行翻转,返回None

 # 将列表进行翻转,返回None
lst_1 = [1,2,3,4,5,6,7,8,9]
# 实验是否在原地址进行翻转还是创建了一块新的内存地址
print(id(lst_1))
# 执行结果
1256964466952 lst_2 = lst_1.reverse()
print(id(lst_1))
# 执行结果
1256964466952 print(lst_1)
# 执行结果
[9, 8, 7, 6, 5, 4, 3, 2, 1] print(lst_2)
# 执行结果
None

- (sort)将类别进行有顺序的排序,返回None

 # 将列表进行排序,返回None
lst_1 = [8,6,3,5,4,2,1,7]
lst_2 = lst_1.sort()
print(lst_1)
# 执行结果
[1, 2, 3, 4, 5, 6, 7, 8] print(lst_2)
# 执行结果
None lst_3 = lst_1.sort(reverse = True)
print(lst_1)
# 执行结果
[8, 7, 6, 5, 4, 3, 2, 1] print(lst_3)
# 执行结果
None

- 列表分片操作创造一个新的内存地址

 # 用列表分片创建一个新的列表
l = [0,1,2,3,4,5,6,7,8,9]
print(l)
# 执行结果
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(id(l))
# 执行结果
1943410856072 l_a = l
print(l_a)
# 执行结果
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(id(l_a))
# 执行结果
1943410856072 # 实验证明l_a直接指向的是l的地址而不是复制到新的地址去 # 利用分片使他创建新的内存区域
l_b = l[:]
print(l_b)
# 执行结果
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(id(l_b))
# 执行结果
1943411459912

Python数据类型的内置函数之list(列表)的更多相关文章

  1. Python数据类型的内置函数之tuple(元组),dict(字典),set(集合)

    Python数据类型内置函数 - str(字符串) - list(列表) - tuple(元组) - dict(字典) - set(收集) tuple(元组)的操作 - (count)统计元组中元素出 ...

  2. Python数据类型的内置函数之str(字符串)

    Python数据类型内置函数 - str(字符串) - list(列表) - tuple(元组) - dict(字典) - set(收集) str(字符串)的一些操作 - 字符串相连方法 # 字符串的 ...

  3. python数据类型常用内置函数之字符串

    1.strip, lstrip, rstrip x = ' jiahuifeng ' print(x.strip(' ')) print(x.lstrip(' ')) print(x.rstrip(' ...

  4. python学习交流 - 内置函数使用方法和应用举例

    内置函数 python提供了68个内置函数,在使用过程中用户不再需要定义函数来实现内置函数支持的功能.更重要的是内置函数的算法是经过python作者优化的,并且部分是使用c语言实现,通常来说使用内置函 ...

  5. 十五. Python基础(15)--内置函数-1

    十五. Python基础(15)--内置函数-1 1 ● eval(), exec(), compile() 执行字符串数据类型的python代码 检测#import os 'import' in c ...

  6. Python的常用内置函数介绍

    Python的常用内置函数介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.取绝对值(abs) #!/usr/bin/env python #_*_coding:utf-8_ ...

  7. python 常见的内置函数

    内置函数 接下来,我们就一起来看看python里的内置函数.截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是python提供给你直接可以拿来使用的所有函数.这 ...

  8. python之路——内置函数和匿名函数

    阅读目录 楔子 内置函数 匿名函数 本章小结 楔子 在讲新知识之前,我们先来复习复习函数的基础知识. 问:函数怎么调用? 函数名() 如果你们这么说...那你们就对了!好了记住这个事儿别给忘记了,咱们 ...

  9. Python---基础---数据类型的内置函数

    2019-05-23 ---------------------------- 一. #数据类型的内置函数Python有哪些数据类型?Number   数值型string   字符型list     ...

随机推荐

  1. [LeetCode&Python] Problem 53. Maximum Subarray

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  2. firefox浏览器,主动出现hao123的解决办法

    听说火狐浏览器前端开发很好用,今天下载了一个体验了一下觉得还是很不错的.但是有个问题!!!为什么我设置了启动时打开空白页没用,它每次都会给我打开 https://www.hao123.com/ hao ...

  3. poj 3641 快速幂

    Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a ...

  4. js中substr、substring、slice的区别

    substr(start, length) substring(from, to) slice(from, to) 以上函数只传一个参数时,认为是起始位置,然后按照正方向截取 substring的参数 ...

  5. [WC2006]水管局长

    原题链接 前言 搞不懂为什么要写LCT,搞不懂为什么要加强数据.像这道题是用父亲表示法来做的.虽然复杂度不是log,但是现在下面这份代码却是无论从空间,还是代码量,还是时间都是优秀不止一点. 而且这样 ...

  6. 19. Rootkit detectors (隐形工具包检测器 5个)

    Sysinternals提供了许多小型Windows实用程序,对于低级别的Windows黑客攻击来说非常有用. 一些是免费的和/或包括源代码,而其他是专有的. 调查受访者最喜欢:ProcessExpl ...

  7. docker(基础篇)

    http://naotu.baidu.com/file/f02773930afb2d3d9e71621249099d31 centos7安装  https://yq.aliyun.com/articl ...

  8. http://blog.csdn.net/u012905422/article/details/53340260

    轉自:http://blog.csdn.net/u012905422/article/details/53340260 对于python2.7版本,很多教程(如http://stackoverflow ...

  9. hg (Mercurial)multiple heads (hg 多头)、撤销 commit,并保留修改

    有时候 commit 后才意识到还未 pull,这个时候会有如下提示: wlan-0-182:mobile-v2 lixiumei$ hg pull -upulling from ssh://hg@b ...

  10. Spring配置从配置文件读取属性值

    spring将properties文件读取后在配置文件中直接将对象的配置信息填充到bean中的变量里. 原本使用PropertyPlaceholderConfigurer类进行文件信息配置.Prope ...