0

I am working on a problem wherein I paste a set of numbers, and I want to put the even and odd list elements from these numbers and put them in their own list, then add them together for a 3rd list.

Here is my code:

#list of numbers
start = """ 
601393 168477
949122 272353
944397 564134
406351 745395
281988 610822
451328 644000
198510 606886
797923 388924
470601 938098
578263 113262
796982 62212
504090 378833
"""

x = start.split()
#turn string into list elements then append to a list
step_two = []

step_two.append(x)

print step_two
# doublecheck that step_two is a list with all the numbers
step_three = step_two[1::2]
#form new list that consists of all the odd elements
print step_three
#form new list that consists of all the even elements
step_four = step_two[0::2]

print step_four
#add ascending even/odd element pairs together and append to new list
final_step = [x + y for x, y in zip(step_three, step_four)]

print final_step

This code yields these results:

""" "Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved.

[['601393', '168477', '949122', '272353', '944397', '564134', '406351', '745395' , '281988', '610822', '451328', '644000', '198510', '606886', '797923', '388924' , '470601', '938098', '578263', '113262', '796982', '62212', '504090', '378833'] ]

[]

[['601393', '168477', '949122', '272353', '944397', '564134', '406351', '745395' , '281988', '610822', '451328', '644000', '198510', '606886', '797923', '388924' , '470601', '938098', '578263', '113262', '796982', '62212', '504090', '378833'] ]

[]

"""

Why is my list in step_three and step_four not working? I am not sure why my [::] functions aren't registering.

Thanks in advance for any advice.

5 Answers5

4

You were too explicit here in step two:

x = start.split()
#turn string into list elements then append to a list
step_two = []

step_two.append(x)

x is already the list you need. step_two creates a new list and adds the previous list to it, so instead of ['601393', '168477',...], you have [['601393', '168477',...]].

To fix this, simply call the split string step_two and proceed from there:

step_two = start.split()
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
1

The reason your lists don't split the numbers into odd and even is that your code assumes that the list alternates between them - you take every other index for each list generation, but the numbers aren't arranged that way in the original string. You'll need to do an 'evenness' test:

step_two = start.split()
step_three = []
step_four = []

for item in step_two:
    if int(item) % 2: # If it divides evenly by two, it returns 0, or False
        step_three.append(item)
    else:
        step_four.append(item)

The lists step_three and step_four will now correctly contain only odds or evens.

Blackbeard
  • 355
  • 2
  • 7
0

Here is how I would generate the odd list, hope it helps!

import sys

# Provided main(), calls mimic_dict() and mimic()
def main():



#list of numbers
  start = '601393 168477 949122 272353 944397 564134 406351 745395 281988 610822 451328 644000 198510 606886 797923 388924470601 938098 578263 113262 796982 62212 504090 378833'

  x = start.split()
    #turn string into list elements then append to a list
  step_two = []
  # gives you 23 items in your list "step_two" instead of 1 item with original append on it's own
  for s in x:
   step_two.append(s)

  #print (step_two)

 # print (step_two)
# doublecheck that step_two is a list with all the numbers
  i = 1
  step_three_odd_list = []
  while i < len(step_two):
    currentodditem = step_two[i]
    step_three_odd_list.append(currentodditem)
    i = i + 2
#form new list that consists of all the odd elements
  print (step_three_odd_list)


if __name__ == '__main__':
  main()
Joe Goeman
  • 1
  • 1
  • 4
0

The [::] that you have going on are extended slices, not tests for even and odd: https://docs.python.org/release/2.3.5/whatsnew/section-slices.html

Also, since those numbers are in a string, when you split them you end up with strings, not integers that you test with math. So, you need to convert them to integers.

This post clued me in on a quick even/odd test: Check if a number is odd or even in python

This code seems to do what you want, if I understand it correctly:

start = """ 
601393 168477
949122 272353
944397 564134
406351 745395
281988 610822
451328 644000
198510 606886
797923 388924
470601 938098
578263 113262
796982 62212
504090 378833
"""

# List comprehension to generate a list of the numbers converted to integers
step_two = [int(x) for x in start.split()]
# Sort the list
step_two.sort()

# Create new lists for even and odd
even_numbers = []
odd_numbers = []

# For each item in the list, test for even or odd and add to the right list
for number in step_two:
    if (number % 2 == 0):
         #even
         even_numbers.append(number)
    else:
        #odd
        odd_numbers.append(number)

print(even_numbers)
print(odd_numbers)

final_step = [x + y for x, y in zip(even_numbers, odd_numbers)]

print(final_step)    
Community
  • 1
  • 1
Matt B.
  • 1
  • 1
0

You can use list comprehensions to create two lists odd and even :

import itertools
start = """ 
601393 168477
949122 272353
944397 564134
406351 745395
281988 610822
451328 644000
198510 606886
797923 388924
470601 938098
578263 113262
796982 62212
504090 378833
"""
lst = start.split()
even = [int(i) for i in lst if int(i) % 2 == 0]
odd = [int(i) for i in lst if int(i) % 2 != 0]

Then you can zip these 2 lists but as even and odd are not the same length, you have to use itertools.zip_longest() in Python 3 and itertools.izip_longest() in Python 2. You will then have a list of tuples:

final_step = [i for i  in itertools.zip_longest(even,odd)]
[(949122, 601393),
 (564134, 168477),
 (281988, 272353),
 (610822, 944397),
 (451328, 406351),
 (644000, 745395),
 (198510, 797923),
 (606886, 470601),
 (388924, 578263),
 (938098, 378833),
 (113262, None),
 (796982, None),
 (62212, None),
 (504090, None)]

You can create the third list result to sum() the values using an other list comprehension but you have to make sure not to try to sum the tuples where one value is None.

result = [sum(e) if e[1] is not None else e[0] for e in final_step]

The final output:

[1550515,
 732611,
 554341,
 1555219,
 857679,
 1389395,
 996433,
 1077487,
 967187,
 1316931,
 113262,
 796982,
 62212,
 504090]
Joe T. Boka
  • 6,554
  • 6
  • 29
  • 48