str && list
string = 'I’m a str'
# 1. 整體轉換
lst = string.split('任何字符串中沒有的分割單位,不能為空')
print(lst)
# output : ['I’m a str']
# 2. 分割轉換
# 通過split默認屬性分割
lst = string.split()
print(lst)
# output : ['I’m', 'a', 'str']
# 3. 強制類型轉換
lst = list(string)
print(lst)
# output : ['I', '’', 'm', ' ', 'a', ' ', 's', 't', 'r']
lst = ['I’m', 'a', 'str']
# 1. 遍歷拼接
new_Str = lst[0] + lst[1] + lst[2]
print(new_Str)
# output : I’mastr
# 2. .join
print(''.join(lst))
print(','.join(lst))
# output : I’mastr
# output : I’m,a,str
str && dict
# 1. json
# 需要注意字符串中的json格式
# json 語法規定 數組或對象之中的字符串必須使用雙引號,不能使用單引號
string1 = "{'nonce': 1589174926391, 'number': '111222333', 'prefix': 86}"
string2 = '{"nonce": "1589174926391", "number": "111222333", "prefix": "86"}'
# dct = json.loads(string1)
# print(type(dct), ':', dct)
# error : json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
dct = json.loads(string2)
print(type(dct), ':', dct)
# output : <class 'dict'> : {'nonce': '1589174926391', 'number': '111222333', 'prefix': '86'}
# 2. eval(eval 存在安全性的問題)
dct = eval(string1)
print(type(dct), ':', dct)
# output : <class 'dict'> : {'nonce': 1589174926391, 'number': '111222333', 'prefix': 86}
dct = eval(string2)
print(type(dct), ':', dct)
# output : <class 'dict'> : {'nonce': '1589174926391', 'number': '111222333', 'prefix': '86'}
# 3. literal_eval(推薦)
import ast
dct = ast.literal_eval(string1)
print(type(dct), ':', dct)
# output : <class 'dict'> : {'nonce': 1589174926391, 'number': '111222333', 'prefix': 86}
dct = ast.literal_eval(string2)
print(type(dct), ':', dct)
# output : <class 'dict'> : {'nonce': '1589174926391', 'number': '111222333', 'prefix': '86'}
# 1. 強制類型轉換
dct1 = {'nonce': 1589174926391, 'number': '111222333', 'prefix': 86}
dct2 = {'nonce': '1589174926391', 'number': '111222333', 'prefix': '86'}
newString = str(dct1)
print(type(newString), ':', newString)
# output : <class 'str'> : {'nonce': '1589174926391', 'number': '111222333', 'prefix': '86'}
# 2. json.dumps()
newString = json.dumps(dct1)
print(type(newString), ':', newString)
# output : <class 'str'> : <class 'str'> : {"nonce": 1589174926391, "number": "111222333", "prefix": 86}
str && json
string1 = "{'nonce': 1589174926391, 'number': '111222333', 'prefix': 86}"
string2 = '{"nonce": "1589174926391", "number": "111222333", "prefix": "86"}'
# jsn = json.loads(string1)
# print(type(jsn), ':', jsn)
# error : json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
jsn = json.loads(string2)
print(type(jsn), ':', jsn)
# output : <class 'dict'> : {'nonce': '1589174926391', 'number': '111222333', 'prefix': '86'}
dct1 = {'nonce': 1589174926391, 'number': '111222333', 'prefix': 86}
string = json.dumps(dct1)
print(type(string), ':', string)
# output : <class 'str'> : {"nonce": "1589174926391", "number": "111222333", "prefix": "86"}
list && dict
# 1. zip()
lst1 = ['key1', 'key2', 'key3', 'key4']
lst2 = ['value1', 'value2', 'value3']
dct = dict(zip(lst1, lst2))
print(type(dct), ':', dct)
# output : <class 'dict'> : {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
# 2. 使用嵌套列表
lst1 = ['key1', 'value1']
lst2 = ['key2', 'value2']
lst3 = ['key3', 'value3']
lst4 = [lst1, lst2, lst3]
dct = dict(lst4)
print(type(dct), ':', dct)
# output : <class 'dict'> : {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
dct = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
lst = list(dct)
print(type(lst), ':', lst)
# output : <class 'list'> : ['key1', 'key2', 'key3']
lst = list(dct.keys())
print(type(lst), ':', lst)
# output : <class 'list'> : ['key1', 'key2', 'key3']
lst = list(dct.values())
print(type(lst), ':', lst)
# output : <class 'list'> : ['value1', 'value2', 'value3']
dict && json
dct = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
jsn = json.dumps(dct)
print(type(jsn), ':', jsn)
# output : <class 'str'> : {"key1": "value1", "key2": "value2", "key3": "value3"}
string = '{"key1": "value1", "key2": "value2", "key3": "value3"}'
dct = json.loads(string)
print(type(dct), ':', dct)
# output : <class 'dict'> : {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}