How to convert string to set in Python

王林
Release: 2023-05-05 23:52:05
forward
2302 people have browsed it

Convert a string to a set in Python

Use theset()class to convert a string to a set, for examplemy_set = set(my_str).set()class will convert a string into a set by splitting its characters.

my_str = 'one' # ✅ 通过拆分字符将字符串转换为集合 my_set = set(my_str) print(my_set) # ????️ {'n', 'o', 'e'} # ---------------------------------------------------- # ✅ 将字符串转换为不拆分字符的集合 my_set = set([my_str]) print(my_set) # ????️ {'one'} # ---------------------------------------------------- # ✅ 将字符串转换为具有多个元素的集合 my_str = 'one,two,three' my_set = set(my_str.split(',')) print(my_set) # ????️ {'one', 'two', 'three'} # ---------------------------------------------------- # ✅ 将字符串转换为具有多个整数元素的集合 my_str = '1,2,3' my_set = set(int(item) for item in my_str.split(',')) print(my_set) # ????️ {1, 2, 3}
Copy after login

How to convert string to set in Python

The first example uses theset()class to convert a string into a collection object by splitting characters.

my_str = 'one' my_set = set(my_str) print(my_set) # ????️ {'n', 'o', 'e'}
Copy after login

Each character in the string becomes a separate collection element.

The same method can be used if we need to convert a string into a set of integers.

my_str = '123' my_set = set(int(digit) for digit in my_str) print(my_set) # ????️ {1, 2, 3}
Copy after login

We use generator expressions to iterate over strings.

Generator expressions are used to perform some operation on each element or select a subset of elements that meet a condition.

In each iteration, we convert the current number to an integer and return the result.

Alternatively, we can pass the list to theset()class.

To convert a string to a set without splitting the characters of the string, pass a list containing the string to theset()class, for examplemy_set = set( [my_str]). The collection will contain strings as its individual elements.

my_str = 'one' my_set = set([my_str]) print(my_set) # ????️ {'one'}
Copy after login

set()The class accepts an iterable optional parameter and returns a new collection object whose elements are taken from the iterable object.

empty_set = set() print(empty_set) # ????️ set() my_set = set(['one', 'two', 'three']) print(my_set) # ????️ {'three', 'two', 'one'}
Copy after login

If you need to split a string on a delimiter to create a collection object, use thestr.split()method.

my_str = 'one,two,three' my_set = set(my_str.split(',')) print(my_set) # ????️ {'one', 'two', 'three'}
Copy after login

We use thestr.split()method to split the string on each comma and pass the result to theset()class to create a set object.

my_str = 'one,two,three' print(my_str.split(',')) # ????️ ['one', 'two', 'three']
Copy after login

str.split()method splits a string into a list of substrings using a delimiter.

This method takes the following 2 parameters:

  • separatorSplit the string into substrings every time a separator appears

  • maxsplitComplete maximum split at most (optional)

When no delimiter is passed tostr.split()method, it splits the input string into one or more whitespace characters.

If the delimiter is not found in the string, a list containing only 1 element is returned.

If we need to split a string into a collection containing integer elements, we can use generator expressions.

my_str = '1,2,3' my_set = set(int(item) for item in my_str.split(',')) print(my_set) # ????️ {1, 2, 3}
Copy after login

We use a generator expression to iterate the list and use theint()class to convert each item to an integer.

setThe object contains integer elements.

Extension: Convert string to array in Python

Usestr.split()method to convert string to array, for examplearray = string .split(',').str.split()The method will split the string into a list on each occurrence of the provided delimiter.

string = 'www,jiyik,com' # ✅ 将逗号分隔的字符串转换为数组 array = string.split(',') print(array) # ????️ ['www', 'jiyik', 'com'] # --------------------------------------------- # ✅ 将空格分隔的字符串转换为数组 string = 'www jiyik com' array = string.split(' ') print(array) # ????️ ['www', 'jiyik', 'com'] # --------------------------------------------- # ✅ 将字符串转换为字符数组 string = 'jiyik' array = list(string) print(array) # ????️ ['j', 'i', 'y', 'i', 'k'] # --------------------------------------------- # ✅ 将字符串转换为整数数组 string = '1,2,3' array = list(int(char) for char in string.split(',')) print(array) # ????️ [1, 2, 3] # --------------------------------------------- # ✅ 将字符串转换为单元素数组 string = 'jiyikcom' array = [string] print(array) # ????️ ['jiyikcom']
Copy after login

How to convert string to set in Python

We use thestr.split()method to convert the string into an array.

str.split() method splits a string into a list of substrings using a delimiter.

This method takes the following 2 parameters:

  • separatorSplit the string into substrings every time a separator appears

  • maxsplitComplete maximum split at most (optional)

This is a comma separated string converted to Example of array.

string = 'www,jiyik,com' array = string.split(',') print(array) # ????️ ['www', 'jiyik', 'com'] string = 'www,jiyik,com' array = string.split(',') print(array) # ????️ ['www', 'jiyik', 'com']
Copy after login

str.split()method splits the string every time a comma occurs.

If we need to convert the string into an array of words, please call thestr.split()method without any parameters.

string = 'www jiyik com' array = string.split() print(array) # ????️ ['www', 'jiyik', 'com']
Copy after login

When no delimiter is passed to thestr.split()method, it splits the input string into one or more whitespace characters.

If the provided delimiter is not found in the string, a list containing only 1 element is returned.

If you need to convert a string into a character array, please use thelist()class.

string = 'jiyik' array = list(string) print(array) # ????️ ['j', 'i', 'y', 'i', 'k']
Copy after login

The list class accepts an iterable object and returns a list object.

If we have a string representation of an array and need to convert the string to an actual list, use theast.literal_eval()method.

from ast import literal_eval my_str = '[1,2,3,4]' my_list = literal_eval(my_str) print(my_list) # ????️ [1, 2, 3, 4] print(type(my_list)) # ????️ 
Copy after login

If we need to convert a string into a list of integers, use generator expressions.

string = '1,2,3' array = list(int(digit) for digit in string.split(',')) print(array) # ????️ [1, 2, 3]
Copy after login

We use generator expressions to iterate over the list obtained from thestr.split()method.

Generator expressions are used to perform some operation on each element or select a subset of elements that meet a condition.

In each iteration, we use theint()class to convert the current number to an integer.

The above is the detailed content of How to convert string to set in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!