將字符串拆分為整數列表:
- 使用
str.split()
方法將字符串拆分為字符串列表。 - 使用列表理解來迭代字符串列表。
- 使用
int()
類將每個字符串轉換為整數。
my_str = '2 4 6 8 10'
list_of_strings = my_str.split(' ')
print(list_of_strings) # ??? ['2', '4', '6', '8', '10']
list_of_integers = [int(x) for x in list_of_strings]
print(list_of_integers) # ??? [2, 4, 6, 8, 10]
列表推導用于對每個元素執(zhí)行一些操作,或者選擇滿足條件的元素子集。
在每次迭代中,我們將當前列表項傳遞給 int()
類并返回結果。
如果我們需要在每個數字上拆分字符串,我們還應該使用列表推導。
my_str = '246810'
list_of_ints = [int(x) for x in my_str]
print(list_of_ints) # ??? [2, 4, 6, 8, 1, 0]
我們直接遍歷字符串,在每次迭代中,我們將包裹在字符串中的數字轉換為整數。
處理包含非數字字符的字符串
如果我們的字符串包含非數字字符,請在將其傳遞給 int()
類之前使用 str.isdigit()
方法檢查當前字符是否為數字。
my_str = 'x y z 2 4 6 8 10 a'
list_of_strings = my_str.split(' ')
# ??? ['x', 'y', 'z', '2', '4', '6', '8', '10', 'a']
print(list_of_strings)
list_of_integers = [int(x) for x in list_of_strings if x.isdigit()]
print(list_of_integers) # ??? [2, 4, 6, 8, 10]
如果字符串中的所有字符都是數字并且至少有 1 個字符,則 str.isdigit 方法返回 True,否則返回 False。
使用 map() 將字符串拆分為整數列表
將字符串拆分為整數列表:
- 使用
str.split()
方法將字符串拆分為字符串列表。 - 使用
map()
函數將每個字符串轉換為整數。 - 使用
list()
類將地圖對象轉換為列表。
# ??? 包含帶空格分隔符的整數的字符串
my_str = '2 4 6 8 10'
list_of_strings = my_str.split(' ')
print(list_of_strings) # ??? ['2', '4', '6', '8', '10']
list_of_integers = list(map(int, list_of_strings))
print(list_of_integers) # ??? [2, 4, 6, 8, 10]
如果我們的字符串不包含數字之間的分隔符,請改用以下代碼示例。
# ??? 如果你想在每個數字上拆分字符串
my_str = '246810'
list_of_ints = [int(x) for x in my_str]
print(list_of_ints) # ??? [2, 4, 6, 8, 1, 0]
如果字符串中的整數用另一個定界符分隔,例如 逗號,將包含逗號的字符串傳遞給 str.split()
方法。
my_str = '2,4,6,8,10'
list_of_strings = my_str.split(',')
print(list_of_strings) # ??? ['2', '4', '6', '8', '10']
list_of_integers = list(map(int, list_of_strings))
print(list_of_integers) # ??? [2, 4, 6, 8, 10]
我們使用 str.split()
方法將字符串拆分為字符串列表。
my_str = '2 4 6 8 10'
list_of_strings = my_str.split(' ')
print(list_of_strings) # ??? ['2', '4', '6', '8', '10']
str.split() 方法使用定界符將字符串拆分為子字符串列表。
該方法采用以下 2 個參數:
- separator 在每次出現分隔符時將字符串拆分為子字符串
- maxsplit 最多完成 maxsplit 拆分(可選)
下一步是使用 map()
函數將列表中的每個字符串轉換為整數。
my_str = '2 4 6 8 10'
list_of_strings = my_str.split(' ')
print(list_of_strings) # ??? ['2', '4', '6', '8', '10']
list_of_integers = list(map(int, list_of_strings))
print(list_of_integers) # ??? [2, 4, 6, 8, 10]
map()
函數將一個函數和一個可迭代對象作為參數,并使用可迭代對象的每個項目調用該函數。
在每次迭代中,我們將字符串傳遞給 int()
類以將其轉換為整數。
map()
函數返回一個地圖對象,因此我們必須使用 list() 類將其轉換為列表。
使用 for 循環(huán)將字符串拆分為整數列表
將字符串拆分為整數列表:
- 使用
str.split()
方法將字符串拆分為字符串列表。 - 使用 for 循環(huán)遍歷列表。
- 將每個字符串轉換為整數并將結果附加到新列表。
my_str = '2 4 6 8 10'
list_of_integers = []
for item in my_str.split(' '):
list_of_integers.append(int(item))
print(list_of_integers) # ??? [2, 4, 6, 8, 10]
我們使用 for 循環(huán)迭代調用 str.split()
的結果。
my_str = '2 4 6 8 10'
# ??? ['2', '4', '6', '8', '10']
print(my_str.split(' '))
在 for 循環(huán)的每次迭代中,我們將當前字符串轉換為整數并將該值附加到新列表中。
list.append()
方法將一個項目添加到列表的末尾。
使用 NumPy 將字符串拆分為整數列表
我們還可以使用 NumPy 模塊將字符串拆分為整數列表。
import numpy as np
my_str = '2 4 6 8 10'
my_list = np.fromstring(my_str, dtype=int, sep=' ').tolist()
print(my_list) # ??? [2, 4, 6, 8, 10]
我們可以通過運行以下命令來安裝 NumPy。
$ pip install numpy
# ??? 或者使用 pip3
$ pip3 install numpy
·numpy.fromstring· 方法返回一個新的一維數組,該數組從字符串中的文本初始化。
該方法采用用于確定拆分字符的 sep 參數。
import numpy as np
my_str = '2,4,6,8,10'
my_list = np.fromstring(my_str, dtype=int, sep=',').tolist()
print(my_list) # ??? [2, 4, 6, 8, 10]
tolist
方法將 numpy 數組轉換為列表。
使用 re.findall() 將字符串拆分為整數列表
我們還可以使用 re.findall()
方法將字符串拆分為整數列表。
import re
my_str = '2 4 6 8 10 12'
list_of_strings = re.findall(r'\d+', my_str)
print(list_of_strings) # ??? ['2', '4', '6', '8', '10', '12']
list_of_ints = [int(x) for x in list_of_strings
if x.isdigit()]
print(list_of_ints) # ??? [2, 4, 6, 8, 10, 12]
re.findall
方法將一個模式和一個字符串作為參數,并返回一個字符串列表,其中包含字符串中該模式的所有非重疊匹配項。
我們傳遞給 re.findall()
方法的第一個參數是一個正則表達式。
\d
字符匹配從 0 到 9 的數字(以及許多其他數字字符)。
加號 +
使正則表達式匹配前面字符(數字范圍)的 1 次或多次重復。
re.findall()
方法返回一個包含匹配字符串的列表。文章來源:http://www.zghlxwxcb.cn/news/detail-451994.html
最后一步是使用列表推導將字符串列表轉換為數字列表。文章來源地址http://www.zghlxwxcb.cn/news/detail-451994.html
到了這里,關于Python 中將字符串拆分為整數列表的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!