如何添加相同键的元组并排序
How to add the tuple of same keys and sorted
我有字符串 s = 'a3c9b1c1'
我预期的结果是 a3b1c10
必须按字母顺序排序
我可以转换成元组
i = iter(t)
m = list(zip(i, i))
print (m)
from collections import defaultdict
d = defaultdict(list)
for k, v in m:
d[k].append((int(v)))
d
我出
defaultdict(list, {'a': [3], 'c': [9, 1], 'b': [1]})
d = {k:tuple(v) for k, v in d.items()}
print (d)
我出
{'a': (3,), 'c': (9, 1), 'b': (1,)}
我添加了下面的代码
for k,v in d.items():
t = ("{}{}".format(k, sum(v)))
print (t,end='')
如何使用相同的键添加值?或没有 end=''
如果字母出现两位(或更多)数字,您的方法将失败:
s = 'a3c9b10c1'
i = iter(s)
m = list(zip(i, i))
print(m)
# [('a', '3'), ('c', '9'), ('b', '1'), ('0', 'c')]
相反,您可以使用正则表达式分隔字母数字对,然后对相同的字母求和:
import re
from collections import defaultdict
s = 'a3c9b1c1'
data = re.findall(r'([A-Za-z])(\d+)', s)
# [('a', '3'), ('c', '9'), ('b', '1'), ('c', '1')]
counts = defaultdict(int)
for letter, count in data:
counts[letter] += int(count)
# {'a': 3, 'c': 10, 'b': 1}
print(''.join('{}{}'.format(k, v) for k, v in sorted(counts.items())))
产出
a3b1c10
假设这是一个 XY 问题并且实际输入是 'aaacccccccccbc'
(以任何顺序)collections.Counter
:
更容易
from collections import Counter
s = 'aaacccccccccbc'
c = Counter(s)
print(''.join('{}{}'.format(k, v) for k, v in sorted(c.items())))
产出
a3b1c10
s = 'a3c9b1c1'
t = list(s)
#print (t)
i = iter(t)
m = list(zip(i, i))
print ('tuple :', m)
sums = defaultdict(int)
for i, j in m:
sums[i] += int(j)
print ('dict:', dict(sums))
print ('joined string:', ''.join('{}{}'.format(key, val) for key, val in sorted(sums.items())))
出来
tuple : [('a', '3'), ('c', '9'), ('b', '1'), ('c', '1')]
dict: {'a': 3, 'c': 10, 'b': 1}
joined string: a3b1c10
我有字符串 s = 'a3c9b1c1'
我预期的结果是 a3b1c10
必须按字母顺序排序
我可以转换成元组
i = iter(t)
m = list(zip(i, i))
print (m)
from collections import defaultdict
d = defaultdict(list)
for k, v in m:
d[k].append((int(v)))
d
我出
defaultdict(list, {'a': [3], 'c': [9, 1], 'b': [1]})
d = {k:tuple(v) for k, v in d.items()}
print (d)
我出
{'a': (3,), 'c': (9, 1), 'b': (1,)}
我添加了下面的代码
for k,v in d.items():
t = ("{}{}".format(k, sum(v)))
print (t,end='')
如何使用相同的键添加值?或没有 end=''
如果字母出现两位(或更多)数字,您的方法将失败:
s = 'a3c9b10c1'
i = iter(s)
m = list(zip(i, i))
print(m)
# [('a', '3'), ('c', '9'), ('b', '1'), ('0', 'c')]
相反,您可以使用正则表达式分隔字母数字对,然后对相同的字母求和:
import re
from collections import defaultdict
s = 'a3c9b1c1'
data = re.findall(r'([A-Za-z])(\d+)', s)
# [('a', '3'), ('c', '9'), ('b', '1'), ('c', '1')]
counts = defaultdict(int)
for letter, count in data:
counts[letter] += int(count)
# {'a': 3, 'c': 10, 'b': 1}
print(''.join('{}{}'.format(k, v) for k, v in sorted(counts.items())))
产出
a3b1c10
假设这是一个 XY 问题并且实际输入是 'aaacccccccccbc'
(以任何顺序)collections.Counter
:
from collections import Counter
s = 'aaacccccccccbc'
c = Counter(s)
print(''.join('{}{}'.format(k, v) for k, v in sorted(c.items())))
产出
a3b1c10
s = 'a3c9b1c1'
t = list(s)
#print (t)
i = iter(t)
m = list(zip(i, i))
print ('tuple :', m)
sums = defaultdict(int)
for i, j in m:
sums[i] += int(j)
print ('dict:', dict(sums))
print ('joined string:', ''.join('{}{}'.format(key, val) for key, val in sorted(sums.items())))
出来
tuple : [('a', '3'), ('c', '9'), ('b', '1'), ('c', '1')]
dict: {'a': 3, 'c': 10, 'b': 1}
joined string: a3b1c10