Python Forum
How to find the first and last of one of several characters in a list of strings?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to find the first and last of one of several characters in a list of strings?
#1
So I have a list of strings such as this:

my_list=["---abcdefgh----abc--","--abcd-a--","----------abcdefghij----ab-","-abcdef---a-","----abcdefghijklm----abc--"]

I want, for each string, to retrieve the position where the first and last letters appear. Or in other words, to find the position of the first character that isn't a "-" and the position of the last character that isn't a "-". It would be perfect if I could save the result as two lists, one of the first positions and another for the last.

I've tried using find() at least for the first position but since the character I'm trying to find is one of several letters, I don't know how to do it.

The output I wanted was something like this:

first_positions=[3,2,10,1,4]
last_positions=[17,7,25,11,23]

Thanks in advance for any answer
Reply
#2
you can use re module
you can iterate over each string and find at what position is the first/last char
if start and end chars are always '-' you can lstrip/rstrip it and see the change in len
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I like using re for this, since it can find the element and give you the index directly.

>>> mylist = ["---abcdefgh----abc--","--abcd-a--","----------abcdefghij----ab-","-abcdef---a-","----abcdefghijklm----abc--"]
>>> [re.search(r"[^-]", x).start() for x in mylist]
[3, 2, 10, 1, 4]
>>> [re.search(r"[^-]-*$", x).start() for x in mylist]
[17, 7, 25, 10, 23]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to find first 2 letter word in a list of words Oldman45 7 436 Aug-11-2020, 08:59 AM
Last Post: Oldman45
  Introduction to escaping characters in strings Geelong_crazy 1 296 Jul-18-2020, 06:58 PM
Last Post: DT2000
  Remove escape characters / Unicode characters from string DreamingInsanity 5 954 May-15-2020, 01:37 PM
Last Post: snippsat
  Find index value in List Martin2998 3 496 May-12-2020, 02:17 PM
Last Post: deanhystad
  Replacing characters in a string with a list cjms981 1 502 Dec-30-2019, 10:50 PM
Last Post: micseydel
  How to find something in a list using its index rix 1 462 Dec-20-2019, 04:12 PM
Last Post: stullis
  Iterating through a list of strings Ash_Ren 1 446 Nov-22-2019, 08:30 PM
Last Post: buran
  How do I delete symbols in a list of strings? Than999 1 515 Nov-16-2019, 09:37 PM
Last Post: ibreeden
  Split a long string into other strings with no delimiters/characters krewlaz 4 551 Nov-15-2019, 02:48 PM
Last Post: ichabod801
  Finding multiple strings between the two same strings Slither 1 747 Jun-05-2019, 09:02 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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