How to convert str and list into each other in python

零到壹度
Release: 2018-03-31 09:54:26
Original
6914 people have browsed it

This article mainly shares with you how pythonstr extracts elements from str and replaces them in the list array. Friends who need it can take a look.

In In python, it is often necessary to extract elements from the string type str into an array list. For example, str is a comma-separated list of names, and each name needs to be extracted into a list whose elements are of type str.

如姓名列表str = 'Alice, Bob, John',需要将其提取为name_list = ['Alice', 'Bob', 'John']。
Copy after login

In turn, sometimes it is necessary to splice the character elements in a list into a complete string according to the specified delimiter. Fortunately, the str type in Python itself comes with two methods that provide corresponding functions.

Convert str to list

Use splitmethod

Basic use

<list> = <str&gt ;.split(<separator>)

<str>: The string that needs to be separated and extracted
<separator>: The delimiter used when extracting elements from <str2> is generally a str type, such as ','
<list>: Return Value, each element in the list is a separated fragment in <str>

Example

str = &#39;abc,def,ghi&#39;
a = str.split(&#39;,&#39;)
print(a)
Copy after login
  • 1

  • 2

  • 3

Get the result:

[&#39;abc&#39;,&#39;def&#39;,&#39;ghi&#39;]
Copy after login
  • 1

list is converted to str

Use the join method

Basically use

< str> = <separator>.join(<list>)

##<separator>: separator, which is str type, such as ','
<list>: List object that needs to be merged, each element must be of type str
<str>: Returns a str object, It is formed by splicing each element in <list> in order with the separator <separator>

Example

a = ','.join([&#39;abc&#39;,&#39;def&#39;,&#39;ghi&#39;])
print(a)
Copy after login

  • 1

  • 2

get

&#39;abc,def,ghi&#39;
Copy after login
  • 1

Note: When using the join method, the parameter list in the brackets must only contain members of type str Both methods are str methods, that is, . must be of str type before The difference between

and os.path.join() and os.path.split()

is in os The system path separator object os.path in the module also has two methods with the same name join() and split(), which are basically similar to str. The main difference is that all list type parameters of the method with the same name in str are here changed to tuple type

In python, it is often necessary to extract elements from the string type str into an array list, for example str is a comma-separated list of names, and each name needs to be extracted into a list whose elements are of type str.

For example, name list str = 'Alice, Bob, John', it needs to be extracted as name_list = ['Alice', 'Bob', 'John' ].

In turn, sometimes it is necessary to splice the character elements in a list into a complete string according to the specified delimiter. Fortunately, the str type in Python itself comes with two methods that provide corresponding functions.

Related recommendations:

Convert str and list to each other in python

Python string (str) and Conversion between lists

The above is the detailed content of How to convert str and list into each other in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template