目錄
- 一.字串 str 與串列 list
- 1.字串轉串列
- 2.串列轉字串
- 二.字串 str 與字典 dict
- 1.字串轉字典
- 2.字典轉字串
- 三.串列 list 與字典 dict
- 1.串列轉字典
- 2.字典轉串列
- 四.猜你喜歡
零基礎 Python 學習路線推薦 : Python 學習目錄 >> Python 基礎入門
一.字串 str 與串列 list
1.字串轉串列
字串轉為串列 list ,可以使用 str.split() 方法, split 方法是在字串中對指定字符進行切片,并回傳一個串列,示例代碼如下:
# !usr/bin/env python
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python字串/串列/元組/字典之間的相互轉換.py
@Time:2021/3/23 07:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!
"""
str1 = "hello word 猿說python python教程"
print(str1) # 輸出字串
print(type(str1)) # 輸出資料型別:
print(len(str1)) # 輸出字串長度
print("***"*20) # 小敲門:直接列印60個*
#根據空格切片
list1 = str1.split(" ") # 對字串中的空格(' ')進行切片,回傳值是一個串列list并賦值給list1
print(list1) # 輸出串列資料
print(type(list1)) # 輸出資料型別:型別
print(len(list1)) # 輸出串列長度(串列的資料個數)
print("***"*20) # 小敲門:直接列印60個*
#根據字符'p'切片
list1 = str1.split("p") # 對字串中的'p'進行切片,回傳值是一個串列list并賦值給list1
print(list1) # 輸出串列資料
print(type(list1)) # 輸出資料型別:型別
print(len(list1)) # 輸出串列長度(串列的資料個數)
print("***"*20) # 小敲門:直接列印60個*
#根據字符'o'切片
list1 = str1.split("o") # 對字串中的'o'進行切片,回傳值是一個串列list并賦值給list1
print(list1) # 輸出串列資料
print(type(list1)) # 輸出資料型別:型別
print(len(list1)) # 輸出串列長度(串列的資料個數)
'''
輸出結果:
hello word 猿說python python教程
<class 'str'>
28
************************************************************
['hello', 'word', '猿說python', 'python教程']
<class 'list'>
4
************************************************************
['hello word 猿說', 'ython ', 'ython教程']
<class 'list'>
3
************************************************************
['hell', ' w', 'rd 猿說pyth', 'n pyth', 'n教程']
<class 'list'>
5
'''
2.串列轉字串
串列轉為字串需要使用”.join()方法,join() 方法可以直接將串列轉為一個字串,示例代碼如下:
# !usr/bin/env python
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python字串/串列/元組/字典之間的相互轉換.py
@Time:2021/3/23 07:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!
"""
list1 = ["hello", "word", "猿說python", "python教程"]
print(list1) # 輸出字串
print(type(list1)) # 輸出資料型別:
print(len(list1)) # 輸出字串長度
print("***"*20) # 小敲門:直接列印60個*
#根據空格切片
str1 = "".join(list1) # 對字串中的空格(' ')進行切片,回傳值是一個串列list并賦值給list1
print(str1) # 輸出串列資料
print(type(str1)) # 輸出資料型別:型別
print(len(str1)) # 輸出串列長度(串列的資料個數)
'''
輸出結果:
['猿說python', 'word', 'python教程', 'hello']
<class 'list'>
4
************************************************************
猿說pythonwordpython教程hello
<class 'str'>
25
'''
二.字串 str 與字典 dict
1.字串轉字典
將字串轉為字典可以通過內置函式 eval() 完成,對于內置函式 eval() 的使用,在后面的文章還會有詳細講解,今天先簡單了解一下:
# !usr/bin/env python
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python字串/串列/元組/字典之間的相互轉換.py
@Time:2021/3/23 07:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!
"""
# 注意單引號和雙引號的配合使用
str1 = '{"name":"zhangsan","age":18,"sing_dog":False }'
print(str1)
print(type(str1))
print(len(str1))
print("***"*20) # 小敲門:直接列印60個*
dict1 = eval(str1) # 強制將字串str轉為字典dict
print(dict1)
print(type(dict1))
print(len(dict1))
'''
輸出結果:
{"name":"zhangsan","age":18,"sing_dog":False }
<class 'str'>
46
************************************************************
{'name': 'zhangsan', 'age': 18, 'sing_dog': False}
<class 'dict'>
3
'''
2.字典轉字串
將字典轉為字串可以直接通過**str()**型別強制轉換即可,示例代碼如下:
# !usr/bin/env python
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python字串/串列/元組/字典之間的相互轉換.py
@Time:2021/3/23 07:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!
"""
dict1 = {"name":"zhangsan","age":18,"sing_dog":False }
print(dict1)
print(type(dict1))
print(len(dict1))
print("***"*20) # 小敲門:直接列印60個*
str1 = str(dict1) # 強制將字典dict轉為字串str
print(str1)
print(type(str1))
print(len(str1))
'''
輸出結果:
{'name': 'zhangsan', 'age': 18, 'sing_dog': False}
<class 'dict'>
3
************************************************************
{'name': 'zhangsan', 'age': 18, 'sing_dog': False}
<class 'str'>
50
'''
三.串列 list 與字典 dict
1.串列轉字典
串列轉為字典不能通過 dict()強制轉換,但是可以通過內置函式**zip()**完成,具體代碼如下:
# !usr/bin/env python
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python字串/串列/元組/字典之間的相互轉換.py
@Time:2021/3/23 07:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!
"""
list1 = ["hello", "word", "猿說python", "python教程"]
list2 = ["a","b","c","d","e","f","g"]
dict1 = dict(zip(list1,list2))
print(dict1)
print(type(dict1))
print(len(dict1))
'''
輸出結果:
{'hello': 'a', 'word': 'b', '猿說python': 'c', 'python教程': 'd'}
<class 'dict'>
4
'''
注意:內置函式 zip 是將兩個串列的資料兩兩組合形成鍵值對,構成字典;如果兩個串列的長度不一致時,多出的元素在另一個串列無匹配的元素時就不展示多出的元素,
2.字典轉串列
可以通過**list()**方法強制將字典中的 key 或者 value 轉為串列,示例代碼如下:
# !usr/bin/env python
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python字串/串列/元組/字典之間的相互轉換.py
@Time:2021/3/23 07:37
@Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!
"""
dict1 = {"name":"zhangsan","age":18,"sing_dog":False }
# 強制將字典dict中的keys轉為串列
list1= list(dict1.keys())
print(list1)
print(type(list1))
print(len(list1))
print("***"*20) # 小敲門:直接列印60個*
# 強制將字典dict中的values轉為串列
list2 = list(dict1.values())
print(list2)
print(type(list2))
print(len(list2))
'''
輸出結果:
['name', 'age', 'sing_dog']
<class 'list'>
3
************************************************************
['zhangsan', 18, False]
<class 'list'>
3
'''
四.猜你喜歡
- Python 簡介
- Python Pycharm Anacanda 區別
- Python2.x 和 Python3.x,如何選擇?
- Python 配置環境
- Python Hello World 入門
- Python 代碼注釋
- Python 中文編碼
- Anaconda 是什么?Anconda 下載安裝教程
- Pycharm 提示:this license **** has been cancelled
- Pycharm 設定開發模板/字體大小/背景顏色
- Python 串列 list
- Python 元組 tuple
- Python 字典 dict
未經允許不得轉載:猿說編程 ? Python 字串/串列/元組/字典之間的相互轉換
本文由博客 - 猿說編程 猿說編程 發布!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/287534.html
標籤:Python
