This article mainly introduces the Python learning tips of the list of the concatenation of the relevant information, the text through the sample code introduced in very detailed, for everyone has a certain reference learning value, the need for friends below to see it together.
This article is about Python implementation of a list of items splicing a small skill, share it for everyone to reference the study, below to see the detailed introduction:
Typical code:
Data_list = [' A ', ' B ', ' C ', ' d ', ' e ', ' f '] separator = ' \ t ' data_joined = Separator.join (data_list) print (data_joined)
Its output is:
A b c d E F
Application Scenarios
When implementing many business requirements, each item in the list needs to be stitched into a string by a delimiter to complete some sort of serialization pattern for network transmission or logging, or to form some intermediate value for subsequent process use.
Why do you think it's a little tricky?
In the process of working with similar scenarios, it is almost the first thought to accomplish related requirements through a For loop, but with a for loop there are several lines of code that will take a bit more time to understand the logic of the code. In particular, there are cases where the last list item needs to be processed: we do not want the suffix of the resulting string to be a delimiter.
Benefits to be brought
1. More compact code, reduced logic loops, making code easier to read
2. Built-in methods, more efficient than stitching strings themselves
Other instructions
1. In fact, this method can also be applied to the tuple type, the collection type, or even a generator type and other types of guest iteration, and not only the list type;
2. Requires that each item in an iterative type be a string type;
3. In Java 8, the String class also provides a similar static method Join,java programming can also use more compact code to stitch strings;
Summarize