Python列表常用操作

Python的列表非常好用,一些常用的操作写在这里。


在Python中创建一个列表时,解释器会在内存中创建一个类似数组(但不是数组)的数据结构来存储数据。列表中的编号从 0 开始,然后是1,依此类推。

这里写图片描述



print() 显示列表;

len() 得出列表中有多少数据项;

append() 在列表末尾追加一个数据项;

extend() 在列表末尾增加一个数据项集合;

pop() 在列表末尾删除一个数据项;

remove() 在列表中删除一个特定的数据项;

insert() 在特定位置前面增加一个数据项;

count() 统计某个数据项在列表中出现的次数;

reverse() 反向列表中数据项。


>>> Fruit = ["Apple","Pear","Grape","Peach","Bananer"]
>>> print(len(Fruit))
5
>>> print(Fruit[0])
Apple
>>> print(Fruit[4])
Bananer

>>> Fruit.append("tomato")
>>> print(Fruit)
['Apple', 'Pear', 'Grape', 'Peach', 'Bananer', 'tomato']

>>> Fruit.extend(["lemon","coconut"])
>>> print(Fruit)
['Apple', 'Pear', 'Grape', 'Peach', 'Bananer', 'tomato', 'lemon', 'coconut']

>>> Fruit.pop()
'coconut'
>>> print(Fruit)
['Apple', 'Pear', 'Grape', 'Peach', 'Bananer', 'tomato', 'lemon']

>>> Fruit.remove("Peach")
>>> print(Fruit)
['Apple', 'Pear', 'Grape', 'Bananer', 'tomato', 'lemon']
>>> Fruit.remove(Fruit[0])
>>> print(Fruit)
['Pear', 'Grape', 'Bananer', 'tomato', 'lemon']

>>> Fruit.insert(0,"Apple")
>>> print(Fruit)
['Apple', 'Pear', 'Grape', 'Bananer', 'tomato', 'lemon']
>>> Fruit.insert(6,"coconut")
>>> print(Fruit)
['Apple', 'Pear', 'Grape', 'Bananer', 'tomato', 'lemon', 'coconut']

>>> Fruit.count("Apple")
1





用迭代显示列表中的数据项,以下的代码段中 forwhile 完成的工作是一样的:

>>> for item in Fruit:
    print(item)
>>> while count < len(Fruit):
      print(Fruit[count]) 
      count = count + 1
# 输出结果
Apple
1
Pear
2
Bananer
3


如果字符串中需要包含双引号,不要忘记转义 ,\”

>>> Fruit.append("\"Tomato\"")
>>> print(Fruit)
['Apple', 1, 'Pear', 2, 'Bananer', 3, '"Tomato"']


isinstance() 函数可以用来判断特定标识符是否包含某个特定类型的数据。

>>> isinstance(Fruit,list)
True

>>> isinstance(count,list)
False



Python中列表可以嵌套,并且可以支持任意多层的嵌套,例如:

>>> print(Fruit)
['Apple', 1, 'Pear', 2, 'Bananer', 3]

>>> Fruit.append(["A","B","C"])
>>> print(Fruit)
['Apple', 1, 'Pear', 2, 'Bananer', 3, '"Tomato"', ['A', 'B', 'C']]

>>> Fruit[-1].append(["aa","bb","cc"])
>>> print(Fruit)
['Apple', 1, 'Pear', 2, 'Bananer', 3, '"Tomato"', ['A', 'B', 'C', ['aa', 'bb', 'cc']]]
>>> for i in Fruit:
    print(i)

Apple
1
Pear
2
Bananer
3
"Tomato"
['A', 'B', 'C', ['aa', 'bb', 'cc']]

试试输出三层嵌套的列表中的各个数据项:

>>> for i in Fruit:
    if isinstance(i,list):
        for j in i:
            if isinstance(j,list):
                for k in j:
                    print(k)
            else:
                print(j)
    else:
        print(i)


Apple
1
Pear
2
Bananer
3
"Tomato"
A
B
C
aa
bb
cc



上面的循环嵌入的有点多,如果是N层嵌套的列表重复代码会很多,来点优化试试:

>>> def my_print(mylist):
    for i in mylist:
        if isinstance(i,list):
            my_print(i)
        else:
            print(i)

>>> my_print(Fruit)

Apple
1
Pear
2
Bananer
3
"Tomato"
A
B
C
aa
bb
cc

定义个递归函数实现,看起来好多了。



列表操作符部分,+表示列表组合,*表示列表重复:

>>> mylist = [1,2,3] + [4,5,6]
>>> print mylist
[1, 2, 3, 4, 5, 6]

>>> mylist = mylist*4
>>> print(mylist)
[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]


今天就写到这里吧。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值