欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

Python 實現(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進行強制轉(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進行強制轉(zhuǎn)換
a = tuple(temp_list)    
print(a)  # (1, 2, 3, 4, 5)
print(type(a))  # <class 'tuple'>

Python中的list和tuple

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

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

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

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

二.定義一個變量,包含現(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)計出現(xiàn)的次數(shù)
 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

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

>>> 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. """
    # 返回值的出現(xiàn)次數(shù)。
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.
    """
    # 如果列表為空或索引超出范圍,則引發(fā)索引錯誤。
def remove(self, *args, **kwargs):
    """
    Remove first occurrence of value.
    # 刪除第一個出現(xiàn)的值。
    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.
    # 按升序?qū)α斜磉M行排序,并返回 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)定的(即保持兩個相等元素的順序)。
    If a key function is given, apply it once to each list item and sort them,
    ascending or descending, according to their function values.
    # 如果給出了一個關(guān)鍵功能,則將其應(yīng)用于每個列表項一次并對其進行排序,
       升序或降序,根據(jù)其函數(shù)值。
    The reverse flag can be set to sort in descending order.

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

相關(guān)文章

  • 詳解Python如何利用Pandas與NumPy進行數(shù)據(jù)清洗

    詳解Python如何利用Pandas與NumPy進行數(shù)據(jù)清洗

    許多數(shù)據(jù)科學(xué)家認為獲取和清理數(shù)據(jù)的初始步驟占工作的 80%,花費大量時間來清理數(shù)據(jù)集并將它們歸結(jié)為可以使用的形式。本文將利用 Python 的 Pandas和 NumPy 庫來清理數(shù)據(jù),需要的可以參考一下
    2022-04-04
  • Python更新數(shù)據(jù)庫腳本兩種方法及對比介紹

    Python更新數(shù)據(jù)庫腳本兩種方法及對比介紹

    這篇文章給大家介紹了Python更新數(shù)據(jù)庫腳本兩種方法及數(shù)據(jù)庫查詢?nèi)N方式,然后在文章下面給大家介紹了兩種方式對比介紹,非常不錯,感興趣的朋友參考下吧
    2017-07-07
  • Python 語言實現(xiàn)六大查找算法

    Python 語言實現(xiàn)六大查找算法

    本文給大家分享Python 語言實現(xiàn)六大查找算法,針對每種算法通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2021-06-06
  • Python爬蟲之爬取二手房信息

    Python爬蟲之爬取二手房信息

    這篇文章主要介紹了Python爬蟲之爬取二手房信息,文中有非常詳細的代碼示例,對正在學(xué)習(xí)python爬蟲的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • python?使用ctypes調(diào)用C/C++?dll詳情

    python?使用ctypes調(diào)用C/C++?dll詳情

    這篇文章主要介紹了python?使用ctypes調(diào)用C/C++?dll詳情,文章首先通過導(dǎo)入ctypes模塊,加載C/C++?dll到python進程空間展開主題相關(guān)內(nèi)容,需要的小伙伴可以參考一下
    2022-04-04
  • Matlab中的mat數(shù)據(jù)轉(zhuǎn)成python中使用的npy數(shù)據(jù)遇到的坑及解決

    Matlab中的mat數(shù)據(jù)轉(zhuǎn)成python中使用的npy數(shù)據(jù)遇到的坑及解決

    這篇文章主要介紹了Matlab中的mat數(shù)據(jù)轉(zhuǎn)成python中使用的npy數(shù)據(jù)遇到的坑及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • python變量數(shù)據(jù)類型和運算符

    python變量數(shù)據(jù)類型和運算符

    這篇文章主要介紹了python變量數(shù)據(jù)類型和運算符,不同類型的變量可以進行的運算是不同的,所以必須理解變量的類型,下面文章的更多相關(guān)內(nèi)容介紹,需要的小伙伴可以參考一下
    2022-07-07
  • Python中os和shutil模塊實用方法集錦

    Python中os和shutil模塊實用方法集錦

    這篇文章主要介紹了Python中os和shutil模塊實用方法集錦,需要的朋友可以參考下
    2014-05-05
  • Python爬蟲動態(tài)IP代理使用及防止被封的方法

    Python爬蟲動態(tài)IP代理使用及防止被封的方法

    在進行網(wǎng)絡(luò)爬蟲時,經(jīng)常會遇到網(wǎng)站的反爬機制,其中之一就是通過IP封禁來限制爬蟲的訪問,為了規(guī)避這種限制,使用動態(tài)IP代理是一種有效的方法,本文將介紹在Python爬蟲中如何使用動態(tài)IP代理,以及一些防止被封的方法,文中有詳細的代碼講解,需要的朋友可以參考下
    2023-11-11
  • 基于PyTorch實現(xiàn)EdgeCNN的實戰(zhàn)教程

    基于PyTorch實現(xiàn)EdgeCNN的實戰(zhàn)教程

    本文我們將使用PyTorch來簡易實現(xiàn)一個EdgeCNN,不使用PyG庫,讓新手可以理解如何PyTorch來搭建一個簡易的圖網(wǎng)絡(luò)實例demo,感興趣的朋友跟隨小編一起看看吧
    2023-02-02

最新評論