Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to do String match
#1
Hi,
I have the following array of string:

kaman
veeru
sri
balc
kaman
tryi
yua
sri

Now, I want to the two thing
Question1:
I want to find the locations(index) of "sri" in the list

Queastion2:
I want to know if the given name exist in the list or not (for example: "kaman"  exist in the list or not)
Thanks in advance,
Reply
#2
For more details about lists you can check Lists tutorial
Output:
>>> a_list = ['a', 'b', 'c'] >>> a_list.index('b') 1 >>> 'c' in a_list True
Reply
#3
Do not use private messages to seek help, use regular posts.

If you need to get indices of all positions of particular term in a list, you can iterate over that list and check its elements and eventually add index to your "list of matching indices". And you can shorten it with a list comprehension:
Output:
>>> a_list = list("sdflksdjfajfd") >>> a_list ['s', 'd', 'f', 'l', 'k', 's', 'd', 'j', 'f', 'a', 'j', 'f', 'd'] >>> occurences_of_f = [idx for idx, item in enumerate(a_list) if item == "f"] >>> occurences_of_f [2, 8, 11]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  if statement string match javiopro 2 1,598 Sep-04-2021, 05:56 PM
Last Post: javiopro
  string match Kristenl2784 1 1,419 Jul-28-2020, 03:14 PM
Last Post: Kristenl2784
  regex match in a string batchen 4 3,161 Jan-20-2020, 08:48 AM
Last Post: batchen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020