美文网首页
python基础 - list和tuple

python基础 - list和tuple

作者: 天才老熊猫 | 来源:发表于2017-11-17 16:15 被阅读10次

list

list 是python内置的一种数据类型。是一种可变的对象,有序的集合,可以随时添加和删除其中的元素

常见方法

# clear
>>> L = [1,2,3,4]
>>> L.clear()
>>> L
[]

#count
>>> L.count(1)
0
>>> L=['a','b','c','d']
>>> L.count('a')
1

#copy
>>> a = L.copy()
>>> a
['a', 'b', 'c', 'd']

#extend
>>> a=[1,2,5,3]
>>> b=[11,12,13,14]
>>> a.extend(b)
>>> a
[1, 2, 5, 3, 11, 12, 13, 14]

# index
>>> a
['a', 'Hello', 'world']
>>> a.index('l')
Traceback (most recent call last):
  File "<pyshell#84>", line 1, in <module>
    a.index('l')
ValueError: 'l' is not in list
>>> a.index('a')
0

# insert
>>> a.insert(0,'b')
>>> a
['b', 'a', 'Hello', 'world']

#pop
>>> a.pop(1)
'a'
>>> a
['b', 'Hello', 'world']

#remove
>>> a.remove('b')
>>> a
['Hello', 'world']

#reverse
>>> a.reverse()
>>> a
['world', 'Hello']

# sort
>>> a=[1,7,2,8,3,9,4,77,34]
>>> a.sort()
>>> a
[1, 2, 3, 4, 7, 8, 9, 34, 77]

tuple

tuple 和list非常的类似,但是tuple一旦初始化就不能修改。也没有类似list的方法

相关文章

  • Python基础知识之tuple

    python基础知识之元组tuple 1. 多元素tulpe的创建 tuple 和 list 非常类似,但是,tu...

  • python入门套路

    Python基础 基础数据类型 bool string list tuple dictionary 基础函数 he...

  • Python知识库

    Python基础语法 1、Python概述2、Python数据类型3、List和tuple4、分支和循环5、Dic...

  • 【原】Python学习笔记——基础篇

    List和Tuple类型 List用[],Tuple用() , Python 规定,单元素 tuple 要多加一个...

  • python基础 - list和tuple

    list list 是python内置的一种数据类型。是一种可变的对象,有序的集合,可以随时添加和删除其中的元素 ...

  • python 基础

    python 基础 tuple list append insert pop set add remove dic...

  • 2019-05-21

    今天复习了一下Python基础。 1. List 和 Tuple List: L = [1,2,3,4,5,6],...

  • 8.Python编程:tuple的指向不变性

    list 和 tuple 在python 中list和tuple的最大的区别要分清:list是有序可变的列表,tu...

  • Python 基础语法

    基础数据结构及语法 序列 sequence 元组(tuple) 和 表(list) tuple和list的主要区别...

  • 4_list (列表)和tuple(元组)

    list (列表)和tuple(元组) list和tuple是Python内置的有序集合,一个可变,一个不可变。根...

网友评论

      本文标题:python基础 - list和tuple

      本文链接:https://www.haomeiwen.com/subject/ybejvxtx.html