求串列中數字和,串列中嵌套層次不限2層
輸入格式:
在一行中輸入串列或元組
輸出格式:
在一行中輸出數字的和
輸入樣例:
在這里給出一組輸入,例如:
[11,2,[3,7],(68,-1),"123",9]
輸出樣例:
在這里給出相應的輸出,例如:
99代碼如下:
#!/usr/bin/python # -*- coding: utf-8 -*- def getint(s): if type(s) == int: return s elif type(s) == str: return 0 elif type(s) == list or type(s) == tuple: result = 0 for i in range(0,len(s)): result += getint(s[i]) return result else: return 0 s = list(eval(input())) sum = 0 for i in range(0,len(s)): sum = sum + int(getint(s[i])) print(sum)
這個程式不難,使用遞回函式去或者數值,然后進行回圈遍歷累加就行,
讀書和健身總有一個在路上
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/154295.html
標籤:Python
下一篇:Python之根據條件篩選特定行
