最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

關(guān)于Python 實(shí)現(xiàn)tuple和list的轉(zhuǎn)換問(wèn)題

 更新時(shí)間:2023年05月16日 11:28:54   作者:山茶花開時(shí)  
這篇文章主要介紹了Python 實(shí)現(xiàn)tuple和list的轉(zhuǎn)換,文中介紹了list(列表)和tuple(元組)共同點(diǎn)和區(qū)別,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

Python 實(shí)現(xiàn)tuple和list的轉(zhuǎn)換

1.list列表轉(zhuǎn)換為tuple元組

temp_list = [1,2,3,4,5]
print(temp_list)  # [1, 2, 3, 4, 5]
print(type(temp_list))  # <class 'list'>
# 將temp_list進(jìn)行強(qiáng)制轉(zhuǎn)換
a = tuple(temp_list)    
print(a)  # (1, 2, 3, 4, 5)
print(type(a))  # <class 'tuple'>

2.tuple元組轉(zhuǎn)換為list列表

temp_list = [1,2,3,4,5]
print(temp_list)  # [1, 2, 3, 4, 5]
print(type(temp_list))  # <class 'list'>
# 將temp_list進(jìn)行強(qiáng)制轉(zhuǎn)換
a = tuple(temp_list)    
print(a)  # (1, 2, 3, 4, 5)
print(type(a))  # <class 'tuple'>

Python中的list和tuple

一.list(列表)和tuple(元組)共同點(diǎn)和區(qū)別

共同點(diǎn):都是一種序列的形式,可以儲(chǔ)存不同類型的數(shù)據(jù)

區(qū)別:1.列表是動(dòng)態(tài)數(shù)組,它們可變且可以重設(shè)長(zhǎng)度(改變其內(nèi)部元素的個(gè)數(shù))。

        2. 元組是靜態(tài)數(shù)組,它們不可變,且其內(nèi)部數(shù)據(jù)一旦創(chuàng)建便無(wú)法改變。

二.定義一個(gè)變量,包含現(xiàn)在所學(xué)的數(shù)據(jù)類型

ist_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'>

三.目前學(xué)到的序列有哪些?

字符串str;字節(jié)bytes;元組tuple;列表list

1.將除tuple之外的序列轉(zhuǎn)換為tuple

str_data = '12'
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))

輸出:

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

2.將除list之外的序列轉(zhuǎn)換為list

str_data = '12'
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))

輸出:

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

四.tuple中有哪些操作方法

count(self, value, /)
      Return number of occurrences of value.
      #統(tǒng)計(jì)出現(xiàn)的次數(shù)
 index(self, value, start=0, stop=9223372036854775807, /)
      Return first index of value.

     #返回第一個(gè)的索引(索引:從0開始)

輸入:

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

輸出

2
2

元組是固定且不可改變的。這意味著一旦元組被創(chuàng)建,和列表不同,它的內(nèi)容無(wú)法被修改或它的大小也無(wú)法被改變。

>>> 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. """
    # 將對(duì)象追加到列表的末尾。
def clear(self, *args, **kwargs):
    """ Remove all items from list. """
    # 從列表中刪除所有項(xiàng)目。
def copy(self, *args, **kwargs):
    """ Return a shallow copy of the list. """
    # 返回列表的淺層副本。
def count(self, *args, **kwargs):
    """ Return number of occurrences of value. """
    # 返回值的出現(xiàn)次數(shù)。
def extend(self, *args, **kwargs):
    """ Extend list by appending elements from the iterable. """
    # 通過(guò)從可迭代對(duì)象追加元素來(lái)擴(kuò)展列表。
def index(self, *args, **kwargs):
    """
    Return first index of value.
    # 返回值的第一個(gè)索引。
    Raises ValueError if the value is not present.
    """
    # 如果值不存在,則提高值錯(cuò)誤。
def insert(self, *args, **kwargs):
    """ Insert object before index. """
    # 在索引之前插入對(duì)象。
def pop(self, *args, **kwargs):
    """
    Remove and return item at index (default last).
    # 刪除并返回索引處的項(xiàng)目(默認(rèn)在后面)。
    Raises IndexError if list is empty or index is out of range.
    """
    # 如果列表為空或索引超出范圍,則引發(fā)索引錯(cuò)誤。
def remove(self, *args, **kwargs):
    """
    Remove first occurrence of value.
    # 刪除第一個(gè)出現(xiàn)的值。
    Raises ValueError if the value is not present.
    """
    # 如果值不存在,則提高值錯(cuò)誤。
def reverse(self, *args, **kwargs):
    """ Reverse *IN PLACE*. """
    # 反向
def sort(self, *args, **kwargs):
    """
    Sort the list in ascending order and return None.
    # 按升序?qū)α斜磉M(jìn)行排序,并返回 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).
    # 排序是就地的(即列表本身被修改)和穩(wěn)定的(即保持兩個(gè)相等元素的順序)。
    If a key function is given, apply it once to each list item and sort them,
    ascending or descending, according to their function values.
    # 如果給出了一個(gè)關(guān)鍵功能,則將其應(yīng)用于每個(gè)列表項(xiàng)一次并對(duì)其進(jìn)行排序,
       升序或降序,根據(jù)其函數(shù)值。
    The reverse flag can be set to sort in descending order.

到此這篇關(guān)于Python 實(shí)現(xiàn)tuple和list的轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)python tuple和list轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

木兰县| 荔波县| 山西省| 全椒县| 长白| 许昌县| 岑溪市| 合水县| 海城市| 濮阳县| 舞钢市| 勐海县| 长沙县| 准格尔旗| 尼勒克县| 塘沽区| 西城区| 乐东| 井陉县| 独山县| 穆棱市| 富阳市| 榕江县| 牡丹江市| 区。| 萍乡市| 大同市| 青阳县| 余庆县| 南靖县| 蒲城县| 磐安县| 化州市| 沂水县| 耿马| 犍为县| 克山县| 惠来县| 上杭县| 平邑县| 兴山县|