[英]Is there a more pythonic way to convert a list of booleans to an integer?
[英]Is there a simple way to convert the value to integer?
a,b,c=input().split()
a=int(a)
b=int(b)
c=int(c)
對於上面的代碼,是否有一種簡單的方法可以在一行中獲取數據並直接以空格分隔為整數? 提前致謝
def main(string):
arr = []
while string.count(', ') > 0:
index = string.index(', ')
arr.append(int(string[0:index]))
string = string[index + 2:len(string)]
arr.append(int(string))
return arr
print(main('1, 2, -3, 7592'))
這通過檢查逗號和空格(新元素的指示)並將每個元素添加到數組中而不帶空格以及將其轉換為 integer 來工作。
最好的解決方案是按照@Asocia 的建議使用map
方法,下面給出了代碼片段
a, b , c = map(int,input().split())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.