python3 字典转化字符串

1. 记录 leetcode 题目

49. 字母异位词分组
383. 赎金信

2. 使用字典分析字符串

#!/usr/bin/python3

input=input()
#input='a:3,b:5,c:2@a:1,b:2,c:2'
split = input.split('@')

dict1={}
dict2={}
for i in split[0].split(','):
    str=i.split(':')
    dict1[str[0]]=int(str[1])

for i in split[1].split(','):
    str=i.split(':')
    dict2[str[0]]=int(str[1])

for k,val in dict2.items():
    dict1[k] -= val
    if 0 == dict1[k]:
        del dict1[k]

list1 = []
for k,v in dict1.items():
    str1 = "{}".format('%s:%d' %(k, v))
    list1.append(str1)
print(",".join(list1))

题目意思:@之前的字符集 要 减去@之后的字符集

输入
a:3,b:5,c:2@a:1,b:2,c:2
输出
a:2,b:3

posted @ 2024-09-30 08:59  靖意风  Views(18)  Comments(0)    收藏  举报