“如何将列表中的字符串计为-lst =('abcdefgabc')”。输出应为a = 2,b = 2,c = 2,d = 1,e = 1,f = 1,g = 1

时间:2019-09-13 18:31:38

标签: python python-3.x

尝试计算lst=('abcdefgabc')

处的字符串计数
lst=('abcdefgabc')
for i in lst:
    lst.count(i)
    print(i,(lst.count(i)))

输出应为a=2, b=2, c=2, d=1,e=1,f=1,g=1

1 个答案:

答案 0 :(得分:0)

这是您要找的东西吗?

lst=('abcdefgabc')
new_list = []
for i in lst:
    a = i+"="+str((lst.count(i)))
    new_list.append(a)
b = sorted(set(new_list))
print(b)
for x in b:
    print(x)

输出:

['a=2', 'b=2', 'd=1', 'c=2', 'f=1', 'e=1', 'g=1']
a=2
b=2
d=1
c=2
f=1
e=1
g=1