您好我希望我的输出
add_sizes(['hello', 'world']) -> [('hello', 5), ('world', 5)]
但我正在
add_sizes(['hello', 'world']) -> [('hello', 5), ('hello', 5, 'helloworld', 5)]
我的代码是
def add_sizes(strings):
s = ()
t=[]
m=[]
for i in strings:
x=i
for i,c in enumerate(list(x)):
t.append(c)
l=(str(''.join(t)),i+1)
s += l
m.append(s)
print(m)
任何建议都将不胜感激
答案 0 :(得分:8)
只需使用列表理解:
>>> def add_sizes(strings):
... return [(s, len(s)) for s in strings]
...
>>>
>>> add_sizes(['hello', 'world'])
[('hello', 5), ('world', 5)]
或者如果你想在现场进行:
>>> def add_size(strings):
... for i, s in enumerate(strings):
... strings[i] = (s, len(s))
... return strings
...
>>> add_sizes(['hello', 'world'])
[('hello', 5), ('world', 5)]
答案 1 :(得分:1)
有人已经给出了一个完整的解决方案,所以我也会发布我的:
=IF(A1>=10^6,TEXT(A1/10^6,"0.0#")&" MHz",IF(A1>=10^3,TEXT(A1/10^3,"0.0#")&" KHz",TEXT(A1,"0.0#")&" Hz"))