Python - 查找所有是给定的字符串列表的子字符串的字符串
当需要找到所有给定字符串列表的子字符串的字符串时,使用“set”和“list”属性。
示例
以下是对其进行演示
my_list_1 = ["Hi", "there", "how", "are", "you"]
my_list_2 = ["Hi", "there", "how", "have", "you", 'been']
print("The first list is :")
print(my_list_1)
print("The second list is :")
print(my_list_2)
my_result = list(set([elem_1 for subset_1 in my_list_1 for elem_1 in my_list_2 if elem_1 in subset_1]))
print("The result is :")
print(my_result)输出
The first list is : ['Hi', 'there', 'how', 'are', 'you'] The second list is : ['Hi', 'there', 'how', 'have', 'you', 'been'] The result is : ['there', 'you', 'Hi', 'how']
解释
定义了两个字符串列表,并在控制台上显示它们。
迭代这两个列表,并使用“set”属性来获取列表中的唯一值。
现在将此转换为列表。
这被分配给一个变量。
这是显示在控制台上的输出。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言
C++
C#
MongoDB
MySQL
Javascript
PHP