list和tuple

编译安装python3,指定安装路径,ln -s做个软连接,网上有很多方法。

接着昨天的中文注释就ok啦!

定个小目标吧!3个月实习期学习300小时1000个脚本,这个可能不太好定义。比如后期去做个爬虫的项目,就行几十行代码,可能需要一周…就像这个list和tuple花了两天才理解

>>> t = ('a', 'b', ['A', 'B'])

>>> t[2][0] = 'X'

>>> t

('a', 'b', ['X', 'B'])

>>> t[2][1] = 'Y'

然后t是这样的,

想明白就很简单了

a,b[X,Y]

总之,练习不断敲打才是王道。

好,直接上分

9.中文支持

[root@localhost xiaoai]# cat 9.py

#!/usr/bin/evo python

#coding:utf-8

print ('中文')

10.list-定义和查看

assmates=['xiaoai','bobo','marry']

>>> classmates

['xiaoai', 'bobo', 'marry']

>>> len(classmates)

3

>>> classmates[0]

'xiaoai'

>>> classmates[1]

'bobo'

>>> classmates[2]

'marry'

>>> classmates[3]

Traceback (most recent call last):

File "", line 1, in

IndexError: list index out of range

>>> classmates[-1]

'marry'

>>> classmates[-2]

'bobo'

>>> classmates[-3]

'xiaoai'

>>> classmates[-4]

Traceback (most recent call last):

File "", line 1, in

IndexError: list index out of range

>>>

#list 元素 len()使用#list 元素 len()使用#list 元素 len()使用

11.list-插入元素

[root@VM_0_6_centos ~]# cat list2.txt

assmates insert(1,'jack')

File "", line 1

classmates insert(1,'jack')

^

SyntaxError: invalid syntax

>>> classmates. insert(1,'jack')

>>> classmates

['xiaoai', 'jack', 'bobo', 'marry']

插入时候在前面要加.结束,报语法错误

12.list3-替换元素

[root@VM_0_6_centos ~]# cat list3

cat: list3: No such file or directory

[root@VM_0_6_centos ~]# cat list3.txt

['xiaoai', 'bobo', 'marry']

>>> classmates[1]='swift'

>>> classmates

['xiaoai', 'swift', 'marry']

#替换元素

>>> L=['apple',1989,'samba']

>>> L

['apple', 1989, 'samba']

#数据类型不同元素

>>> a=['python','java',['asp','php'],'scheme']

>>> len(a)

4

>>> p=['asp','php']

>>> s

Traceback (most recent call last):

File "", line 1, in

NameError: name 's' is not defined

>>> s=['python','java',p,'scheme']

>>> s[1]

'java'

>>> s[2]

['asp', 'php']

>>> s[3]

'scheme'

#这个有问题,暂时过了

13.删除list中元素

[root@VM_0_6_centos ~]# cat pop.txt

assmates

['xiaoai', 'jack', 'bobo', 'marry']

>>> classmates.pop(1)

'jack'

>>> classmates

['xiaoai', 'bobo', 'marry']

#删除指定元素

14.tuple

[root@VM_0_6_centos ~]# cat tuple.txt

>>> A=('xiaoai','king','tokoy')

>>> A

('xiaoai', 'king', 'tokoy')

>>> t=(3308,'joy')

>>> t

(3308, 'joy')

#定义空tuple

>>> n=()

>>> n

()

要定义一个只有1个元素的tuple,如果你这么定义:

>>>c=(1)

>>>c

1

>>>

>>>

#定义的不是tuple,是1这个数!这是因为括号()既可以表示tuple,又可以表示数学公式中的小括号,这就产生了歧义,因此,Python规定,这种情况下,按小括号进行计算,计算结果自然是1。

所以,只有1个元素的tuple定义时必须加一个逗号,,来消除歧义:

>>> ss=(1,)

>>> ss

(1,)

放大招,可变tuple

>>> z=('xiaoai','aixiao',['XIAOAI','AIXIAO'])

>>> z[2][0]='ShangHai'

>>> z[2][1]='BeiJing'

>>> z

('xiaoai', 'aixiao', ['ShangHai', 'BeiJing'])

#

>>> L

[['Apple', 'Google', 'Microsoft'], ['Java', 'Python', 'Ruby', 'PHP'], ['Adam', 'Bart', 'Lisa']]

>>> L[0]

['Apple', 'Google', 'Microsoft']

>>> print([0][0])

>>> print(L[0][0])

Apple

>>> print (L[2][2])

Lisa

>>> print (L[2][2])

Lisa

>>> print(L[1])

['Java', 'Python', 'Ruby', 'PHP']

>>> print(L[1][1])

Python

>>>

  • 发表于:
  • 原文链接:http://kuaibao.qq.com/s/20171228G041WA00?refer=cp_1026

同媒体快讯

扫码关注云+社区