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
  How to read module/class from list of strings? popular_dog 1 466 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  Program to find Mode of a list PythonBoy 6 1,059 Sep-12-2023, 09:31 AM
Last Post: PythonBoy
  Trying to understand strings and lists of strings Konstantin23 2 752 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  problem in using int() with a list of strings akbarza 4 685 Jul-19-2023, 06:46 PM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,506 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,173 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Help with Logical error processing List of strings dmc8300 3 1,076 Nov-27-2022, 04:10 PM
Last Post: Larz60+
  Find (each) element from a list in a file tester_V 3 1,199 Nov-15-2022, 08:40 PM
Last Post: tester_V
  read a text file, find all integers, append to list oldtrafford 12 3,491 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  find some word in text list file and a bit change to them RolanRoll 3 1,512 Jun-27-2022, 01:36 AM
Last Post: RolanRoll

Forum Jump:

User Panel Messages

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