catalogue
- I String str and list
- II String str and dictionary dict
- III list and dictionary dict
- IV Guess you like it
Zero basic Python learning route recommendation: Python learning directory >> Getting started with Python Basics
I String str and list
1. String to list
String to List list , you can use the str.split() method. The split method slices the specified characters in the string and returns a list. The example code is as follows:
# !usr/bin/env python # !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:Ape programming @Blog(Personal blog address): www.codersrc.com @File:Python character string/list/tuple/Mutual conversion between Dictionaries.py @Time:2021/3/23 07:37 @Motto:No small steps lead to thousands of miles. No small streams lead to rivers and seas. The brilliance of program life needs to be accumulated unremittingly! """ str1 = "hello word Ape theory python python course" print(str1) # Output string print(type(str1)) # Output data type: print(len(str1)) # Output string length print("***"*20) # Knock on the door: print 60 directly* #Slice according to space list1 = str1.split(" ") # Slice the space (') in the string. The return value is a list and assigned to list1 print(list1) # Output list data print(type(list1)) # Output data type: print(len(list1)) # Output list length (number of data in the list) print("***"*20) # Knock on the door: print 60 directly* #Slice according to character 'p' list1 = str1.split("p") # Slice the 'p' in the string. The return value is a list and assigned to list1 print(list1) # Output list data print(type(list1)) # Output data type: print(len(list1)) # Output list length (number of data in the list) print("***"*20) # Knock on the door: print 60 directly* #Slice according to character 'o' list1 = str1.split("o") # Slice the 'o' in the string. The return value is a list and assigned to list1 print(list1) # Output list data print(type(list1)) # Output data type: print(len(list1)) # Output list length (number of data in the list) ''' Output result: hello word Ape theory python python course <class 'str'> 28 ************************************************************ ['hello', 'word', 'Ape theory python', 'python course'] <class 'list'> 4 ************************************************************ ['hello word Ape theory', 'ython ', 'ython course'] <class 'list'> 3 ************************************************************ ['hell', ' w', 'rd Ape theory pyth', 'n pyth', 'n course'] <class 'list'> 5 '''
2. List to string
List to character string A * * is required join() * * method, which can directly convert the list into a string. The example code is as follows:
# !usr/bin/env python # !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:Ape programming @Blog(Personal blog address): www.codersrc.com @File:Python character string/list/tuple/Mutual conversion between Dictionaries.py @Time:2021/3/23 07:37 @Motto:No small steps can lead to thousands of miles, no small streams can lead to rivers and seas. The brilliance of program life needs to be accumulated unremittingly! """ list1 = ["hello", "word", "Ape theory python", "python course"] print(list1) # Output string print(type(list1)) # Output data type: print(len(list1)) # Output string length print("***"*20) # Knock on the door: print 60 directly* #Slice according to space str1 = "".join(list1) # Slice the space (') in the string. The return value is a list and assigned to list1 print(str1) # Output list data print(type(str1)) # Output data type: print(len(str1)) # Output list length (number of data in the list) ''' Output result: ['Ape theory python', 'word', 'python course', 'hello'] <class 'list'> 4 ************************************************************ Ape theory pythonwordpython course hello <class 'str'> 25 '''
II String str and dictionary dict
1. String to dictionary
Convert string to Dictionaries Can pass Built in function Eval() is completed. The use of the built-in function eval() will be explained in detail in later articles. Let's have a brief understanding today:
# !usr/bin/env python # !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:Ape programming @Blog(Personal blog address): www.codersrc.com @File:Python character string/list/tuple/Mutual conversion between Dictionaries.py @Time:2021/3/23 07:37 @Motto:No small steps lead to thousands of miles. No small streams lead to rivers and seas. The brilliance of program life needs to be accumulated unremittingly! """ # Pay attention to the combination of single quotation marks and double quotation marks str1 = '{"name":"zhangsan","age":18,"sing_dog":False }' print(str1) print(type(str1)) print(len(str1)) print("***"*20) # Knock on the door: print 60 directly* dict1 = eval(str1) # Force string str to dictionary dict print(dict1) print(type(dict1)) print(len(dict1)) ''' Output result: {"name":"zhangsan","age":18,"sing_dog":False } <class 'str'> 46 ************************************************************ {'name': 'zhangsan', 'age': 18, 'sing_dog': False} <class 'dict'> 3 '''
2. Dictionary to string
The dictionary can be converted into a string directly through * * str() * * type coercion. The example code is as follows:
# !usr/bin/env python # !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:Ape programming @Blog(Personal blog address): www.codersrc.com @File:Python character string/list/tuple/Mutual conversion between Dictionaries.py @Time:2021/3/23 07:37 @Motto:No small steps lead to thousands of miles. No small streams lead to rivers and seas. The brilliance of program life needs to be accumulated unremittingly! """ dict1 = {"name":"zhangsan","age":18,"sing_dog":False } print(dict1) print(type(dict1)) print(len(dict1)) print("***"*20) # Knock on the door: print 60 directly* str1 = str(dict1) # Force dictionary dict to string str print(str1) print(type(str1)) print(len(str1)) ''' Output result: {'name': 'zhangsan', 'age': 18, 'sing_dog': False} <class 'dict'> 3 ************************************************************ {'name': 'zhangsan', 'age': 18, 'sing_dog': False} <class 'str'> 50 '''
III list and dictionary dict
1. List to dictionary
List to Dictionaries It cannot be forced through dict(), but it can be completed through the built-in function * * zip() * * with the specific code as follows:
# !usr/bin/env python # !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:Ape programming @Blog(Personal blog address): www.codersrc.com @File:Python character string/list/tuple/Mutual conversion between Dictionaries.py @Time:2021/3/23 07:37 @Motto:No small steps lead to thousands of miles. No small streams lead to rivers and seas. The brilliance of program life needs to be accumulated unremittingly! """ list1 = ["hello", "word", "Ape theory python", "python course"] list2 = ["a","b","c","d","e","f","g"] dict1 = dict(zip(list1,list2)) print(dict1) print(type(dict1)) print(len(dict1)) ''' Output result: {'hello': 'a', 'word': 'b', 'Ape theory python': 'c', 'python course': 'd'} <class 'dict'> 4 '''
Note: the built-in function zip combines the data of two lists to form a key value pair to form a dictionary; If the length of the two lists is inconsistent, the extra elements will not be displayed when there are no matching elements in the other list.
2. Dictionary transfer list
You can forcibly convert the key or value in the dictionary into a list through the * * list() * * method. The example code is as follows:
# !usr/bin/env python # !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:Ape programming @Blog(Personal blog address): www.codersrc.com @File:Python character string/list/tuple/Mutual conversion between Dictionaries.py @Time:2021/3/23 07:37 @Motto:No small steps lead to thousands of miles. No small streams lead to rivers and seas. The brilliance of program life needs to be accumulated unremittingly! """ dict1 = {"name":"zhangsan","age":18,"sing_dog":False } # Force keys in dictionary dict to be converted into a list list1= list(dict1.keys()) print(list1) print(type(list1)) print(len(list1)) print("***"*20) # Knock on the door: print 60 directly* # Force values in dictionary dict to be converted into a list list2 = list(dict1.values()) print(list2) print(type(list2)) print(len(list2)) ''' Output result: ['name', 'age', 'sing_dog'] <class 'list'> 3 ************************************************************ ['zhangsan', 18, False] <class 'list'> 3 '''
IV Guess you like it
- Introduction to Python
- Python pycham anacanda differences
- Python2.x and python 3 x. How to choose?
- Python configuration environment
- Getting started with Python Hello World
- Python code comments
- Python Chinese code
- What is Anaconda? Anconda download installation tutorial
- Pychart prompt: this license **** has been cancelled
- Pychart set development template / font size / background color
- Python list
- Python tuple
- Python dictionary dict
No reprint without permission: Ape programming ยป Conversion between Python Strings / lists / tuples / Dictionaries