到目前为止,我们已经讨论了Python中的各种转换方法。在本教程中,我们将学习另一种转换方法,即将字符串转换为Python中的列表。

为了实现我们的目标,我们将使用以下方法:

  1. 使用 split()
  2. 使用带有分隔符的 split()
  3. 使用 strip()
  4. 使用 map()

让我们讨论其中每一个。

在第一个程序中,我们将使用 split() 来将字符串转换为Python中的列表。

使用 split()

下面的程序说明了如何执行它。

# Initialising the string values  
str_val1 = "Let us study programming."  
str_val2 = "But before that it is essential to have a basic knowledge of computers."  
str_val3 = "So first study what is IPO cycle."  
str_val4 = "Then learn about the generation of computers."  
#using split()  
print(str_val1.split())  
print(str_val2.split())  
print(str_val3.split())  
print(str_val4.split()) 

输出:

['Let', 'us', 'study', 'programming.']
['But', 'before', 'that', 'it', 'is', 'essential', 'to', 'have', 'a', 'basic', 'knowledge', 'of', 'computers.']
['So', 'first', 'study', 'what', 'is', 'IPO', 'cycle.']
['Then', 'learn', 'about', 'the', 'generation', 'of', 'computers.']

解释-

  1. 在第一步,我们初始化了四个我们想要转换的字符串。
  2. 在此之后,我们使用了 split(),以便获得一个列表,其中字符串的每个单词表示列表的一个元素。

在第二个程序中,我们在 split() 中指定了一个分隔符。

使用带有分隔符的 split()

考虑下面的程序,

# Initializing the string values  
str_val1="Let @ us @ study @ programming."  
str_val2="But # before # that # it # is # essential # to # have # a # basic # knowledge # of # computers."  
str_val3="So $ first $ study $ what $ is $ IPO $ cycle."  
str_val4="Then % learn % about % the % generation % of % computers."  
# Using split()  
print(str_val1.split("@"))  
print(str_val2.split("#"))  
print(str_val3.split("$"))  
print(str_val4.split("%"))  

输出:

['Let ', ' us ', ' study ', ' programming.']
['But ', ' before ', ' that ', ' it ', ' is ', ' essential ', ' to ', ' have ', ' a ', ' basic ', ' knowledge ', ' of ', ' computers.']
['So ', ' first ', ' study ', ' what ', ' is ', ' IPO ', ' cycle.']
['Then ', ' learn ', ' about ', ' the ', ' generation ', ' of ', ' computers.']

解释-

这种方法与前一个程序类似,唯一的区别是在出现分隔符时会将其视为一个列表元素。

在这个程序中,字符串中的分隔符分别是 @、#、$ 和 %。

现在,让我们看看如何使用 strip()

使用 strip()

下面的程序说明了相同的方法-

# Initialising the string values  
str_val1 = "Let us study programming."  
str_val2 = "But before that it is essential to have a basic knowledge of computers."  
# Using list()  
print(list(str_val1.strip()))  
print(list(str_val2.strip()))  

输出:

['L', 'e', 't', ' ', 'u', 's', ' ', 's', 't', 'u', 'd', 'y', ' ', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g', '.']
['B', 'u', 't', ' ', 'b', 'e', 'f', 'o', 'r', 'e', ' ', 't', 'h', 'a', 't', ' ', 'i', 't', ' ', 'i', 's', ' ', 'e', 's', 's', 'e', 'n', 't', 'i', 'a', 'l', ' ', 't', 'o', ' ', 'h', 'a', 'v', 'e', ' ', 'a', ' ', 'b', 'a', 's', 'i', 'c', ' ', 'k', 'n', 'o', 'w', 'l', 'e', 'd', 'g', 'e', ' ', 'o', 'f', ' ', 'c', 'o', 'm', 'p', 'u', 't', 'e', 'r', 's', '.']

解释-

  1. 在第一步,我们初始化了两个我们想要转换的字符串。
  2. 在此之后,我们使用了 strip(),以便获得一个列表,其中字符串的每个字符表示列表中的一个元素。

使用 map() 将字符串转换为列表的列表

# Initializing the string values  
str_val1="Let us study programming."  
str_val2="But before that it is essential to have a basic knowledge of computers."  
#using split()  
str_val1 = str_val1.split()  
str_val2 = str_val2.split()  
list_str1 = list(map(list,str_val1))  
list_str2 = list(map(list,str_val2))  
#displaying the list values  
print(list_str1)  
print(list_str2)  

输出:

[['L', 'e', 't'], ['u', 's'], ['s', 't', 'u', 'd', 'y'], ['p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g', '.']]
[['B', 'u', 't'], ['b', 'e', 'f', 'o', 'r', 'e'], ['t', 'h', 'a', 't'], ['i', 't'], ['i', 's'], ['e', 's', 's', 'e', 'n', 't', 'i', 'a', 'l'], ['t', 'o'], ['h', 'a', 'v', 'e'], ['a'], ['b', 'a', 's', 'i', 'c'], ['k', 'n', 'o', 'w', 'l', 'e', 'd', 'g', 'e'], ['o', 'f'], ['c', 'o', 'm', 'p', 'u', 't', 'e', 'r', 's', '.']]

解释-

  1. 在第一步,我们初始化了两个我们想要转换的字符串。
  2. 在此之后,我们使用了 split() 方法,然后使用 map(),以便将列表功能映射到字符串的每个元素。
  3. 执行给定程序后,将显示所需的输出。

最后,在最后一个程序中,我们使用了整数字符串。

将整数字符串转换

考虑下面的程序,

#initialising the string values  
str_val1 = "1 2 3 4 5 6 7 8 9"  
str_val2 = "12 21 32 44 54 76 83"  
#using split()  
str_val1 = str_val1.split()  
str_val2 = str_val2.split()  
list_str1 = list(map(int,str_val1))  
list_str2 = list(map(int,str_val2))  
#displaying the list values  
print(list_str1)  
print(list_str2)  

输出:

[1, 2, 3, 4, 5, 6, 7, 8, 9]
[12, 21, 32, 44, 54, 76, 83]

解释-

这个逻辑与上面的程序类似,但这里我们传递了一个整数字符串,并在所有元素上应用了 'int' 功能。

结论

在本教程中,我们学习了将字符串转换为Python中的列表的简单技巧。

标签: Tkinter教程, Tkinter安装, Tkinter库, Tkinter入门, Tkinter学习, Tkinter入门教程, Tkinter, Tkinter进阶, Tkinter指南, Tkinter学习指南, Tkinter进阶教程, Tkinter编程