Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python grouping program
#1
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)
Reply
#2
Your solution is here:
https://docs.python.org/3.6/library/func...tml#sorted
https://docs.python.org/3/library/iterto...ls.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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
(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).
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Grouping Data based on 30% bracket purnima1 4 1,142 Mar-10-2023, 07:38 PM
Last Post: deanhystad
  Grouping and sum of a list of objects Otbredbaron 1 3,132 Oct-23-2021, 01:42 PM
Last Post: Gribouillis
  Grouping and summing of dataset jef 0 1,608 Oct-04-2020, 11:03 PM
Last Post: jef
  Grouping algorithm riccardoob 7 2,931 May-19-2020, 01:22 PM
Last Post: deanhystad
  Help Grouping by Intervals on list paul41 1 2,072 Dec-03-2019, 09:43 PM
Last Post: michael1789
  Grouping a list of various time into intervals paul41 1 3,731 Nov-24-2019, 01:47 PM
Last Post: buran
  resample grouping pr0blem olufemig 1 1,924 Nov-06-2019, 10:45 PM
Last Post: Larz60+
  Splitting lines ang grouping three at once samsonite 5 2,718 Jun-21-2019, 05:19 PM
Last Post: ichabod801
  Grouping csv by name terrydidi 8 3,832 Jan-14-2019, 09:27 AM
Last Post: perfringo
  Function for grouping variables Scott 1 2,673 Nov-13-2018, 03:01 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020