Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python grouping program
#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


Messages In This Thread
Python grouping program - by GhostZero199 - Jul-18-2017, 06:26 AM
RE: Python grouping program - by DeaD_EyE - Jul-18-2017, 07:36 AM
RE: Python grouping program - by sparkz_alot - Jul-18-2017, 12:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Grouping Data based on 30% bracket purnima1 4 1,289 Mar-10-2023, 07:38 PM
Last Post: deanhystad
  Grouping and sum of a list of objects Otbredbaron 1 3,367 Oct-23-2021, 01:42 PM
Last Post: Gribouillis
  Grouping and summing of dataset jef 0 1,697 Oct-04-2020, 11:03 PM
Last Post: jef
  Grouping algorithm riccardoob 7 3,197 May-19-2020, 01:22 PM
Last Post: deanhystad
  Help Grouping by Intervals on list paul41 1 2,249 Dec-03-2019, 09:43 PM
Last Post: michael1789
  Grouping a list of various time into intervals paul41 1 3,856 Nov-24-2019, 01:47 PM
Last Post: buran
  resample grouping pr0blem olufemig 1 2,048 Nov-06-2019, 10:45 PM
Last Post: Larz60+
  Splitting lines ang grouping three at once samsonite 5 2,902 Jun-21-2019, 05:19 PM
Last Post: ichabod801
  Grouping csv by name terrydidi 8 4,084 Jan-14-2019, 09:27 AM
Last Post: perfringo
  Function for grouping variables Scott 1 2,768 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