0

I've been working on a problem that involves taking multiple number pairs, and creating some form of sum loop that adds each pair together.

I am not getting the correct number of outputs, e.g. 15 pairs of numbers are inputted and only 8 are coming out.

Here's my code so far...

data = "917128 607663\
907859 281478\
880236 180499\
138147 764933\
120281 410091\
27737 932325\
540724 934920\
428397 637913\
879249 469640\
104749 325216\
113555 304966\
941166 925887\
46286 299745\
319716 662161\
853092 455361"

data_list = data.split(" ") # creating a list of strings

data_list_numbers = [] # converting list of strings to list of integers 
for d in data_list:
    data_list_numbers.append(int(d))

#splitting the lists into two with every other integer (basically to get the pairs again.
list_one = data_list_numbers[::2]   
list_two = data_list_numbers[1::2]

zipped_list = zip(list_one, list_two) #zipping lists 

sum = [x+y for x,y in zip(list_one, list_two)] # finding the sum of each pair

print(sum)

What am I missing?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Ben Smith
  • 13
  • 1
  • What do you see if you look at the output of `print(data)`? Is it what you expect? – mkrieger1 Jul 05 '21 at 18:49
  • Does this answer your question? [How to create string with line breaks in Python?](https://stackoverflow.com/questions/51568874/how-to-create-string-with-line-breaks-in-python) – mkrieger1 Jul 05 '21 at 18:53
  • I ran `print(data)` and because there were no spaces before the ` \ `, every other number was being combined. I've adjusted this and it's now resulting in the correct number of outputs! Thanks so much! – Ben Smith Jul 05 '21 at 18:54
  • in your data, \ means the number is going to continue in next line. Add a space before all \ – thavan Jul 05 '21 at 19:03
  • That's correct and fixed the problem. I also added, `for num in sum: print(num)` to print answers line by line. – Ben Smith Jul 05 '21 at 19:19

1 Answers1

1

Quote the input string like so: """...""", remove the backslashes, and use re.split to split on whitespace. Note that using backslashes without spaces, as you did, causes the numbers in data to smash into each other. That is, this:

"607663\
907859"

is the same as: "607663907859".

import re

data = """917128 607663
907859 281478
880236 180499
138147 764933
120281 410091
27737 932325
540724 934920
428397 637913
879249 469640
104749 325216
113555 304966
941166 925887
46286 299745
319716 662161
853092 455361"""

data_list = re.split(r'\s+', data) # creating a list of strings

data_list_numbers = [] # converting list of strings to list of integers 
for d in data_list:
    data_list_numbers.append(int(d))

#splitting the lists into two with every other integer (basically to get the pairs again.
list_one = data_list_numbers[::2]   
list_two = data_list_numbers[1::2]

zipped_list = zip(list_one, list_two) #zipping lists 

sum = [x+y for x,y in zip(list_one, list_two)] # finding the sum of each pair

print(sum)
# [1524791, 1189337, 1060735, 903080, 530372, 960062, 1475644, 1066310, 1348889, 429965, 418521, 1867053, 346031, 981877, 1308453]
Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
  • 1
    Much better, I withdraw my objection. But I will note that `re.split` is overkill, regular `str.split` works great if you just leave off the parameter - the default is a combination of all whitespace characters, including newlines. – Mark Ransom Jul 05 '21 at 19:30
  • This is a much better way of solving this. Are you able to explain the regular expression formation `r'\s+'` ? I understand the `\s` is finding the white space? – Ben Smith Jul 05 '21 at 19:35
  • 1
    @BenSmith Yes, `data_list = re.split(r'\s+', data)` is splitting `data` on 1 or more whitespace (which includes space, newline, etc). But also see the comment above from Mark Ransom: you can simply use `data_list = data.split()`, and avoid `import re` – Timur Shtatland Jul 05 '21 at 19:58