我有一个字符串l1
,例如包含以下内容:aackcdldccc
。我想计算每个字符使用字典出现的次数。所需的最终结果应该是这样的字典:
a:2
c:5
k:1
d:2
l:1
如何修复我的代码,使其可以正常工作? 我使用以下代码并收到错误消息:
l1= ('aackcdldccc')
print (l1)
d={}
print (len(l1))
for i in (range (len(l1))):
print (i)
print (l1[i])
print (list(d.keys()))
if l1[i] in list(d.keys()):
print ('Y')
print (l1[i])
print (list(d.keys())[l1[i]])
d1 = {l1[i]:list(d.values())[l1[i]+1]}
#print (d1)
#d.update (d1)
else:
print ('N')
d1={l1[i]:1}
d.update (d1)
这是我得到的错误: aackcdldccc
11
0
a
[]
N
1
a
['a']
Y
a
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-185-edf313da1f8d> in <module>()
10 print ('Y')
11 print (l1[i])
---> 12 print (list(d.keys())[l1[i]])
13 #d1 = {l1[i]:list(d.values())[l1[i]+1]}
14 #print (d1)
TypeError: list indices must be integers or slices, not str
答案 0 :(得分:2)
有两种方法可以做到这一点:
REPLACE( @[User::Variable],"\\","")