已解决:从列表 python 中删除多个字符串

最后更新: 09/11/2023

问题是 Python 没有从列表中删除多个字符串的内置函数。

I have a list of strings:
<code>list = ['a','b','c','d']
</code>
and I want to remove multiple strings from the list, for example: 
<code>remove_list = ['a', 'c']
</code>


A:

You can use <code>set.difference()</code>:  (If you don't mind the order of elements in your list)  (If you want to keep the order of elements in your list, then use <code>filter()</code>)   (If you want to keep only unique elements in your list, then use <code>set()</code>)   (If you want to remove all duplicates from your list, then use <code>list(set())</code>)   (If you want to remove all duplicates from your list and keep the order of elements as well, then use <code>[i for i in set(lst)]</code>) 

琴弦

在 Python 中,字符串是字符序列。 字符串可用于存储文本、数字或任何其他类型的数据。

要在 Python 中创建字符串,您可以使用字符串构造函数:

字符串 = “你好”

您还可以通过将两个字符串连接在一起来创建一个字符串:

字符串 = “你好” + “世界”

书单

在 Python 中,列表是一种数据结构,可让您存储项目的集合。 列表可以是空的,也可以包含任何类型的对象,包括其他列表。

要在 Python 中创建列表,您可以使用 list() 函数。 要访问列表中的第一项,可以使用 index() 函数。 要访问列表中的最后一项,可以使用 len() 函数。

要将项目添加到列表的末尾,可以使用 append() 函数。 要从列表末尾删除一个项目,可以使用 pop() 函数。

相关文章: