[英]How can i convert string to int list with python?
stringIds = "948274432, 948364892, 943224012"
我有這樣一個字符串。 我如何通過將它們轉換為 int 將此處的 ID 轉移到列表中?
intList = []
for x in stringIds:
intList.append(int(x))
我嘗試了這樣的代碼,但我得到了ValueError: invalid literal for int() with base 10: ','
這是我想要得到的樣本列表
intList = [948274432, 948364892, 943224012]
str.split
可以在逗號處拆分字符串,然后您可以將int
應用於每個元素以將它們作為數字獲取。
intList = [int(x) for x in stringIds.split(', ')]
或者,像您的示例一樣寫成for
循環,
intList = []
for x in stringIds.split(', '):
intList.append(int(x))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.