当需要复制字符串中重复出现的字符时,可以使用键、'index' 方法和列表推导式。列表推导式是迭代列表并对其执行操作的简写。'index' 方法返回特定值/可迭代对象的索引,以下是对此的演示 - 示例实时演示my_str = 'Jane is the best . Jane loves to cook. Jane and Will cook together' print("字符串为:") print(my_str) replace_dict = {'Jane' : 'She' } my_list = my_str.split(' ') my_result = ' '.join([replace_dict.get(val) if val in replace_dict.keys() and my_list.index(val) != idx else ... 阅读更多