Python Forum
How to remove extra space from output list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to remove extra space from output list?
#4
Thank you. That answers my question.
(May-05-2022, 10:21 AM)ibreeden Wrote: Deanhystad is right, it is not a very robust way of processing input. But if you can be sure the first letter is followed by a space you can use split() to isolate this first letter. But you must use parameter "maxsplit=1" to prevent it to also split the rest of your input.
user_input = "P (0, 2,+1) (2, 0, -1) (0, 4,+1) (4, 0, -1)".split(maxsplit=1)
print(user_input)
Output:
['P', '(0, 2,+1) (2, 0, -1) (0, 4,+1) (4, 0, -1)']
Then you have a list of two strings. You can pop the first element to "algorithm_type" as you did and then you have a list of one string for further processing.
You need to identify the parts of the string between parentheses. You can use "re" (regular expressions) to isolate these parts. Regular expressions are not easy to read so I will explain the pattern you need.
r"(\(.*?\))"
  1. r"..." : pattern must be treated as a raw string.
  2. (..) : the parentheses capture the string that you need.
  3. \(...\) : search for a string inside parentheses. These parentheses must be escaped by backslashes or else they will be interpreted as capturing parentheses.
  4. . : dot means: match any character.
  5. * : asterisk means: previous character (the dot) repeated zero or more times.
  6. ? : question mark means: do a non-greedy (reluctant) match, so match the closest pair of parentheses. (Or else everything from the first "(" to the last ")" is captured as one string.)

import re

x = re.findall(r"\(.*?\)", "(0, 2,+1) (2, 0, -1) (0, 4,+1) (4, 0, -1)")
print(x)
Output:
['(0, 2,+1)', '(2, 0, -1)', '(0, 4,+1)', '(4, 0, -1)']
So you get a list of strings. If you are sure the strings contain tuples, you can use eval() to make tuples of the strings, like you did.
algorithm_type_2 = []
for y in x:
    algorithm_type_2.append(eval(y))
print(algorithm_type_2)
Output:
[(0, 2, 1), (2, 0, -1), (0, 4, 1), (4, 0, -1)]
Reply


Messages In This Thread
RE: How to remove extra space from output list? - by longmen - May-05-2022, 11:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Exercise list remove duplicates RavCOder 9 5,326 Oct-23-2019, 04:16 PM
Last Post: jefsummers
  Removing extra space sumncguy 4 2,888 Jun-07-2019, 09:16 PM
Last Post: sumncguy
  Remove special character from list vestkok 3 4,286 Nov-04-2018, 01:48 PM
Last Post: ichabod801
  "List index out of range" for output values pegn305 3 5,342 Nov-26-2017, 02:20 PM
Last Post: heiner55
  Functions to remove vowels and extra blanks RiceGum 10 5,922 Nov-17-2017, 05:40 PM
Last Post: heiner55
  How to keep duplicates and remove all other items in list? student8 1 4,976 Oct-28-2017, 05:52 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

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