python的新功能。我正在尝试学习如何对具有混合类型的列表执行操作:
mylist = ['jack', 12, 'snake', 17, 'tumbleweed', 39]
如果该项目是一个str,我想将其添加到output_str,如果它是一个int,我想对其求和。这是我尝试过的:
mylist = ['jack', 12, 'snake', 17, 'tumbleweed', 39]
for x in mylist:
output_str = ''
if isinstance(x, str):
output_str += x
print(output_str)
elif isinstance(x, int):
print(sum(x))
我的预期输出:
'jacksnaketumbleweed'
68
但是出现以下错误:
TypeErrorTraceback (most recent call last)
<ipython-input-404-926417194c69> in <module>()
5 output_str += x
6 elif isinstance(x, int):
----> 7 print(sum(x))
TypeError: 'int' object is not iterable
我不明白为什么会这样。 谢谢
答案 0 :(得分:4)
使用列表理解功能,更快,更易读,更简单:
mylist = ['jack', 12, 'snake', 17, 'tumbleweed', 39]
int_sum = sum([i for i in mylist if isinstance(i, int)])
str_join = ' '.join([i for i in mylist if isinstance(i, str)])
print(str_join)
print(int_sum)
输出:
C:\Users\Documents>py test.py
jack snake tumbleweed
68
答案 1 :(得分:1)
sum
函数用于对数组中的所有元素求和,这解释了您得到的错误。另外,如果您在output_str
函数中声明for
,则每次扫描列表中的新值时,output_str
都会重置。这就是为什么我现在只在for
开始之前声明一次。
我还添加了变量final
,该变量将负责存储到目前为止所有数字的总和。所以我们有:
mylist = ['jack', 12, 'snake', 17, 'tumbleweed', 39]
final = 0
output_str = ''
for x in mylist:
if isinstance(x, str):
#here, we will concatenate each x value with the current value of output_str
output_str += x
elif isinstance(x, int):
#and here, everytime x is a number, we will sum it to final's current value. This operation is equal to final = final + 1
final += x
print(output_str)
print(final)
答案 2 :(得分:1)
尝试一下:
mylist = ['jack', 12, 'snake', 17, 'tumbleweed', 39]
total = 0
output_str = ''
for x in mylist:
if isinstance(x, str):
output_str = output_str + x
elif isinstance(x, int):
total += x
print(output_str)
print(total)
答案 3 :(得分:1)
您不能对单个元素进行求和,求和需要可迭代的输入类型。您可以这样做:
代码:
mylist = ['jack', 12, 'snake', 17, 'tumbleweed', 39]
output_str = ''
sum_of_int = 0
for x in mylist:
if isinstance(x, str):
output_str += x
elif isinstance(x, int):
sum_of_int += x
print(output_str)
print(sum_of_int)
输出:
jacksnaketumbleweed
68
答案 4 :(得分:1)
您也可以这样:
mylist = ['jack', 12, 'snake', 17, 'tumbleweed', 39]
output_str = ''
sum = 0
for i in mylist:
if type(i) == str:
output_str += i
elif type(i) == int:
sum += i
现在sum
将包含列表中整数的总和,而output_str
将包含列表中所有字符串的串联。
答案 5 :(得分:1)
sum()函数期望可迭代。试试这个:
from functools import reduce
mylist = ['jack', 12, 'snake', 17, 'tumbleweed', 39]
n, s = reduce(lambda a, x: (a[0] + x, a[1]) if isinstance(x, int) else (a[0], a[1] + x), mylist, (0, ''))
print('n = {}; s = {};'.format(n, s))
输出:
n = 68; s = jacksnaketumbleweed;
答案 6 :(得分:1)
my_list = ['jack', 12, 'snake', 17, 'tumbleweed', 39]
a, b = sum(e for e in my_list if isinstance(e, int)), ''.join(s for s in my_list if isinstance(s, str))
print(a, b)
输出
68 jacksnaketumbleweed
如果列表中同时包含浮点数和整数,则可以使用Number:
from numbers import Number
my_list = ['jack', 12, 'snake', 17, 'tumbleweed', 39.0]
a, b = sum(e for e in my_list if isinstance(e, Number)), ''.join(s for s in my_list if isinstance(s, str))
print(a, b)
输出
68.0 jacksnaketumbleweed
答案 7 :(得分:1)
您可以这样做:
mylist = ['jack', 12, 'snake', 17, 'tumbleweed', 39]
ints = [str(i).isnumeric() for i in mylist]
ix_int = np.flatnonzero(ints)
np.array(mylist)[ix_int].astype(int).sum()
68
对于字符串:
ix_str = np.setdiff1d(np.arange(len(mylist)), indices)
''.join(np.array(mylist)[ix_str])
'jacksnaketumbleweed'