關(guān)于Python 實(shí)現(xiàn)tuple和list的轉(zhuǎn)換問題
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)建便無法改變。
二.定義一個(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)容無法被修改或它的大小也無法被改變。
>>> 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. """
# 通過從可迭代對(duì)象追加元素來擴(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)文章
python 應(yīng)用之Pycharm 新建模板默認(rèn)添加編碼格式-作者-時(shí)間等信息【推薦】
這篇文章主要介紹了Pycharm 新建模板默認(rèn)添加編碼格式-作者-時(shí)間等信息 ,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
python 使用get_argument獲取url query參數(shù)
這篇文章主要介紹了python 使用get_argument獲取url query參數(shù)的相關(guān)資料,需要的朋友可以參考下2017-04-04
Python中選擇結(jié)構(gòu)實(shí)例講解
在本篇文章里小編給大家整理了關(guān)于Python選擇結(jié)構(gòu)的基礎(chǔ)知識(shí)點(diǎn)及相關(guān)實(shí)例,有需要的朋友們可以學(xué)習(xí)參考下。2022-11-11
Python使用內(nèi)置函數(shù)setattr設(shè)置對(duì)象的屬性值
這篇文章主要介紹了Python使用內(nèi)置函數(shù)setattr設(shè)置對(duì)象的屬性值,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
Python Opencv實(shí)現(xiàn)圖像輪廓識(shí)別功能
這篇文章主要為大家詳細(xì)介紹了Python Opencv實(shí)現(xiàn)圖像輪廓識(shí)別功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
Python(Django)項(xiàng)目與Apache的管理交互的方法
這篇文章主要介紹了Python(Django)項(xiàng)目與Apache的管理交互的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
Python實(shí)現(xiàn)抓取頁面上鏈接的簡(jiǎn)單爬蟲分享
這篇文章主要介紹了Python實(shí)現(xiàn)抓取頁面上鏈接的簡(jiǎn)單爬蟲分享,本文使用了一個(gè)開源模塊requests實(shí)現(xiàn)需求,需要的朋友可以參考下2015-01-01

