Home  >  Article  >  Backend Development  >  How to convert str and list into each other in python

How to convert str and list into each other in python

零到壹度
零到壹度Original
2018-03-31 09:54:266888browse

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']。

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

4309a73696dbaeac0ddd115cebb6f9b7 = c277776b32055af9a38da728551201d8)

2bad728196366c441f29df524916be3d: The string that needs to be separated and extracted
319207bb0754d0bae0b0e669f17cedb3: The delimiter used when extracting elements from 2c4050acb7a40aac16f2a09ca22749cf is generally a str type, such as ','
4309a73696dbaeac0ddd115cebb6f9b7: Return Value, each element in the list is a separated fragment in 2bad728196366c441f29df524916be3d

Example

str = 'abc,def,ghi'
a = str.split(',')
print(a)
  • 1

  • 2

  • 3

Get the result:

['abc','def','ghi']
  • 1

list is converted to str

Use the join method

Basically use

d6064ac59b722ec93dddd54e64115bee = 319207bb0754d0bae0b0e669f17cedb3.join(4309a73696dbaeac0ddd115cebb6f9b7)

##319207bb0754d0bae0b0e669f17cedb3: separator, which is str type, such as ','
4309a73696dbaeac0ddd115cebb6f9b7: List object that needs to be merged, each element must be of type str
2bad728196366c441f29df524916be3d: Returns a str object, It is formed by splicing each element in 4309a73696dbaeac0ddd115cebb6f9b7 in order with the separator 319207bb0754d0bae0b0e669f17cedb3

Example

a = ','.join(['abc','def','ghi'])
print(a)

  • 1

  • 2

get

'abc,def,ghi'

  • 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!

Statement:
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
Previous article:How to use @ in pythonNext article:How to use @ in python