-1

I am working backwards on my current task. I have data formatted as:

qn1 = (["123 + 12", "234 + 23"], True)

I need to read the strings of equations and parse the individual words. My problem is that I cannot split the strings in the list, because the list is in a tuple.

My desired output would be:

Image is the output of >>> print(" 123     234\n""+ 12    + 23\n""====    ====").

My search so far took me to;

This other s/flow question; but that is not what am looking for

Please, do not solve the whole problem for me. I like the challenge, (and it is part of my freecodecamp exam)

I just need some pointers to get unstuck.

I have found that my question is different from the question How can I split and parse a string in Python? in that I am not just splitting a tuple, I am splitting a list inside a tuple, (to further split the string inside at spaces so that i can select the numbers and "+" sign inside to arrange them.)

Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
  • Look up how to unpack tuples and lists, then you can access the individual elements and split them. – L3viathan Oct 20 '20 at 16:14
  • ```qn1[0]``` gives you the first element of the tuple. ```qn1[0][0].split()``` give you ```['123', '+', '12']```, I hope that will help you. – Mark Oct 20 '20 at 16:25
  • Does this answer your question? [How can I split and parse a string in Python?](https://stackoverflow.com/questions/5749195/how-can-i-split-and-parse-a-string-in-python) – Christopher Peisert Oct 20 '20 at 16:25
  • hey @Mark, i still have problems because `print(qn1[0][0][0])` gives me `1`. How can i make so that i get `123` which is what i want? – Erick Kieti Oct 20 '20 at 16:52
  • @ChristopherPeisert i have really tried but i cannot get the answer from that particular question.thank you – Erick Kieti Oct 20 '20 at 16:57
  • @Erick Kieti ```qn1[0][0].split()[0]``` gets you ```123``` and ```qn1[0][0].split()[1]``` gets you ```+``` etc – Mark Oct 20 '20 at 17:12

2 Answers2

1

Start by getting the data from the tuple.

>>> qn1 = (["123 + 12", "234 + 23"], True)
>>> equations, flag = qn1
>>> equations
['123 + 12', '234 + 23']

Or you could use an index into the tuple:

>>> qn1 = (["123 + 12", "234 + 23"], True)
>>> equations = qn1[0]
>>> equations
['123 + 12', '234 + 23']

Next, split the equation into parts:

>>> for equation in equations:
...     tokens = equation.split()
...     print(tokens)
... 
['123', '+', '12']
['234', '+', '23']

To get started on parsing mathematical expressions see:
Evaluating a mathematical expression in a string

For ideas on formatting the output, see:
Create nice column output in python

Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
0

You're going to want to split up the strings into lists so you can put them into print statements

hopefully, this should challenge you a bit :^)

qn1 = (["123 + 12", "234 + 23"], True)

eq1 = []
eq2 = []

#try to get a list of the parts of that equation. E.g ["567", "+", "56"]

print(f"{eq1[0]}     {eq2[0]}")
print(f"{eq1[1]} {eq1[2]}   {eq2[1]} {eq2[2]}")
print(f"====    ====")
Ironkey
  • 2,568
  • 1
  • 8
  • 30
  • yeah, it is a little challenging and awesome too.thank you i've been working on it and am not through yet but its no longer a brick wall.so thanks – Erick Kieti Oct 20 '20 at 17:50