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?
#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


Messages In This Thread
RE: How to find the first and last of one of several characters in a list of strings? - by bowlofred - Jun-02-2020, 05:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read module/class from list of strings? popular_dog 1 475 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  Program to find Mode of a list PythonBoy 6 1,094 Sep-12-2023, 09:31 AM
Last Post: PythonBoy
  Trying to understand strings and lists of strings Konstantin23 2 761 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  problem in using int() with a list of strings akbarza 4 702 Jul-19-2023, 06:46 PM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,542 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,222 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Help with Logical error processing List of strings dmc8300 3 1,084 Nov-27-2022, 04:10 PM
Last Post: Larz60+
  Find (each) element from a list in a file tester_V 3 1,225 Nov-15-2022, 08:40 PM
Last Post: tester_V
  read a text file, find all integers, append to list oldtrafford 12 3,584 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,537 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