[英]Import vendored dependencies in Python package without modifying sys.path or 3rd party packages
[英]Is there a way to generate a list string within Python, without any other 3rd party packages?
此代碼生成一個整數列表
dir_list = list(range(11))
dir_list
numpy 可以將每個元素轉換為字符串類型
import numpy as np
dir_list = np.array(dir_list, dtype=np.str)
dir_list
array(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
dtype='<U2')
有沒有辦法在 Python 中完成這項工作,而無需任何其他 3rd 方軟件包?
當然,通過這種方式,您可以遍歷 range 對象的每個元素並轉換為 str 並存儲為列表:
dir_list = [str(i) for i in range(11)]
>>> dir_list
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
您可以使用內置的map
函數簡單地將每個整數映射到字符串,並且map
返回迭代器,以便您可以將其轉換為列表。
list(map(str, range(11)))
應該可以。
輸出:
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.