Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize list splicing and merging by python

2025-01-19 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 to achieve list stitching and merging in python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

List splicing & merging

Start with the list of strings:

Colors = ['red',' blue', 'green',' yellow']

We want to concatenate these strings together to create a long chain. Especially when the number of substrings is large, avoid doing this:

Result =''for s in colors: result + = s

It's very slow to do this. And takes up a lot of memory and performance. The sum will accumulate, store, and then proceed with each intermediate step.

Instead, do the following:

Colors = ['red',' blue', 'green',' yellow'] print ('Choose',', '.join (colors [:-1]),\' or', colors [- 1]) > > Choose red, blue, green or yellow

The join () function makes an entire copy at once. When dealing with only a few strings, it is no different from other functions. But it can get you into the habit of building long chains with the best functions, because using the join () function does make a big difference when faced with hundreds of strings.

Here are some tips for using the join () function. If you want to use spaces as delimiters:

# Do this: # And not this: if x: if x = = True: pass pass# Do this: # And not this: if items: if len (items)! = 0: pass pass# and especially not that: if items! = []: pass

Or commas and spaces:

Result =', '.join (colors)

To make the sentence grammatically correct, use a comma between every value except the last value (people prefer to use "or"). The syntax for splitting the list will do the rest. [:-1] returns everything except the last value, which we can connect with a comma.

Colors = ['red',' blue', 'green',' yellow'] print ('Choose',', '.join (colors [:-1]),\' or', colors [- 1]) > > Choose red, blue, green or yellow this article on "how to implement list splicing and merging in python" ends here. I hope the above content can be helpful 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report