python列表和元组的区别

目录

一.list(列表)和tuple(元组)共同点和区别

二.定义一个变量,包含现在所学的数据类型

三.目前学到的序列有哪些?

1.将除tuple之外的序列转换为tuple

2.将除list之外的序列转换为list

四.tuple中有哪些操作方法

五.list中有哪些操作方法


一.list(列表)和tuple(元组)共同点和区别

共同点:都是一种序列的形式,可以储存不同类型的数据

区别:1.列表是动态数组,它们可变且可以重设长度(改变其内部元素的个数)。

        2. 元组是静态数组,它们不可变,且其内部数据一旦创建便无法改变。

二.定义一个变量,包含现在所学的数据类型

list_data = [1, 1.2, b'123', True, None, 1+2j, 'az', (6, 6, 6), [1, 2]]
print(list_data, type(list_data))

输出:

[1, 1.2, b'123', True, None, (1+2j), 'az', (6, 6, 6), [1, 2]] <class 'list'>

三.目前学到的序列有哪些?

字符串str;字节bytes;元组tuple;列表list

1.将除tuple之外的序列转换为tuple

str_data = 'az'
bytes_data = b'123'
list_data = [1, 2]
tuple_data1 = tuple(str_data)
tuple_data2 = tuple(bytes_data)
tuple_data3 = tuple(list_data)
print(tuple_data1, type(tuple_data1))
print(tuple_data2, type(tuple_data2))
print(tuple_data3, type(tuple_data3))

输出:

('a', 'z') <class 'tuple'>
(49, 50,51) <class 'tuple'>
(1, 2) <class 'tuple'>

2.将除list之外的序列转换为list

str_data = 'az'
bytes_data = b'123'
tuple_data = (1, 2)
list_data1 = list(str_data)
list_data2 = list(bytes_data)
list_data3 = list(tuple_data)
print(list_data1, type(list_data1))
print(list_data2, type(list_data2))
print(list_data3, type(list_data3))

输出:

['a', 'z'] <class 'list'>
[49,50,51] <class 'list'>
[1, 2] <class 'list'>

四.tuple中有哪些操作方法

count(self, value, /)
      Return number of occurrences of value.
      #统计出现的次数

 index(self, value, start=0, stop=9223372036854775807, /)
      Return first index of value.

     #返回第一个的索引(索引:从0开始)

输入:

tuple_data = tuple("hello")
print(tuple_data.count("l"))
print(tuple_data.index("l"))

输出

2
2

元组是固定且不可改变的。这意味着一旦元组被创建,和列表不同,它的内容无法被修改或它的大小也无法被改变。

>>> tuple_data = (1, 2, 3, 4)
>>> tuple_data[0] = 5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

五.list中有哪些操作方法

def append(self, *args, **kwargs):
    """ Append object to the end of the list. """
    # 将对象追加到列表的末尾。

def clear(self, *args, **kwargs):
    """ Remove all items from list. """
    # 从列表中删除所有项目。

def copy(self, *args, **kwargs):
    """ Return a shallow copy of the list. """
    # 返回列表的浅层副本。

def count(self, *args, **kwargs):
    """ Return number of occurrences of value. """
    # 返回值的出现次数。

def extend(self, *args, **kwargs):
    """ Extend list by appending elements from the iterable. """
    # 通过从可迭代对象追加元素来扩展列表。

def index(self, *args, **kwargs):
    """
    Return first index of value.
    # 返回值的第一个索引。

    Raises ValueError if the value is not present.
    """
    # 如果值不存在,则提高值错误。

def insert(self, *args, **kwargs):
    """ Insert object before index. """
    # 在索引之前插入对象。

def pop(self, *args, **kwargs):
    """
    Remove and return item at index (default last).
    # 删除并返回索引处的项目(默认在后面)。
    Raises IndexError if list is empty or index is out of range.
    """
    # 如果列表为空或索引超出范围,则引发索引错误。



def remove(self, *args, **kwargs):
    """
    Remove first occurrence of value.
    # 删除第一个出现的值。
    Raises ValueError if the value is not present.
    """
    # 如果值不存在,则提高值错误。

def reverse(self, *args, **kwargs):
    """ Reverse *IN PLACE*. """
    # 反向

def sort(self, *args, **kwargs):
    """
    Sort the list in ascending order and return None.
    # 按升序对列表进行排序,并返回 None。
    The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
    order of two equal elements is maintained).
    # 排序是就地的(即列表本身被修改)和稳定的(即保持两个相等元素的顺序)。
    If a key function is given, apply it once to each list item and sort them,
    ascending or descending, according to their function values.
    # 如果给出了一个关键功能,则将其应用于每个列表项一次并对其进行排序,
       升序或降序,根据其函数值。
    The reverse flag can be set to sort in descending order.
    """
    # 可以将反向标志设置为按降序排序。

来源:Fran_klin__

物联沃分享整理
物联沃-IOTWORD物联网 » python列表和元组的区别

发表评论