Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string of digits into lists of even and odd integers

Tags:

python

Having such code

numbers = '1 2 3 4 5 6 7 8'
nums = {'evens': [], 'odds': []}

for number in numbers.split(' '):
    if int(number) % 2:
        nums['odds'].append(number)
    else:
        nums['evens'].append(number)

How can accomplish same on fewer lines?

like image 923
micgeronimo Avatar asked Jul 19 '16 07:07

micgeronimo


People also ask

How do you separate even and odd numbers in a list?

Step 1 : create a user input list. Step 2 : take two empty list one for odd and another for even. Step 3 : then traverse each element in the main list. Step 4 : every element is divided by 2, if remainder is 0 then it's even number and add to the even list, otherwise its odd number and add to the odd list.

How do you split a string into numbers?

To split a string into a list of integers: Use the str. split() method to split the string into a list of strings. Use the map() function to convert each string into an integer.

How do you write a program for odd and even numbers in Python?

num = int (input (“Enter any number to test whether it is odd or even: “) if (num % 2) == 0: print (“The number is even”) else: print (“The provided number is odd”) Output: Enter any number to test whether it is odd or even: 887 887 is odd. The program above only accepts integers as input.


1 Answers

Short code is not better code. Short code is not faster code. Short code is not maintainable code. Now, that said, it is good to make your individual components concise and simple.

Here's what I would do:

def split_odd_even(number_list):
    return {
        'odds': filter(lambda n: (n % 2) != 0, number_list),
        'evens': filter(lambda n: (n % 2) == 0, number_list)
    }

def string_to_ints(string):
    return map(int, numbers.strip().split())

numbers = '1 2 3 4 5 6 7 8 9 10'
nums = split_odd_even(string_to_ints(numbers))

print nums

This gives me:

{'odds': [1, 3, 5, 7, 9], 'evens': [2, 4, 6, 8, 10]}

While this code has actually added a few lines in length, it has become much more clear what the program is doing, as we've applied Abstraction and made each component of the code do only one thing well.

Even though we've added two functions, the most-visible part of the code has gone from this:

numbers = '1 2 3 4 5 6 7 8'
nums = {'evens': [], 'odds': []}
for number in numbers.split(' '):
    if int(number) % 2:
        nums['odds'].append(number)
    else:
        nums['evens'].append(number)

To this:

numbers = '1 2 3 4 5 6 7 8 9 10'
nums = split_odd_even(string_to_ints(numbers))

And just by reading these two lines, we know that numbers is converted from a string to a list of ints, and that we then split those numbers into odd and even, and assign the result to nums.

To explain a a couple of things that may not be familiar to all:

  • map() calls a function for every item in a list (or tuple or other iterable), and returns a new list with the result of the function being called on each item. In this case, we use it to call int() on each item in the list.

  • filter() calls a function for every item in a list (or tuple or other iterable), which returns True or False for each item (well, truthy or falsey), and returns a list of items that evaluated to True when the function is called.

  • Lambda Expressions (lambda) are like "mini-functions" that take arguments and can be created in-place.

like image 108
Will Avatar answered Nov 14 '22 22:11

Will