如何删除列表中多次出现的字符串

如何删除列表中多次出现的字符串

本文介绍了如何删除列表中多次出现的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个像'''0024'',''哈哈'','0024''这样的清单 和作为输出我想要[''哈哈''] 如果我 myList.remove(''0024'') > 然后只删除'0024''的第一个实例。 似乎正常的表达式是救援,但我找不到 正确的工具。 谢谢! bahoo 解决方案 set([" 0024"," haha​​"]) set([" haha​​]) set([" 0024"," haha​​"])>> ; s.remove(" 0024") set([" haha​​"]) 如果需要,你也可以遍历列表,如下所示: counter = 0 your_list = [" 0024"," haha​​"," 0024"] for your in your_list : 如果我==''0024'': your_list.pop(柜台) 柜台+ = 1 Mike bahoo写道: 很难想象在这里使用正则表达式。这是一个简单的 尝试: def removeall(mylist,obj): 而mjist中的obj: mylist.remove(obj) 或者,如果您不需要进行更改: [x for m in mylist if x!= obj] Tom Hi,I have a list like [''0024'', ''haha'', ''0024'']and as output I want [''haha'']If ImyList.remove(''0024'')then only the first instance of ''0024'' is removed.It seems like regular expressions is the rescue, but I couldn''t findthe right tool.Thanks!bahoo 解决方案set(["0024","haha"])set(["haha"])set(["0024","haha"])>>s.remove("0024")set(["haha"])If you want, you can also loop over the list, like so:counter = 0your_list = ["0024","haha","0024"]for i in your_list:if i == ''0024'':your_list.pop(counter)counter += 1MikeIt''s hard to imagine using regular expressions here. Here''s a simpleattempt:def removeall(mylist,obj):while obj in mylist:mylist.remove(obj)Or, if you don''t need the changes in place:[x for x in mylist if x!= obj]Tom 这篇关于如何删除列表中多次出现的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-18 22:33