Python基本類型的連接組合和互相轉(zhuǎn)換方式(13種)
本篇總結(jié)了一下字符串,列表,字典,元組的連接組合使用和類型的互相轉(zhuǎn)換小例子,尤其列表中的extend()方法和字典中的

update方法非常的常用。
1.連接兩個(gè)字符串
a = "hello " b = "world" a += b print(a) # hello world
2.字典的連接
dict1 = {1: "a", 2: "b"}
dict2 = {3: "c", 4: "d"}
dict1.update(dict2)
print(dict1) # {1: 'a', 2: 'b', 3: 'c', 4: 'd'}
3.列表的連接
list1 = [1, 2, 3] list2 = [4, 5, 6] list1.extend(list2) # [1, 2, 3, 4, 5, 6] print(list1)
4.元組的連接
tuple1 = (1, 2) tuple2 = (3, 4) tuple1 += tuple2 print(tuple1) # (1, 2, 3, 4)
5.字典轉(zhuǎn)換為字符串
dict1 = {1: "a", 2: "b"}
str1 = str(dict1)
print(str1) # {1: 'a', 2: 'b'}
print(type(str1)) # <class 'str'>
6.字典轉(zhuǎn)換為列表
dict1 = {1: "a", 2: "b"}
list1 = list(dict1.keys())
list2 = list(dict1.values())
list3 = list(dict1)
print(list1) # [1, 2]
print(list2) # ['a', 'b']
print(list3) # [1,2]
7.字典轉(zhuǎn)換為元組
dict1 = {1: "a", 2: "b"}
tuple1 = tuple(dict1.keys())
tuple2 = tuple(dict1.values())
tuple3 = tuple(dict1)
print(tuple1) # (1, 2)
print(tuple2) # ('a', 'b')
print(tuple3) # (1, 2)
8.列表轉(zhuǎn)換為字符串
list1 = [1, 2, 3] str1 = str(list1) print(str1) # [1, 2, 3] print(type(str1)) # <class 'str'>
9.列表轉(zhuǎn)換為字典
# 1.
list1 = [1, 2, 3]
list2 = ["a", "b", "c"]
dict1 = dict(zip(list1, list2))
print(dict1) # {1: 'a', 2: 'b', 3: 'c'}
# 2.
dict1 = {}
for i in list1:
dict1[i] = list2[list1.index(i)]
print(dict1) # {1: 'a', 2: 'b', 3: 'c'}
# 3.
list1 = [[1, 'a'], [2, 'b'], [3, 'c']]
dict1 = dict(list1)
print(dict1) # {1: 'a', 2: 'b', 3: 'c'}
10.列表轉(zhuǎn)換為元組
list1 = [1, 2, 3] tuple1 = tuple(list1) print(tuple1) # (1, 2, 3)
11.元組轉(zhuǎn)換為字符串
tuple1 = (1, 2, 3) str1 = tuple(tuple1) print(str1) # (1, 2, 3) print(type(str1)) # <class 'tuple'>
12.元組轉(zhuǎn)換為字典
# 1.
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
dict1 = dict(zip(tuple1, tuple2))
print(dict1) # {1: 4, 2: 5, 3: 6}
# 2
dict1 = {}
for i in tuple1:
dict1[i] = tuple2[tuple1.index(i)]
print(dict1) # {1: 4, 2: 5, 3: 6}
# 3
tuple1 = (1, 2)
tuple2 = (4, 5)
tuple3 = (tuple1, tuple2)
dict1 = dict(tuple3)
print(dict1) # {1: 2, 4: 5}
13.元組轉(zhuǎn)換為列表
tuple1 = (1, 2) list1 = list(tuple1) print(list1) # [1, 2]
總結(jié)
以上所述是小編給大家介紹的Python基本類型的連接組合和互相轉(zhuǎn)換方式,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- python opencv 圖像邊框(填充)添加及圖像混合的實(shí)現(xiàn)方法(末尾實(shí)現(xiàn)類似幻燈片漸變的效果)
- python實(shí)現(xiàn)向ppt文件里插入新幻燈片頁(yè)面的方法
- Python面向?qū)ο蟪绦蛟O(shè)計(jì)OOP深入分析【構(gòu)造函數(shù),組合類,工具類等】
- Python面向?qū)ο箢惱^承和組合實(shí)例分析
- Python 如何查找特定類型文件
- 解析python 類方法、對(duì)象方法、靜態(tài)方法
- 詳解python metaclass(元類)
- python字典key不能是可以是啥類型
- Python通過(guò)類的組合模擬街道紅綠燈
相關(guān)文章
簡(jiǎn)單談?wù)凱ython中函數(shù)的可變參數(shù)
和C語(yǔ)言一樣,Python中也有可變參數(shù)函數(shù),即一個(gè)函數(shù)可以接收多個(gè)參數(shù),而這些參數(shù)的個(gè)數(shù)在函數(shù)調(diào)用之前事先是不知道的。下面這篇文章我們來(lái)介紹下python中的可變參數(shù)2016-09-09
Python運(yùn)維開(kāi)發(fā)之psutil庫(kù)的使用詳解
這篇文章主要介紹了Python運(yùn)維開(kāi)發(fā)之psutil庫(kù)的使用,psutil能夠輕松實(shí)現(xiàn)獲取系統(tǒng)運(yùn)行的進(jìn)程和系統(tǒng)利用率。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
詳解Python 實(shí)現(xiàn) ZeroMQ 的三種基本工作模式
ZMQ是一個(gè)簡(jiǎn)單好用的傳輸層,像框架一樣的一個(gè) socket library,他使得 Socket 編程更加簡(jiǎn)單、簡(jiǎn)潔和性能更高。 ,這篇文章主要介紹了Python 實(shí)現(xiàn) ZeroMQ 的三種基本工作模式,需要的朋友可以參考下2020-03-03
python 寫(xiě)的一個(gè)爬蟲(chóng)程序源碼
這篇文章主要介紹了python 寫(xiě)的一個(gè)爬蟲(chóng)程序源碼,需要的朋友可以參考下2016-02-02
Python?cv.Canny()方法參數(shù)與使用方法
這篇文章主要介紹了Python?cv.Canny()方法參數(shù)與使用方法,OpenCV提供了cv.Canny()方法,該方法將輸入的原始圖像轉(zhuǎn)換為邊緣圖像,更多相關(guān)內(nèi)容需要的朋友可以參考一下2022-07-07

