在Java中添加数字

时间:2014-12-06 21:18:02

标签: java

我想加上数字。

(java.numbers)

2 个答案:

答案 0 :(得分:1)

你可以使用切片

a = ("jim", [2,3,4,6])  
sum(a[1][:-1])               only will work if the number in order, else need to use sorted

演示:

>>> a=("jim", [2,3,4,6])
>>> a[1]
[2, 3, 4, 6]
>>> a[1][:-1]  
[2, 3, 4]
>>> sum(a[1][:-1])     # sum will give sum of element in list
9
>>> print a[0],sum(a[1][:-1])  
jim 9

功能:

>>> def my_function(*a):       # *a is called splat operator, can catch any number of argument in tuple
...     return a[0],sum(sorted(a[1][:-1]))      #sorted will sort the list, so that largest number goes at end of list, so we can exclude it
... 
>>> my_function("jim", [2,3,4,6])
('jim', 9)

答案 1 :(得分:0)

以下是一些能够满足您需求的代码。

l = ("jim", [2,3,4,6])
def totalAndDisplay(l):
    l[1].sort()
    print l[0],sum(l[1][:-1])