python零基础教程第二课_《Python基础教程》学习的第二课0120

今日学习列表与元组

1.序列

>>> a=[‘RP‘,22]

>>> b=[‘RY‘,55]

>>> database=[a,b]

>>> database

[[‘RP‘, 22], [‘RY‘, 55]]

>>>

2.索引

>>> hello=‘nihao‘

>>> hello[0]

‘n‘

>>> #也可以这样写

>>> ‘nihao‘[0]

‘n‘

>>>

3.分片

>>> numbers=[1,2,3,4,5,6,7,8,9,10]     //编号的使用,正着数是从0开始计数,倒着数也是从0开始计数。所以一般最后一个元素不好取到。

>>> numbers[3:6]

[4, 5, 6]

>>> #分片

>>> numbers[0:1]

[1]

>>> numbers[7:10]

[8, 9, 10]

>>> numbers[-1:-3]

[]

>>> numbers[-3:-1]

[8, 9]

>>> numbers[-3:]     //输出直到最后一个元素

[8, 9, 10]

>>> numbers[:3]     //从首位元素输出到指定元素

[1, 2, 3]

>>> numbers[:]       //输出全部元素

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

一个简单的示例:

>>> url=raw_input(‘please enter the url:‘)

please enter the url:http://www.python.org

>>> domain=url[11:-4]

>>> print "domain name: "+domain

domain name: python

>>>

3.1分片--以更大的步长

上面的例子中增加步长是1,接下来可以使用不同的步长。

>>> numbers[0:10:1]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]                                                           //  a:b:c其中a是指定的起始位,b是指定的末位,c是指定的步长。

>>> numbers[0:10:3]

[1, 4, 7, 10]

>>> numbers[3:6:3]

[4]

>>>

一个小tip

>>> numbers[::4]                                                    //需要将每4个中的第一个提取出来,只需要将步长设置为4.

[1, 5, 9]

步长可以为负数,不可以为0;(负数即为倒着输出)

>>> numbers[8:3:-2]

[9, 7, 5]

>>>

简单总结:如果要得到需要的输出必须知道如何设置abc参数。

4.序列相加

>>> #序列相加

>>> [1,2,3,4]+[5,6,7]

[1, 2, 3, 4, 5, 6, 7]

>>> [1,2,3]+‘world‘                                   //列表和字符串无法连接在一起

Traceback (most recent call last):

File "", line 1, in

[1,2,3]+‘world‘

TypeError: can only concatenate list (not "str") to list

>>>

5.乘法

>>> ‘nihao ‘*5                                                        //字符串乘法

‘nihao nihao nihao nihao nihao ‘

>>> [1,5]*3                                                             //列表乘法

[1, 5, 1, 5, 1, 5]

>>> x=‘hello‘                                                             //字符串乘法

>>> x*5

‘hellohellohellohellohello‘

>>> wo=[you ]*6

Traceback (most recent call last):

File "", line 1, in

wo=[you ]*6

NameError: name ‘you‘ is not defined

>>> wo =[None]*2                                                  //这里可以看出只有pyhton定义的关键字可以不加‘ ’,而非关键字则需要加‘ ’以字符的形式带入运算。

>>> wo

[None, None]

>>>

7.成员资格--in

>>> u=[‘foo‘,‘wo‘,‘koo‘]                                //列表里不允许查找非字符串

>>> ‘o‘in u

False

>>> ‘foo‘in u

True

>>> sub=‘ni$$wo%%ta‘

>>> ‘$‘in sub

True

>>> ‘P‘in ‘Python‘

True

>>>

8.长度最小值和最大值

>>> members=[100,34,68]

>>> len(members)

3

>>> min(members)

34

>>> max(members)

100

>>> len([45,26])

2

>>> min([45,26])

26

>>> len(‘jack‘)

4

>>> min(‘abcxyz‘)

‘a‘

>>> max(2+3,5+6)

11

>>>

9.列表

9.1   list

>>> #list 函数

>>> list(‘hello‘)                                          //运用list将字符串转化成列表

[‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘]

>>> x=[1,21,1]

>>> x[1]=2

>>> x

[1, 2, 1]

>>> name=[‘alice‘,‘bob‘,‘tom‘]

>>> del name[2]                                           //删除列表中的元素

>>> name

[‘alice‘, ‘bob‘]

>>>

9.2   分片赋值

>>> name=list(‘perl‘)

>>> name

[‘p‘, ‘e‘, ‘r‘, ‘l‘]

>>> name[2:]

[‘r‘, ‘l‘]

>>> ‘arr‘=name[2:]                                                                   //字符串若没有list成列表则不能直接赋值

SyntaxError: can‘t assign to literal

>>> name[2:]=list(‘arr‘)                                                             //list后可以赋值

>>> name

[‘p‘, ‘e‘, ‘a‘, ‘r‘, ‘r‘]

注意:列表与列表的赋值

>>> number=[1,5]

>>> number[2:]=[2,3,4]

>>> number

[1, 5, 2, 3, 4]

>>> number=[1,2,3,4,5,6,7,8,9,10]

>>> number[2:9:2]=[11,12,13,14]                //以指定步长赋值

>>> number

[1, 2, 11, 4, 12, 6, 13, 8, 14, 10]

>>> number[2:9:2]=[11,12,13,14,15]

//若多加一个15则超出原始列表的范围,会造成错误。

Traceback (most recent call last):

File "", line 1, in

number[2:9:2]=[11,12,13,14,15]

ValueError: attempt to assign sequence of size 5 to extended slice of size 4

>>>

9.3   列表方法

append

>>> #列表方法--append    调用方法: 对象.方法(参数)

>>> lst=[1,2,3]

>>> lst.append(4)

>>> lst

[1, 2, 3, 4]

>>> #append用于在列表末尾添加新的对象

>>>

count

>>> #count用于统计某个元素在列表中的出现次数

>>> [‘to‘,‘be‘,‘or‘,‘not‘,‘to‘,‘be‘,‘is‘,‘a‘,‘question‘].count(‘to‘)

2

>>> x=[1,2,[3,4],5,[3,4],[3,[7,8]]]

>>> x.count(3)

0

>>> x.count([3,4])

2

>>> x.count([7,8])

0

>>>

extend

>>> #extend用于在列表的末尾一次性追加另一个序列中的多个值(可以用新的列表拓展原有的列表)

>>> a=[1,2,3]

>>> b=[10,11,12]

>>> a.extend(b)

>>> a

[1, 2, 3, 10, 11, 12]

>>> #此操作看起来像连接,二者区别是:extend方法修改了被扩展的序列,而原始的连接会返回一个全新的列表

>>> a=[1,2,3]

>>> b=[4,5,6]

>>> a+b

[1, 2, 3, 4, 5, 6]

>>> a

[1, 2, 3]

>>>

insert

>>> #insert用于将对象插入到列表中

>>> numbers=[1,2,3,4,5,6,7,8]

>>> numbers.insert(3,‘four‘)

>>> numbers

[1, 2, 3, ‘four‘, 4, 5, 6, 7, 8]

>>> numbers[1:7:2]=[‘R‘]               //这里出现了问题,我指定的起始是1,终点是7,每隔两个步长将‘R’插入,但是这样做会使得终点随着新的元素的插入而产生变化,所

Traceback (most recent call last):     //以无法完成此要求。

File "", line 1, in

numbers[1:7:2]=[‘R‘]

ValueError: attempt to assign sequence of size 1 to extended slice of size 3

>>>

index

>>> #index用于从列表中找出某个值第一个匹配项的索引位置

>>> a=[‘wo‘,‘he‘,‘ni‘,‘hai‘,‘you‘,‘ta‘]

>>> a.index(‘wo‘)

0

>>> a.index(‘ji‘)                                      //调用index函数没有找到,返回错误

Traceback (most recent call last):

File "", line 1, in

a.index(‘ji‘)

ValueError: ‘ji‘ is not in list

>>>

pop

>>> #pop移除列表中的一个元素(默认是最后一个)

>>> x=[1,2,3]

>>> x.pop()

3

>>> x

[1, 2]

>>> x.pop(0)

1

>>> x

[2]

>>> x=[1,2,3,4,5,6,7,8,9]

>>> x.pop([1:8:2])

SyntaxError: invalid syntax

remove

>>> #remove 用于移除列表中某个值的第一个匹配项

>>> a=[‘to‘,‘be‘,‘or‘,‘not‘,‘to‘,‘be‘,‘is‘,‘a‘,‘question‘]

>>> a.remove(‘be‘)

>>> a

[‘to‘, ‘or‘, ‘not‘, ‘to‘, ‘be‘, ‘is‘, ‘a‘, ‘question‘]

sort

>>> #sort用于在原位置对列表进行排序,在原位置排序意味着改变原来的列表,从而让其中的元素能按照一定的顺序排列,而不是简单的返回一个以排序的列表副本

>>> x=[1,2,3]

>>> x.sort()

>>> x

[1, 2, 3]

>>> x=[5,9,4]

>>> x.sort()

>>> x

[4, 5, 9]

>>> sort(5,9,8)

sorted             //是函数,直接调用,不用.调用

>>> x=[7,8,2,9,4,0,9,8,6]

>>> y=x[:]

>>> y

[7, 8, 2, 9, 4, 0, 9, 8, 6]

>>> y.sort()

>>> y

[0, 2, 4, 6, 7, 8, 8, 9, 9]

>>> y=sorted(x)

>>> y

[0, 2, 4, 6, 7, 8, 8, 9, 9]

>>> sorted(‘python‘)

[‘h‘, ‘n‘, ‘o‘, ‘p‘, ‘t‘, ‘y‘]

>>> sorted(‘x‘,‘bd‘,‘c‘)

Traceback (most recent call last):

File "", line 1, in

sorted(‘x‘,‘bd‘,‘c‘)

TypeError: ‘str‘ object is not callable

>>> sorted(‘d‘,‘a‘,‘c‘)

Traceback (most recent call last):

File "", line 1, in

sorted(‘d‘,‘a‘,‘c‘)

TypeError: ‘str‘ object is not callable

>>>

reverse                                 //与sort刚好相反

>>> #reverse将列表中的元素反向存放

>>> x=[3,2,1]

>>> x.reverse()

>>> x

[1, 2, 3]

>>>

10.高级排序

cmp,可类比C语言字符串函数中的strcmp函数

>>> cmp(42,39)

1

>>> cmp(‘a‘,‘z‘)

-1

>>> cmp(‘ios‘,‘ump‘)

-1

>>> numbers=[8,2,9,7]

>>> numbers.sort(cmp)

>>> numbers

[2, 7, 8, 9]

>>>

11.元组:不可变序列

>>> 1,2,3

(1, 2, 3)

>>> (1,2,3)

(1, 2, 3)

>>> (42.)

42.0

>>> (42,)

(42,)

>>> 6+(40.)

46.0

>>> (42/8)*3

15

>>> 3*(42)

126

>>> 3*(40+2.)

126.0

>>> 3*(40+2,)

(42, 42, 42)

>>>

tuple

此函数功能是将一个序列转变为元组

>>> tuple([1,2,3])

(1, 2, 3)

>>> tuple(‘abc‘)

(‘a‘, ‘b‘, ‘c‘)

>>> tuple((‘uji‘))

(‘u‘, ‘j‘, ‘i‘)

>>>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值