Python Learning tips about list item stitching

Source: Internet
Author: User
Tags compact
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.