基础数据类型-list
序列是python中的基础数据结构,序列里每一个元素都有一个下标,从0开始,依次递增.
list,tuple,dictionary是使用最频繁的三类数据结构。
(1)序列都有的方法包括:索引,切片,检查成员,加,乘:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#索引
list_name = ['Paul', 'John', 'James']
print(list_name[1]) #切片
list_number = [1, 1, 2, 3, 4, 5]
print(list_number[1:5]) #检查成员
if 'Joshua' in list_name:
print("Joshua is in list_name")
else:
print("Joshua is not in list_name") #加
print(list_name + list_number) #乘
print(list_name * 2)
Code
John
[1, 2, 3, 4]
Joshua is not in list_name
['Paul', 'John', 'James', 1, 1, 2, 3, 4, 5]
['Paul', 'John', 'James', 'Paul', 'John', 'James']
Result
(2)遍历列表:
#遍历列表
for number in list_number:
print(number)
Code
(3)list的函数有len(),max(),min(),list()
#函数
print(len(list_number))
print(max(list_number))
print(min(list_number))
tuple_number = (1, 3, 5, 7)
print(type(tuple_number), type(list(tuple_number)))#list()强制将序列转化为list类型
Code
6
5
1
<class 'tuple'> <class 'list'>
Result
(4)list的常用方法有
-append(), extend(),insert();
#append(), extend(),insert();
list_number = [1, 1, 2, 3, 4, 5]
list_number.append(7) #在列表末尾添加新的对象
print(list_number)
list_number.extend([10, 11, 12]) #在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
print(list_number)
list_number.insert(1, 99) #list.insert(index, obj)将对象插入列表中index位置
print(list_number)
Code
[1, 1, 2, 3, 4, 5, 7]
[1, 1, 2, 3, 4, 5, 7, 10, 11, 12]
[1, 99, 1, 2, 3, 4, 5, 7, 10, 11, 12]
Result
-remove(),pop(),clear();
#remove(),pop(),clear();
list_number = [1, 1, 2, 3, 4, 5]
list_number.remove(1)#移除列表中某个值的第一个匹配项
print(list_number)
pop_number = list_number.pop(4)#list.pop(obj=list[-1]) 移除列表中的索引位置元素(默认最后一个元素),并且返回该元素的值
print("pop number:", pop_number)
print(list_number)
list_number.clear()#清空列表
print(list_number)
Code
[1, 2, 3, 4, 5]
pop number: 5
[1, 2, 3, 4]
[]
Result
-sort(),reverse();
#sort(),reverse();
list_number = [4, 2, 5, 7, 1, 3]
print(list_number)
list_number.sort() #对原列表进行排序
print(list_number)
list_number.reverse() #反向原列表中元素
print(list_number)
Code
[4, 2, 5, 7, 1, 3]
[1, 2, 3, 4, 5, 7]
[7, 5, 4, 3, 2, 1]
Result
-count(),index(),copy()
#count(),index(),copy()
list_number = [1, 1, 2, 2, 2, 3, 3, 3, 3]
print(list_number.count(3)) #统计某个元素在列表中出现的次数
print(list_number.index(2)) #从列表中找出某个值第一个匹配项的索引位置
print("address of list_number:", id(list_number))
copy_list_number = list_number.copy() #复制列表
print(copy_list_number)
print("address of copy_list_number:", id(copy_list_number))
Code
4
2
address of list_number: 35406832
[1, 1, 2, 2, 2, 3, 3, 3, 3]
address of copy_list_number: 35421720
Result
最后来看下list类的定义:
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
Class List
基础数据类型-list的更多相关文章
- [.net 面向对象编程基础] (3) 基础中的基础——数据类型
[.net 面向对象编程基础] (3) 基础中的基础——数据类型 关于数据类型,这是基础中的基础. 基础..基础..基础.基本功必须要扎实. 首先,从使用电脑开始,再到编程,电脑要存储数据,就要按类型 ...
- TypeScript学习指南第一章--基础数据类型(Basic Types)
基础数据类型(Basic Types) 为了搭建应用程序,我们需要使用一些基础数据类型比如:numbers,strings,structures,boolean等等. 在TypeScript中除了Ja ...
- 【Swift】学习笔记(一)——熟知 基础数据类型,编码风格,元组,主张
自从苹果宣布swift之后,我一直想了解,他一直没有能够把它的正式学习,从今天开始,我会用我的博客来驱动swift得知,据我们了解还快. 1.定义变量和常量 var 定义变量,let定义常量. 比如 ...
- 二、Windows基础数据类型
六.Windows Data Types 简介: 6.1.这些数据类型都是C语言数据类型的再次的进行包装. 6.2.因为考虑到如果使用的是C中的基础数据类型可能无法表示,想表示的精准的含义. 6.3. ...
- java基础数据类型包装类
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
- java.lang基础数据类型boolean、char、byte、short、int、long、float、double (JDK1.8)
java.lang.Boolean public static int hashCode(boolean value) { return value ? 1231 : 1237; } JDK 1.8新 ...
- Python基础数据类型之列表和元组
一.列表 list 列表是python中的基础数据类型之一,其他语言中也有类似于列表的数据类型,比如js中叫数组,他是以[]括起来,每个元素以逗号隔开,而且他里面可以存放各种数据类型比如: li ...
- Python基础数据类型之字典
基础数据类型之字典 ps:数据类型划分:可变数据类型和不可变数据类型. 不可变数据类型:元组(tupe).布尔值(bool).整数型(int).字符串(str).不可变数据类型也称为可哈希. 可变 ...
- Python基础数据类型之集合以及其他和深浅copy
一.基础数据类型汇总补充 list 在循环一个列表时,最好不要删除列表中的元素,这样会使索引发生改变,从而报错(可以从后向前循环删除,这样不会改变未删元素的索引). 错误示范: lis = [,,, ...
- python基础二(基础数据类型)
一. 引子 1. 什么是数据 x=10,10是我们要存储的数据 2. 为何数据要分不同的类型 数据是用来表示状态的,不同的状态就应该用不同的类型的数据去表示 3.数据类型 数字 字符串 列表 元组 字 ...
随机推荐
- SQLMAP使用详解
使用示例 python sqlmap.py -u "http://xx.com/member.php?id=XX" -p id --dbms "Mysql" ...
- phalcon框架与Volt 模块引擎 使用简介
———— 近期工作中web页面使用由C语言编写的Volt模板引擎,相比之前由js动态加载页面速度更快,更利于百度数据的抓取,现根据文档整理一下使用思路 (Volt是一个超快速和设计者友好的模板语言 ...
- Mysql的TIMESTAMPDIFF和TIMESTAMPADD的用法
[1.]TIMESTAMPDIFF(interval,colum1,colum2) 字段类型:date或者datetime 计算过程:colum2减去colum1,即后面的减去前面的 计算结果:整数 ...
- DOM节点操作阶段性总结
HTML中能看到的所有东西都是dom树中的一个节点,注意是“所有”,使用childNodes()可以看到,回车(换行)也是一个节点. 从上图可以看到,select中有四个option,但是有9个节点. ...
- 解决微信小程序安卓手机访问不到图片,无法显示图片
关于微信小程序不显示图片 通病可能有以下几个可能性: 非本地图片:确定图片资源存在,copy 图片url再浏览器打开,确定图片资源存在且能正常访问 本地图片:确定相对路径或者绝对路径正确 微信小程序图 ...
- 数据库5.7-jdbc版本8.0.12驱动连接
现在版本的jdbc连接方式和原来不一样了, 假如你使用String driver = "com.mysql.jdbc.Driver"; 会抛出错误: Loading class ` ...
- 【Hadoop故障处理】在高可用(HA)配置下,8088端口无法访问,resourcemanager进程无法启动问题
[故障背景] 8088网页打不开,因8088是yarn平台的端口,所以我从yarn开始排查,首先到各个机器上使用jps命令查看yarn的各个节点是否启动,发现虽然有nodemanager进程,但是主节 ...
- wordpress网站程序漏洞修复办法
近日wordpress被爆出高危的网站漏洞,该漏洞可以伪造代码进行远程代码执行,获取管理员的session以及获取cookies值,漏洞的产生是在于wordpress默认开启的文章评论功能,该功能在对 ...
- Git项目管理
参考 参考书籍 <git学习指南> 参考网站 https://git-scm.com/ Git局限性讨论 高复杂度 两张图看懂集中式版本管理系统和分布式管理系统的区别-集中式vs分布式 g ...
- Strange RadioButton group behavior with ToolBar
原文地址:https://social.msdn.microsoft.com/Forums/vstudio/zh-CN/83352293-ca52-4e22-8092-8e23c453bc75/str ...