参考:List Tuple

Note

List

List是Python中一个很吊的数据结构,类似C语言的数组。

1.定义:listname = [variable 1, v2, v3, ..., vn]

eg.

#!/usr/bin/env python3

classmates = ['Chen', 'Li', 'Wang']

print(classmates)

output:

sh-3.2# ./list1.py
['Chen', 'Li', 'Wang']

2.可以使用索引来访问list中的元素,即C语言数组中的a[i],通过索引i访问数组中第i个元素。

3.list可以使用负数作为索引,如索引-i,代表从list最后第i个元素。

eg.

i = int(input())

print(classmates[i])

output:

sh-3.2# ./list1.py
['Chen', 'Li', 'Wang']
0
Chen sh-3.2# ./list1.py
['Chen', 'Li', 'Wang']
1
Li sh-3.2# ./list1.py
['Chen', 'Li', 'Wang']
2
Wang sh-3.2# ./list1.py
['Chen', 'Li', 'Wang']
-1
Wang sh-3.2# ./list1.py
['Chen', 'Li', 'Wang']
-2
Li sh-3.2# ./list1.py
['Chen', 'Li', 'Wang']
-3
Chen

当然,访问的索引不能越界。

4.List是可变长的有序表,可以往list的末尾加入元素,使用的是append()方法。

eg.

classmates.append('Zhang')

print(classmates)

output:

['Chen', 'Li', 'Wang', 'Zhang']

5.也可以把元素插入到指定位置,使用的是insert()方法。

eg.

classmates.insert(3, 'Chang')    # 将元素'Chang'插入到list的第三个位置

print(classmates)

output:

['Chen', 'Li', 'Wang', 'Zhang']
['Chen', 'Li', 'Wang', 'Chang', 'Zhang']

6.删除末尾元素,使用的是pop()方法,删除索引为i的元素,使用的是pop(i)方法。

eg.

print(classmates)

classmates.pop()    # 删除最后一个元素'Zhang'

print(classmates)

classmates.pop(1)   # 删除第一个元素'Li'

print(classmates)

output:

['Chen', 'Li', 'Wang', 'Chang', 'Zhang']
['Chen', 'Li', 'Wang', 'Chang']
['Chen', 'Wang', 'Chang']

7.替换第i个元素:listname[i] = [替换元素]

eg.

print(classmate)

classmates[0] = 'Lingzhizzz'

print(classmates)

output:

['Chen', 'Li', 'Wang', 'Chang', 'Zhang']
['Lingzhizzz', 'Li', 'Wang', 'Chang', 'Zhang']

8.list元素类型也可以不一样

eg.

love = ['money', '952693358', 15, 32, 23, 9]

print(love)

output:

['money', '952693358', 15, 32, 23, 9]

9.用方法len()计算list的长度:

eg.

print(len(love))

output:

6

10.list可以相互嵌套,即list中的元素可以是list,类似C语言二维数组。

eg.

classmates.append(love)

print(classmates)

output:

['Lingzhizzz', 'Li', 'Wang', 'Chang', 'Zhang', ['money', '952693358', 15, 32, 23, 9]]

访问listA中的元素listB中的元素(真绕= =),比如访问classmates中love中的'952693358'元素,方法是:classmates[5][1]。

eg.

print(classmates[5][1])

print(classmate[5])

output:

15
['money', '952693358', 15, 32, 23, 9]

11.如果list中一个元素都没有,就是一个空的list,长度为0.

Tuple

Python中还有一种有序列表,叫做元组tuple,tuple和list非常类似,但是tuple一旦初始化就不能修改

1.tuple中的元素不能修改。

2.tuple定义:tuplename = (v1, v2, v3, ..., vn)

eg.

#!/usr/bin/env python3

mates = (1, 'a', 'c')

print(mates)

output:

sh-3.2# ./tuple1.py
(1, 'a', 'c')

3.注意:当tuple只有一个元素的时候,需要在该元素后面加“,”!

原因:这是因为括号()既可以表示tuple,又可以表示数学公式中的小括号,这就产生了歧义,因此,Python规定,这种情况下,按小括号进行计算。

eg.

mates = (1) # 此时mates是整数

print(mates) 

mates = (1,) # 此时mates是tuple

print(mates)

4.可变成的tuple,当tuple里面的元素有list的时候,list是可变长的。

tuple所谓的“不变”是说,tuple的每个元素,指向永远不变。即指向'a',就不能改成指向'b',指向一个list,就不能改成指向其他对象,但指向的这个list本身是可变的!

eg.

#!/usr/bin/env python3

mates = (1, 'a', 'c', ['Chen', 'Lingzhizzz'])

print(mates)

mates[3].append('Zhang')

print(mates)

mates[3].pop()

print(mates)

output:

sh-3.2# ./tuple1.py
(1, 'a', 'c', ['Chen', 'Lingzhizzz'])
(1, 'a', 'c', ['Chen', 'Lingzhizzz', 'Zhang'])
(1, 'a', 'c', ['Chen', 'Lingzhizzz'])

练习

请用索引取出下面list的指定元素:

# -*- coding: utf-8 -*-

L = [
['Apple', 'Google', 'Microsoft'],
['Java', 'Python', 'Ruby', 'PHP'],
['Adam', 'Bart', 'Lisa']
] # 打印Apple:
print(?)
# 打印Python:
print(?)
# 打印Lisa:
print(?)

Ans:

#!/usr/bin/env python3
# -*- coding: utf-8 -*- L = [
['Apple', 'Google', 'Microsoft'],
['Java', 'Python', 'Ruby', 'PHP'],
['Adam', 'Bart', 'Lisa']
] # 打印Apple:
print(L[0][0]) # 打印Python:
print(L[1][1]) # 打印Lisa:
print(L[2][2])

Output:

sh-3.2# ./list2.py
Apple
Python
Lisa

2017/1/25

Python学习札记(六) Basic3 List和Tuple的更多相关文章

  1. python学习第六讲,python中的数据类型,列表,元祖,字典,之列表使用与介绍

    目录 python学习第六讲,python中的数据类型,列表,元祖,字典,之列表使用与介绍. 二丶列表,其它语言称为数组 1.列表的定义,以及语法 2.列表的使用,以及常用方法. 3.列表的常用操作 ...

  2. Python学习第六课

    Python学习第六课 课前回顾 列表 创建 通过 [] :写在[]里,元素之间用逗号隔开 对应操作: 查 增 append insert 改(重新赋值) 删除(remove del pop(删除后会 ...

  3. Python学习笔记六

    Python课堂笔记六 常用模块已经可以在单位实际项目中使用,可以实现运维自动化.无需手工备份文件,数据库,拷贝,压缩. 常用模块 time模块 time.time time.localtime ti ...

  4. Python学习札记(三十六) 面向对象编程 Object Oriented Program 7 __slots__

    参考:slots NOTE 1.动态语言灵活绑定属性及方法. #!/usr/bin/env python3 class MyClass(object): def __init__(self): pas ...

  5. Python学习札记(十六) 高级特性2 迭代

    参考:迭代 Note 1.如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们称为迭代(Iteration). 在C.C++.Java等语言中,for循 ...

  6. Python学习札记(二十六) 函数式编程7 修饰器

    修饰器 NOTE 1.函数对象有一个__name__属性,可以拿到函数的名字: #!/usr/bin/env python3 def now(): print('2017/2/19') def mai ...

  7. Python学习第六课——基本数据类型一之tuple and dict

    元组 (tuple) tu=(11,22,(123,456),[22,55],) # 一般定义元组的时候最后面加一个, # 元组不能被修改或者删除 v = tu[0] # 也可以根据索引取值 prin ...

  8. Python学习 :六个标准数据类型

    一.Numbers(数字类型) 数字类型主要分为两种—— 整数(Integer)与 浮点数(Float) 整数分为整型和长整型(在Python3中已经不再区分为整型与长整型,统一称为整型) 注意:数字 ...

  9. python学习(六)元组学习

    元组就是列表的一种,不过元组具有不可变性,而且是用圆括号访问的. 索引(下表索引或者键索引都是用的中括号) #!/usr/bin/python # 这节来学习元组, tuple, 基本上就像一个不可以 ...

随机推荐

  1. px、dp、sp、mm、in、pt这些单位有什么区别?

    相信每个Android新手都会遇到这个问题,希望这篇帖子能让你不再纠结. px: 即像素,1px代表屏幕上一个物理的像素点: px单位不被建议使用,因为同样100px的图片,在不同手机上显示的实际大小 ...

  2. VC++SDK编程——模拟时钟

    #include <Windows.h> #include <tchar.h> #include <math.h> typedef struct Time { in ...

  3. nginx 哈希表结构图

  4. MySQL索引优化案例浅析

    MySQL是关系型数据库的一种,查询功能强,数据一致性高,数据安全性高,支持二级索引.但是性能比起非关系型数据库稍弱,特别是百万级以上的数据,很容易出现查询慢的现象.这时候要分析慢的原因,一般情况下是 ...

  5. python web框架 django 练习1 django 1.11版本

    django练习 在我自己项目里创建一个xiaoliu的文件夹 里面创建s1.py 文件 s1.py文件 里面写各种函数 from django.shortcuts import HttpRespon ...

  6. SIP中的 session, dialog 及 transaction 的解释

    http://stackoverflow.com/questions/35133331/difference-between-session-dialog-and-transaction-in-sip ...

  7. mysql lock

    http://blog.chinaunix.net/uid-21505614-id-289450.html http://bbs.csdn.net/topics/340127237 http://ww ...

  8. 记一次服务器迁移 TFS客户端ip更换

    服务器迁移,TFS服务端IP由原10.58.8.231更换至10.58.1.230 TFS客户端更换ip操作比较复杂,请谨慎操作,避免脱库的风险!!! 打开注册表,运行->regedit 找到H ...

  9. Java设计原则—开闭原则(转)

    原文出自:http://www.cnblogs.com/muzongyan/archive/2010/08/05/1793454.html 开闭原则(Open Closed Principle)是Ja ...

  10. 2017浙江省赛 B - Problem Preparation ZOJ - 3959

    地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3959 题目: It's time to prepare the pr ...