简体   繁体   English

如何从列表中删除一个字符串

[英]How do I remove one string from a list

I would like to remove one string from a list of strings (some are identical strings). 我想从字符串列表中删除一个字符串(有些是相同的字符串)。 For example in the list [hello, world, world, hello hello], I would like to remove one of the hello's at random. 例如,在列表[hello,world,world,hello hello]中,我想随机删除其中一个hello。 How would I do this? 我该怎么做?

PS In this situation I do not know the position of any of the 'hello' strings in the list PS在这种情况下,我不知道列表中任何“ hello”字符串的位置

original = ['hello', 'world', 'world', 'hello', 'hello']
hello_indices = [i for i, x in enumerate(original) if x == 'hello']
del original[random.choice(hello_indices)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM