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?
#1
Hi,

I have this input
P (0, 2,+1) (2, 0, -1) (0, 4,+1) (4, 0, -1)
and I would like to have it printed out this way
[(0, 2, 1), (2, 0, -1), (0, 4, 1), (4, 0, -1)]
. However, due to the extra space in the input I ran into this error. Without making any change to the input, I wonder if anyone could advise? Thanks
    algorithm_type_2 = list(eval(user_input_2))
  File "<string>", line 1
    (0,,2,+1),(2,,0,,-1),(0,,4,+1),(4,,0,,-1)
       ^
SyntaxError: invalid syntax
user_input = input().split()
# input_list = user_input.split()
# algorithm_type = 'X'
algorithm_type = user_input.pop(0)

user_input_2 = ','.join(user_input)
algorithm_type_2 = list(eval(user_input_2))
print(user_input_2)
print(algorithm_type_2)
Reply
#2
Looks like you want to replace ") (" with "), (". That is not a very robust way of processing input, but it will work for your specific case.
Reply
#3
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)]
longmen likes this post
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Exercise list remove duplicates RavCOder 9 5,288 Oct-23-2019, 04:16 PM
Last Post: jefsummers
  Removing extra space sumncguy 4 2,848 Jun-07-2019, 09:16 PM
Last Post: sumncguy
  Remove special character from list vestkok 3 4,234 Nov-04-2018, 01:48 PM
Last Post: ichabod801
  "List index out of range" for output values pegn305 3 5,310 Nov-26-2017, 02:20 PM
Last Post: heiner55
  Functions to remove vowels and extra blanks RiceGum 10 5,877 Nov-17-2017, 05:40 PM
Last Post: heiner55
  How to keep duplicates and remove all other items in list? student8 1 4,947 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