python基本数据类型——list
一、创建列表:
- li = []
- li = list()
- name_list = ['alex', 'seven', 'eric']
- name_list = list(['alex', 'seven', 'eric'])
二、基本操作:
- #append追加
- name_list = ["zhangyanlin","suoning","nick"]
- name_list.append('zhang')
- print(name_list)
- #count制定字符出现几次
- name_list = ["zhangyanlin","suoning","nick"]
- name_list.append('zhang')
- name_list.append('zhang')
- name_list.append('zhang')
- print(name_list.count('zhang'))
- #extend可扩展,批量往里加数据
- name_list = ["zhangyanlin","suoning","nick"]
- name = ["aylin","zhang","yan","lin"]
- name_list.extend(name)
- print(name_list)
- #index找到字符所在的位置
- name_list = ["zhangyanlin","suoning","nick"]
- print(name_list.index('nick'))
- #insert插入,往索引里面插入值
- name_list = ["zhangyanlin","suoning","nick"]
- name_list.insert(1,"zhang")
- print(name_list)
- #pop在原列表中移除掉最后一个元素,并赋值给另一个变量
- name_list = ["zhangyanlin","suoning","nick"]
- name = name_list.pop()
- print(name)
- #remove移除,只移除从左边找到的第一个
- name_list = ["zhangyanlin","suoning","nick"]
- name_list.remove('nick')
- print(name_list)
- #reverse反转
- name_list = ["zhangyanlin","suoning","nick"]
- name_list.reverse()
- print(name_list)
- #del删除其中元素,删除1到3之间的
- name_list = ["zhangyanlin","suoning","nick"]
- del name_list[1:3]
- print(name_list)
- #join将列表元素用指定字符串连接
- name_list = ["you","are","good"]
- s = " ".join(name_list)
- print(name_list)
# you are good
- class list(object):
- """
- list() -> new empty list
- list(iterable) -> new list initialized from iterable's items
- """
- def append(self, p_object): # real signature unknown; restored from __doc__
- """ L.append(object) -- append object to end """
- pass
- def count(self, value): # real signature unknown; restored from __doc__
- """ L.count(value) -> integer -- return number of occurrences of value """
- return 0
- def extend(self, iterable): # real signature unknown; restored from __doc__
- """ L.extend(iterable) -- extend list by appending elements from the iterable """
- pass
- def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
- """
- L.index(value, [start, [stop]]) -> integer -- return first index of value.
- Raises ValueError if the value is not present.
- """
- return 0
- def insert(self, index, p_object): # real signature unknown; restored from __doc__
- """ L.insert(index, object) -- insert object before index """
- pass
- def pop(self, index=None): # real signature unknown; restored from __doc__
- """
- L.pop([index]) -> item -- remove and return item at index (default last).
- Raises IndexError if list is empty or index is out of range.
- """
- pass
- def remove(self, value): # real signature unknown; restored from __doc__
- """
- L.remove(value) -- remove first occurrence of value.
- Raises ValueError if the value is not present.
- """
- pass
- def reverse(self): # real signature unknown; restored from __doc__
- """ L.reverse() -- reverse *IN PLACE* """
- pass
- def sort(self, cmp=None, key=None, reverse=False): # real signature unknown; restored from __doc__
- """
- L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
- cmp(x, y) -> -1, 0, 1
- """
- pass
- def __add__(self, y): # real signature unknown; restored from __doc__
- """ x.__add__(y) <==> x+y """
- pass
- def __contains__(self, y): # real signature unknown; restored from __doc__
- """ x.__contains__(y) <==> y in x """
- pass
- def __delitem__(self, y): # real signature unknown; restored from __doc__
- """ x.__delitem__(y) <==> del x[y] """
- pass
- def __delslice__(self, i, j): # real signature unknown; restored from __doc__
- """
- x.__delslice__(i, j) <==> del x[i:j]
- Use of negative indices is not supported.
- """
- pass
- def __eq__(self, y): # real signature unknown; restored from __doc__
- """ x.__eq__(y) <==> x==y """
- pass
- def __getattribute__(self, name): # real signature unknown; restored from __doc__
- """ x.__getattribute__('name') <==> x.name """
- pass
- def __getitem__(self, y): # real signature unknown; restored from __doc__
- """ x.__getitem__(y) <==> x[y] """
- pass
- def __getslice__(self, i, j): # real signature unknown; restored from __doc__
- """
- x.__getslice__(i, j) <==> x[i:j]
- Use of negative indices is not supported.
- """
- pass
- def __ge__(self, y): # real signature unknown; restored from __doc__
- """ x.__ge__(y) <==> x>=y """
- pass
- def __gt__(self, y): # real signature unknown; restored from __doc__
- """ x.__gt__(y) <==> x>y """
- pass
- def __iadd__(self, y): # real signature unknown; restored from __doc__
- """ x.__iadd__(y) <==> x+=y """
- pass
- def __imul__(self, y): # real signature unknown; restored from __doc__
- """ x.__imul__(y) <==> x*=y """
- pass
- def __init__(self, seq=()): # known special case of list.__init__
- """
- list() -> new empty list
- list(iterable) -> new list initialized from iterable's items
- # (copied from class doc)
- """
- pass
- def __iter__(self): # real signature unknown; restored from __doc__
- """ x.__iter__() <==> iter(x) """
- pass
- def __len__(self): # real signature unknown; restored from __doc__
- """ x.__len__() <==> len(x) """
- pass
- def __le__(self, y): # real signature unknown; restored from __doc__
- """ x.__le__(y) <==> x<=y """
- pass
- def __lt__(self, y): # real signature unknown; restored from __doc__
- """ x.__lt__(y) <==> x<y """
- pass
- def __mul__(self, n): # real signature unknown; restored from __doc__
- """ x.__mul__(n) <==> x*n """
- pass
- @staticmethod # known case of __new__
- def __new__(S, *more): # real signature unknown; restored from __doc__
- """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
- pass
- def __ne__(self, y): # real signature unknown; restored from __doc__
- """ x.__ne__(y) <==> x!=y """
- pass
- def __repr__(self): # real signature unknown; restored from __doc__
- """ x.__repr__() <==> repr(x) """
- pass
- def __reversed__(self): # real signature unknown; restored from __doc__
- """ L.__reversed__() -- return a reverse iterator over the list """
- pass
- def __rmul__(self, n): # real signature unknown; restored from __doc__
- """ x.__rmul__(n) <==> n*x """
- pass
- def __setitem__(self, i, y): # real signature unknown; restored from __doc__
- """ x.__setitem__(i, y) <==> x[i]=y """
- pass
- def __setslice__(self, i, j, y): # real signature unknown; restored from __doc__
- """
- x.__setslice__(i, j, y) <==> x[i:j]=y
- Use of negative indices is not supported.
- """
- pass
- def __sizeof__(self): # real signature unknown; restored from __doc__
- """ L.__sizeof__() -- size of L in memory, in bytes """
- pass
- __hash__ = None
- list
list源码
三、数据类型转换
字符串转列表
- s = "你好morra"
- li = list(s)
- print(li)
- OUTPUT:
- ['你', '好', 'm', 'o', 'r', 'r', 'a']
元组转列表
- tu = ("你好","alex")
- li = list(tu)
- print(li)
- OUTPUT:
- ['你好', 'alex']
字典转列表
- dic = {'k1':'hello','k2':'morra'}
- l3 = list(dic) #字典在循环的时候默认只循环key
- print(l3)
- l4 = list(dic.values())
- print(l4)
- l5 = list(dic.items())
- print(l5)
- OUTPUT:
- ['k2', 'k1']
- ['morra', 'hello']
- [('k2', 'morra'), ('k1', 'hello')]
四、可迭代性
- l = ['i', 'am', 'spark']
- # 可以被for循环所迭代
- for i in l:
- print (i)
- # i am spark
五、可嵌套性
- li = ['字符串',('tuple','hh'),{"key1":"value1","key2":"value2"}]
- print(li[2]["key1"])
- #输出 value1
python基本数据类型——list的更多相关文章
- python 基本数据类型分析
在python中,一切都是对象!对象由类创建而来,对象所拥有的功能都来自于类.在本节中,我们了解一下python基本数据类型对象具有哪些功能,我们平常是怎么使用的. 对于python,一切事物都是对象 ...
- python常用数据类型内置方法介绍
熟练掌握python常用数据类型内置方法是每个初学者必须具备的内功. 下面介绍了python常用的集中数据类型及其方法,点开源代码,其中对主要方法都进行了中文注释. 一.整型 a = 100 a.xx ...
- 闲聊之Python的数据类型 - 零基础入门学习Python005
闲聊之Python的数据类型 让编程改变世界 Change the world by program Python的数据类型 闲聊之Python的数据类型所谓闲聊,goosip,就是屁大点事可以咱聊上 ...
- python自学笔记(二)python基本数据类型之字符串处理
一.数据类型的组成分3部分:身份.类型.值 身份:id方法来看它的唯一标识符,内存地址靠这个查看 类型:type方法查看 值:数据项 二.常用基本数据类型 int 整型 boolean 布尔型 str ...
- Python入门-数据类型
一.变量 1)变量定义 name = 100(name是变量名 = 号是赋值号100是变量的值) 2)变量赋值 直接赋值 a=1 链式赋值 a=b=c=1 序列解包赋值 a,b,c = 1,2,3 ...
- Python基础:八、python基本数据类型
一.什么是数据类型? 我们人类可以很容易的分清数字与字符的区别,但是计算机并不能,计算机虽然很强大,但从某种角度上来看又很傻,除非你明确告诉它,"1"是数字,"壹&quo ...
- python之数据类型详解
python之数据类型详解 二.列表list (可以存储多个值)(列表内数字不需要加引号) sort s1=[','!'] # s1.sort() # print(s1) -->['!', ' ...
- Python特色数据类型(列表)(上)
Python从零开始系列连载(9)——Python特色数据类型(列表)(上) 原创 2017-10-07 王大伟 Python爱好者社区 列表 列表,可以是这样的: 分享了一波我的网易云音乐列表 今天 ...
- 【Python】-NO.97.Note.2.Python -【Python 基本数据类型】
1.0.0 Summary Tittle:[Python]-NO.97.Note.2.Python -[Python 基本数据类型] Style:Python Series:Python Since: ...
- python基本数据类型之集合
python基本数据类型之集合 集合是一种容器,用来存放不同元素. 集合有3大特点: 集合的元素必须是不可变类型(字符串.数字.元组): 集合中的元素不能重复: 集合是无序的. 在集合中直接存入lis ...
随机推荐
- hog特征及其提取方法图示
1 什么是hog特征 hog特征是histogram of gradient的缩写.我们观察图像时,信息更多来自目标边沿的突变.我们计算一块区域内的所有像素处的梯度信息,即突变的方向和大小,然后对36 ...
- 关于web测试收集
页面部分 页面清单是否完整(是否已经将所需要的页面全部都列出来了) 页面是否显示(在不同分辨率下页面是否存在,在不同浏览器版本中页面是是否显示) 页面在窗口中的显示是否正确.美观(在调整浏览器窗口大小 ...
- ListView的简单使用
首先在主界面建立一个ListView的布局
- gzip 与 gunzip 语法与示例
gzip 与 gunzip 语法与示例 语法: gunzip -c 被压缩的文件 > 已解压的文件示例: 将 catalina.out.gz 文件解压到 catalina.out 文件中: gu ...
- vue搭建开发环境
windows下搭建vue开发环境 一.安装node.js 安装 vue项目通常通过webpack工具来构建,而webpack命令的执行是依赖node.js环境,所以首先要安装node.js. n ...
- 视频swiper轮播
关于本次文章的内容,实际上是咪咕阅读详情页中的一个前端需求要做的效果,不过比起原需求,此次案例已经被删减掉许多部分了.音频部分舍弃,调用客户端接口舍弃,并做一些整理.最后留下的是这个精简版的案例.方便 ...
- iOS 开发遇到 调不起相机问题
在iOS 开发中 使用html 中的input 标签调起工程里面的相机,手机无反应 1.先看看info.plist 加没加相机的权限,添加Privacy - Camera Usage Descript ...
- Terminating app due to uncaught exception 'NSUnknownKeyException' this class is not key value coding-compliant for the key
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ViewController > se ...
- 关于bootstrap原理及优缺点
网格系统的实现原理,是通过定义容器大小,平分12份(也有平分成24份或32份,但12份是最常见的),再调整内外边距,最后结合媒体查询,就制作出了强大的响应式网格系统.Bootstrap框架中的网格系统 ...
- NTP时间服务器
1. NTP简介 NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议.它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0 ...