How to convert a Python string type list to a real list type
This article focuses on "how to convert the Python string type list into a real list type". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to convert a Python string type list into a real list type.
In the process of writing code, we often use the for loop to loop the list, so if we get a list of type str and for it, we will see the following code and figure:
Str_list = str (['axiajiajiajiaoyun']) for row in str_list: print (row)
Results:
So the for loop prints out each character of the str type list one by one, and this result is not what we want, so how to solve this problem? To use the third-party module, look at the following code
From ast import literal_eval# assumes that you get a list of str types str_list = str (['axiaxiajiaozhuo']) print (type (str_list)) # # through the function literal_eval Convert a list of type str to a real list type of type list new_list = literal_eval (str_list) print (type (new_list)) # # and then you can get every value in the list for row in new_list: print (row) # execution result''abc''' through the for loop
The literal function of ast will determine whether the content to be calculated is a legal python type after calculation. If so, the operation will be performed, otherwise the operation will not be performed.
At this point, I believe you have a deeper understanding of "how to convert a Python string type list into a real list type". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!