首页 > 其他 > 详细

替换字符串中的字符为“(” 或“)”

时间:2020-04-05 17:44:29      阅读:93      评论:0      收藏:0      [点我收藏+]
题目描述:
# The goal of this exercise is to convert a string to a new string where each character
# in the new string is "(" if that character appears only once in the original string,
# or ")" if that character appears more than once in the original string.
# Ignore capitalization when determining if a character is a duplicate

我的解答:
def duplicate_letter(b):
c = {}
for i in b:
if i not in c.keys():
c[i] = 1
elif i in c.keys():
c[i] = c[i] + 1
new_word = b.lower()
for j in new_word:
if c[j] > 1:
new_word = new_word.replace(j, ")")
elif c[j] == 1:
new_word = new_word.replace(j, "(")
return new_word

替换字符串中的字符为“(” 或“)”

原文:https://www.cnblogs.com/wlj-axia/p/12637780.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!