In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how python uses Split () to create a string list. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Use Split () to create a list of strings
We often use strings as identifiers for specific objects. For example, we can use a string as a key in a dictionary. In data science projects, a string is usually the column name of the data. When selecting multiple columns, it is inevitable that you need to create a list of strings. Indeed, we can create strings using the text in the list. However, we have to write pairs of quotation marks to enclose each string, which is a bit cumbersome for "lazy" people. Therefore, I prefer to use the split () method of a string to create a list of strings, as shown in the following code snippet.
# List of strings... # The typical way... Columns = ['name',' age', 'gender',' address', 'account_type']. Print ("* Literals:", columns)... # Do this instead... Columns = 'name age gender address account_type'.split ()... Print ("* Split with spaces:", columns)... # If the strings contain spaces, you can use commas instead... Columns = 'name, age, gender, address, account type'.split (',')... Print ("* Split with commas:", columns)... * Literals: ['name',' age', 'gender',' address', 'account_type'] * Split with spaces: [' name', 'age',' gender', 'address',' account_type'] * Split with commas: ['name',' age', 'gender',' address', 'account type']
As shown above, split () by default, this method uses spaces as delimiters and creates a list of strings based on strings. It is worth noting that when you create a list of strings that contain certain elements that contain spaces, you can choose to use other types of delimiters (for example, commas).
This usage is inspired by some built-in features. For example, when you create a tuple class, we can do this: Student = namedtuple ("Student", ["name", "gender", "age"]). The string list specifies the attributes of the tuple. However, you can also define the class to support it locally by defining Student = namedtuple ("Student", "name gender age"). For another instance, create an Enum class that supports the same alternative solution.
This is the end of the article on "how python uses Split () to create a string list". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.