一.字符串str與列表list
1.字符串轉列表
字符串轉爲列表list,可以使用str.split()方法,split方法是在字符串中對指定字符進行切片,並返回一個列表,示例代碼如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | # !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:何以解憂 @Blog(個人博客地址): shuopython.com @WeChat Official Account(微信公衆號):猿說python @Github:www.github.com
@File:python_data.py @Time:2019/9/20 20:45
@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)) # 輸出列表長度(列表的數據個數) |
輸出結果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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()方法可以直接將列表轉爲一個字符串,示例代碼如下:
1 2 3 4 5 6 7 8 9 10 11 | 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)) # 輸出列表長度(列表的數據個數) |
輸出結果:
1 2 3 4 5 6 7 | ['猿說python', 'word', 'python教程', 'hello'] <class 'list'> 4 ************************************************************ 猿說pythonwordpython教程hello <class 'str'> 25 |
二.字符串str與字典dict
1.字符串轉字典
將字符串轉爲字典可以通過內置函數eval()完成,對於內置函數eval()的使用,在後面的文章還會有詳細講解,今天先簡單瞭解一下:
1 2 3 4 5 6 7 8 9 10 11 12 | # 注意單引號和雙引號的配合使用 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)) |
輸出結果:
1 2 3 4 5 6 7 | {"name":"zhangsan","age":18,"sing_dog":False } <class 'str'> 46 ************************************************************ {'name': 'zhangsan', 'age': 18, 'sing_dog': False} <class 'dict'> 3 |
2.字典轉字符串
將字典轉爲字符串可以直接通過str()類型強制轉換即可,示例代碼如下:
1 2 3 4 5 6 7 8 9 10 11 | 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)) |
輸出結果:
1 2 3 4 5 6 7 | {'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()完成,具體代碼如下:
1 2 3 4 5 6 7 | 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)) |
輸出結果:
1 2 3 | {'hello': 'a', 'word': 'b', '猿說python': 'c', 'python教程': 'd'} <class 'dict'> 4 |
注意:內置函數zip 是將兩個列表的數據兩兩組合形成鍵值對,構成字典;如果兩個列表的長度不一致時,多出的元素在另一個列表無匹配的元素時就不展示多出的元素。
2.字典轉列表
可以通過list()方法強制將字典中的key 或者 value轉爲列表,示例代碼如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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)) |
輸出結果:
1 2 3 4 5 6 7 | ['name', 'age', 'sing_dog'] <class 'list'> 3 ************************************************************ ['zhangsan', 18, False] <class 'list'> 3 |
猜你喜歡:
轉載請註明:猿說Python » python 字符串(str)/列表(list)/元組(tuple)/字典(dict)之間的相互轉換