Python 去除列表中重复字母的简单方法
使用 Python 实现一个功能,输入为包含一组字母的列表,输出为去掉输入中重复字母的列表。
def remove_duplicates(input_list):
return list(set(input_list))
# 示例输入
input_list = ['a', 'b', 'a', 'c', 'b', 'd', 'e', 'f', 'e', 'g']
# 调用函数并打印结果
output_list = remove_duplicates(input_list)
print(output_list)
输出结果为:
['a', 'c', 'g', 'f', 'b', 'e', 'd']
在这个例子中,输入列表 input_list 包含重复的字母。通过调用 remove_duplicates 函数,并将输入列表作为参数传递,可以得到一个去除重复字母的输出列表 output_list。这里使用了 Python 内置的 set 数据结构,它会自动去除重复元素。最后,将 set 转换回列表类型,即可得到最终的输出结果。
原文地址: https://www.cveoy.top/t/topic/H44 著作权归作者所有。请勿转载和采集!