Use the set()
class to convert a string to a set, for example my_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}
The first example uses the set()
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'}
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}
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 the set()
class.
To convert a string to a set without splitting the characters of the string, pass a list containing the string to the set()
class, for example my_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'}
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'}
If you need to split a string on a delimiter to create a collection object, use the str.split()
method.
my_str = 'one,two,three' my_set = set(my_str.split(',')) print(my_set) # ????️ {'one', 'two', 'three'}
We use the str.split()
method to split the string on each comma and pass the result to the set()
class to create a set object.
my_str = 'one,two,three' print(my_str.split(',')) # ????️ ['one', 'two', 'three']
str.split()
method splits a string into a list of substrings using a delimiter.
This method takes the following 2 parameters:
separator Split the string into substrings every time a separator appears
maxsplit Complete maximum split at most (optional)
When no delimiter is passed to
str.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}
We use a generator expression to iterate the list and use the int()
class to convert each item to an integer.
set
The object contains integer elements.
Use str.split()
method to convert string to array, for example array = 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']
We use the str.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:
separator Split the string into substrings every time a separator appears
maxsplit Complete 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']
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 the str.split()
method without any parameters.
string = 'www jiyik com' array = string.split() print(array) # ????️ ['www', 'jiyik', 'com']
When no delimiter is passed to the str.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 the list()
class.
string = 'jiyik' array = list(string) print(array) # ????️ ['j', 'i', 'y', 'i', 'k']
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 the ast.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)) # ????️ <class 'list'>
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]
We use generator expressions to iterate over the list obtained from the str.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 the int()
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!