Python Forum
Python grouping program - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python grouping program (/thread-4029.html)



Python grouping program - GhostZero199 - Jul-18-2017

I try to make a program that sorts words into predifined groups. The thing is how do I make it show me the content that had been transfered to those groups? Thank You!

Update: I did this program but I want to make it with multiple posibilities. "if" statement returns nothing for print.

ConstantinEduard = ('blablabla')

print ("this is a test, input name")

last = input("Numele de familie")
first = input("Prenumele")

print (ConstantinEduard)


RE: Python grouping program - DeaD_EyE - Jul-18-2017

Your solution is here:
https://docs.python.org/3.6/library/functions.html#sorted
https://docs.python.org/3/library/itertools.html#itertools.groupby

import itertools


def bycase(word):
    if word[0].isupper():
        return 'uppercase'
    else:
        return 'lowercase'

def byfirstletter(word):
    return word[0].lower()

words = ['foo', 'bar', 'Baz', 'baz', 'Bar']

# now sort bycase
words_sorted = sorted(words, key=bycase)
for key, group in itertools.groupby(words_sorted, bycase):
    print('Key:', key)
    for word in group:
        print('Word:', word)

# now sort byfirstletter
words_sorted = sorted(words, key=byfirstletter)
for key, group in itertools.groupby(words_sorted, byfirstletter):
    print('Key:', key)
    for word in group:
        print('Word:', word)
The more interesting part is, when you have a list like a table.
For example you have a table with cars and one row has the color.
Sorting by color example:

from itertools import groupby
from operator import itemgetter

#example data
##### col0,  col1, col2
cars = [('BMW', '318i', 'blue'),
     ('BMW', '323i', 'blue'),
     ('BMW', '318i', 'black'),
     ('BMW', '323i', 'blue'),
     ('BMW', '323i', 'black'),
     ('BMW', '320i', 'silver'),
     ('BMW', '318i', 'silver'),
     ('BMW', '318i', 'black'),
     ('BMW', '323i', 'silver'),
     ('BMW', '320i', 'silver'),
     ('BMW', '323i', 'black'),
     ('BMW', '318i', 'blue'),
     ('BMW', '323i', 'blue'),
     ('BMW', '323i', 'blue'),
     ('BMW', '323i', 'blue')]

sorted_cars = sorted(cars, key=itemgetter(2)) # sort by color
for color, group in groupby(sorted_cars, itemgetter(2)): # group by color
    print('Color:', color)
    for manufacturer, model, color in group:
        print(manufacturer, model, color)
Hope it helps. Next time you should post example data.

Ok, you edited you original post, my answer is useless in this topic.


RE: Python grouping program - sparkz_alot - Jul-18-2017

(Jul-18-2017, 06:26 AM)GhostZero199 Wrote: "if" statement returns nothing for print.

What 'if' statement?. You need to post your code (between code tags), output (between output tags) and any errors, in their entirety (between error tags).