簡體   English   中英

Python-如何將包含多個數字的字符串轉換為int?

[英]Python - How do I convert a a string containing more than one number to an int?

如何將包含多個數字的字符串轉換為python中的int,例如: "100, 200, 300""400, 500" 我知道如何將包含單個數字(例如"100""56"的字符串轉換為int,但不能將包含2個數字的字符串轉換為int。

有誰知道該怎么做?

非常感謝你的幫助。

使用map()將列表元素映射到int

x = "100, 200, 300"
list(map(int, x.split(",")))

輸出:

[100, 200, 300]

您還可以使用列表推導

In [635]: x = "100, 200, 300"
In [638]: results = [int(i) for i in x.split(',')]

In [639]: results
Out[639]: [100, 200, 300]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM