Python程序从列表中查找输入字符串的所有紧密匹配项
在本教程中,我们将找到问题的解决方案。让我们看看问题出在哪里。我们有一个字符串列表和一个元素。我们必须从列表中找到字符串,这些字符串必须与给定元素紧密匹配。参见示例。
Inputs strings = ["Lion", "Li", "Tiger", "Tig"] element = "Lion" Ouput Lion Li
我们可以通过使用startswith 内置方法来实现。请参阅步骤以查找字符串。
初始化字符串列表和字符串。
遍历列表。
如果列表中的字符串以元素开头或元素以列表中的字符串开头
Print the string
示例
## initializing the string list
strings = ["Lion", "Li", "Tiger", "Tig"]
element = "Lion"
for string in strings:
## checking for the condition mentioned above
if string.startswith(element) or element.startswith(string):
## printing the eligible string
print(string, end = " ")
print()如果您运行上述程序,
输出结果
Lion Li
如果您对该程序有任何疑问,请在评论部分中提及它们。
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便