Python:每日一题86(答题领鱼币)
题目:这次很简单,希望大家踊跃参加。
一个列表,其中的元素可以是数值(整数、浮点数),也可以是字符串,要求计算各个元素之和,字符串以其长度参与计算,例如'abc',长度为3,就将3加入总和中。
def sum1(list1):
......
>>> sum1()
9.2
>>> sum1()
3.2
>>> sum1()
3
>>> sum1(['12321','asdewdew'])
13
我的解法,供参考。
**** Hidden Message ***** def sum1(list1):
result =0
for item in list1:
ifisinstance(item,list1):
number =len(item)
else:
number=item
result+=number
return result {:5_91:} 本帖最后由 sunnychou 于 2017-8-26 09:50 编辑
sunnychou 发表于 2017-8-26 09:27
a =
def Sum(lis):
b = []
for i in a:
if isinstance(i,(int,float)):
b.append(i)
else:
b.append(len(i))
return sum(b)
Sum(a)
TypeError: 'float' object is not callable
发现了出现了这样一个问题,然后觉得经常出现,查了一下,原来是之前有定义sum,You probably assigned some value to max before it.所以会返回这样的错误,不知道有没有人遇到 def sum1(list):
sum = 0
for i in list:
if type(i) == str:
sum += len(i)
else:
sum += i
return sum def sum1(list1):
sum=0
for i in list1:
if(isinstance(i,str)):
sum=sum+len(i)
else:
sum=sum+i
return sum
print(sum1()) def sum1(list1):
h=0.0
for i in list1:
if str(type(i))=="<class 'str'>":
h+=len(i)
else:
h+=i
return(h)
a=
b=sum1(a)
print(b)
我就想问问我的结果后面 那一长串000000007你们是怎么解决的 本帖最后由 pythonlaser 于 2017-8-26 21:26 编辑
def sm(*args):
typelist =
res=0
for each in args:
if type(each) in typelist:
if isinstance(each,str):
res+=len(each)
else:
res+=each
else:
raise TypeError('输入类型错误')
print(res)
sm(12,'asd',2.123) #17.123
PS:我没看清题目,我这个是随意输入,不是输入一个列表{:5_91:} def sum1(list1):
sum = 0
sum2 = 0
sum3 =0
for i in list1:
if type(i) == str:
sum2 += len(i)
else:
sum3 += i
sum = sum2 + sum3
return sum
print(sum1())
print(sum1())
print(sum1())
print(sum1(['12321','asdewdew'])) def sum1(list1):
s = 0
for each in list1:
s += len(each) if isinstance(each,str) else each
return s
def sum1(list):
sum = 0
for i in list:
if type(i) == str:
sum += len(i)
else:
sum += i
return sum def sum1(list1):
total = 0
for i in list1:
if type(i) == str:
total += len(i)
elif type(i) in :
total += i
else:
return None
return total def sum1(list1):
mysub=0
for i in list1:
mysub+=i if isinstance(i, int) or isinstance(i, float) else len(i)
return mysub def sum1(list):
sum = 0
for i in list:
if type(i) == str:
sum += len(i)
else:
sum += i
return sum 本帖最后由 wwb_coco 于 2017-8-28 19:44 编辑
def sum1(list1):
sum1 = 0
for item in list1:
if isinstance(item, (int,float)):
sum1 +=item
else:
sum1 += len(item)
return sum1
print(sum1())
print(sum1())
print(sum1())
print(sum1(['12321','asdewdew'])) 用 map 过一遍
def sum1(list1):
return sum(map(lambda x: len(x) if isinstance(x,str) else x, list1))
a =
def Sum(lis):
b = []
for i in a:
if isinstance(i,(int,float)):
b.append(i)
else:
b.append(len(i))
return sum(b)
Sum(a) solomonxian 发表于 2017-8-28 21:09
用 map 过一遍
还可以这样用{:5_106:} # -*- coding:utf8 -*-
# /usr/bin/en python
def sum1(list1):
list2=[]
for i in list1:
try:
i/2
list2.append(i)
except:
list2.append(len(i))
return sum(list2)
list1=,{'tom':'175','lily':'160'}]
print sum1(list1) import types
def deal_list(l):
sum = 0.0
for i in l:
if type(i) is types.StringType:
sum = sum + len(i)
else:
sum = sum + i
return sum
l =
a = deal_list(l)
print a
页:
[1]
2