导语:
本文主要介绍了关于python怎么样去除一个列表里重复的项的相关知识,包括python去除字符串重复元素,以及pythonlist去重复元素这些编程知识,希望对大家有参考作用。
python四种方法实现去除列表中的重复元素:
#第一种,使用集合的方式
def func1(one_list):
return list(set(one_list))
#第二种,使用字典的方式
def func2(one_list):
return {}.fromkeys(one_list).keys()
#第三种,使用列表推导的方式
def func3(one_list):
temp_list=[]
for one in one_list:
if one not in temp_list:
temp_list.append(one)
return temp_list
#第四种,使用排序的方式
def func4(one_list):
result_list=[]
temp_list=sorted(one_list)
i=0
while i<len(temp_list):
if temp_list[i] not in result_list:
result_list.append(temp_list[i])
else:
i+=1
return result_list
if __name__ == '__main__':
one_list=[56,7,4,23,56,9,0,56,12,3,56,34,45,5,6,56]
print func1(one_list)
print func2(one_list)
print func3(one_list)
print func4(one_list)
结果如下:
[0, 34, 3, 4, 5, 6, 7, 9, 12, 45, 23, 56]
[0, 34, 3, 4, 5, 6, 7, 9, 12, 45, 23, 56]
[56, 7, 4, 23, 9, 0, 12, 3, 34, 45, 5, 6]
[0, 3, 4, 5, 6, 7, 9, 12, 23, 34, 45, 56]
众多
,尽在python学习网,欢迎在线学习!
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ len在python中是什么意思10/27
- ♥ python路径是怎么转换的?11/28
- ♥ python如何将字符串中的所有“you”替换为“we”11/18
- ♥ 详细讲解如何在python中将ASCII码转换为字符12/20
- ♥ 如何在python中导入包10/14
- ♥ python3内置的hash函数是什么?11/20
内容反馈