1

Let's say I have a list like this:

[['John', 1], ['Fred', 1], ['Carolyn', 1], ['Kenneth', 3], ['Ronald', 3], ['Dorothy', 2], ['Joyce', 2], ['Julia', 3], ['Deborah', 1], ['Jonathan', 2], ['Aaron', 2], ['Marie', 1], ['Adam', 2], ['Kevin', 2], ['Alice', 2], ['Jerry', 1], ['Kimberly', 1], ['Lawrence', 1], ['Louis', 2], ['Anthony', 1], ['Carolyn', 3], ['Edward', 2], ['Samuel', 3], ['Rachel', 1], ['Kathleen', 1], ['Fred', 3], ['Fred', 3], ['Gerald', 2], ['Donna', 2], ['Keith', 3], ['Matthew', 3], ['Stephanie', 1]]

How can I get this output:

John Dorothy Kenneth
Fred Joyce Ronald
Carolyn Jonathan Julia
Deborah Aaron Carolyn
Marie Adam Samuel
Jerry Kevin Fred
Kimberly Alice Fred
Lawrence Louis Keith
Anthony Edward Matthew

What I'm trying to do is group up in groups of 3 elements. If a group is not made up of 3 names, it will not be displayed.

Another example:

Input:

[['Heather', 2], ['Evelyn', 1], ['Norma', 1], ['Evelyn', 3], ['Harry', 3], ['Sean', 1], ['Anna', 1], ['Jerry', 3], ['Anna', 3], ['Julia', 1], ['Dorothy', 2]]

Expected output:

Evelyn Heather Evelyn
Norma Dorothy Harry

Until now, I only managed to group their names into sub-lists according to each number (1, 2, 3).

r = [[] for i in range(3)]
for i in l:
    if i[1] == 1:
        r[0].append(i[0])
    elif i[1] == 2:
        r[1].append(i[0])
    elif i[1] == 3:
        r[2].append(i[0])
print r

r = [['John', 'Fred', 'Carolyn', 'Deborah', 'Marie', 'Jerry', 'Kimberly', 'Lawrence', 'Anthony', 'Rachel', 'Kathleen', 'Stephanie'], ['Dorothy', 'Joyce', 'Jonathan', 'Aaron', 'Adam', 'Kevin', 'Alice', 'Louis', 'Edward', 'Gerald', 'Donna'], ['Kenneth', 'Ronald', 'Julia', 'Carolyn', 'Samuel', 'Fred', 'Fred', 'Keith', 'Matthew']]
kode master
  • 100
  • 1
  • 15
dun.py
  • 73
  • 1
  • 7
  • 1
    You can see this link I think it help you..https://stackoverflow.com/questions/5695208/group-list-by-values?r=SearchResults – Anshu Jun 15 '19 at 18:36

7 Answers7

2

Here is an approach similar to this question :

l1 = [['John', 1], ['Fred', 1], ['Carolyn', 1], ['Kenneth', 3], ['Ronald', 3], ['Dorothy', 2], ['Joyce', 2], ['Julia', 3], ['Deborah', 1], ['Jonathan', 2], ['Aaron', 2], ['Marie', 1], ['Adam', 2], ['Kevin', 2], ['Alice', 2], ['Jerry', 1], ['Kimberly', 1], ['Lawrence', 1], ['Louis', 2], ['Anthony', 1], ['Carolyn', 3], ['Edward', 2], ['Samuel', 3], ['Rachel', 1], ['Kathleen', 1], ['Fred', 3], ['Fred', 3], ['Gerald', 2], ['Donna', 2], ['Keith', 3], ['Matthew', 3], ['Stephanie', 1]]
l2 = [['Heather', 2], ['Evelyn', 1], ['Norma', 1], ['Evelyn', 3], ['Harry', 3], ['Sean', 1], ['Anna', 1], ['Jerry', 3], ['Anna', 3], ['Julia', 1], ['Dorothy', 2]]

def get_exp(l):
    v = set(map(lambda x:x[1], l))
    nl = [[y[0] for y in l if y[1]==x] for x in v]
    return '\n'.join(list(map(' '.join, zip(*nl))))
output_l1 = get_exp(l1)
output_l2 = get_exp(l2)

output_l1 :

John Dorothy Kenneth
Fred Joyce Ronald
Carolyn Jonathan Julia
Deborah Aaron Carolyn
Marie Adam Samuel
Jerry Kevin Fred
Kimberly Alice Fred
Lawrence Louis Keith
Anthony Edward Matthew

output_l2 :

Evelyn Heather Evelyn
Norma Dorothy Harry
Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
1

how about :

l = [['Heather', 2], ['Evelyn', 1], ['Norma', 1], ['Evelyn', 3], ['Harry', 3], ['Sean', 1], ['Anna', 1], ['Jerry', 3], ['Anna', 3], ['Julia', 1], ['Dorothy', 2]]
from itertools import groupby
def keyfunc(arr) :
   return arr[1]

l = sorted(l, key=keyfunc)
s =[[*x,] for  i,x in groupby(data , keyfunc)]
combinations = [*zip(*s),]

and then you could just print out the elements by doing :

for l in combinations :
   print(' '.join([x[0] for x in l]))

prints out :

John Dorothy Kenneth
Fred Joyce Ronald
Carolyn Jonathan Julia
Deborah Aaron Carolyn
Marie Adam Samuel
Jerry Kevin Fred
Kimberly Alice Fred
Lawrence Louis Keith
Anthony Edward Matthew
Ayoub ZAROU
  • 2,387
  • 6
  • 20
1

You can do this pretty succinctly with a combination of zip and itertools.groupby. First, sort the list by the number, then group and zip. If you want strings you can then join:

from operator import itemgetter
from itertools import groupby

l = [['John', 1], ['Fred', 1], ['Carolyn', 1], ['Kenneth', 3], ['Ronald', 3], ['Dorothy', 2], ['Joyce', 2], ['Julia', 3], ['Deborah', 1], ['Jonathan', 2], ['Aaron', 2], ['Marie', 1], ['Adam', 2], ['Kevin', 2], ['Alice', 2], ['Jerry', 1], ['Kimberly', 1], ['Lawrence', 1], ['Louis', 2], ['Anthony', 1], ['Carolyn', 3], ['Edward', 2], ['Samuel', 3], ['Rachel', 1], ['Kathleen', 1], ['Fred', 3], ['Fred', 3], ['Gerald', 2], ['Donna', 2], ['Keith', 3], ['Matthew', 3], ['Stephanie', 1]]

l.sort(key = itemgetter(1))
groups = zip(*([name for name, g in n] for k, n in groupby(l, itemgetter(1))))

[" ".join(names) for names in groups]

output:

['John Dorothy Kenneth',
 'Fred Joyce Ronald',
 'Carolyn Jonathan Julia',
 'Deborah Aaron Carolyn',
 'Marie Adam Samuel',
 'Jerry Kevin Fred',
 'Kimberly Alice Fred',
 'Lawrence Louis Keith',
 'Anthony Edward Matthew']
Mark
  • 90,562
  • 7
  • 108
  • 148
0

I think the easiest answer is to use a dictionary to gather results by the number you have (which will be the dictionary key). Then you can screen the results held in the dictionary by length:

In [7]: from collections import defaultdict                                     

In [8]: results = defaultdict(list)                                             

In [9]: name_list = [['bob', 1], ['cindy', 1], ['ted', 2]]                      

In [10]: for (value, key) in name_list: 
    ...:     results[key].append(value) 
    ...:                                                                        

In [11]: results                                                                
Out[11]: defaultdict(list, {1: ['bob', 'cindy'], 2: ['ted']})



In [13]: for key in results: 
    ...:     if len(results.get(key)) == 2: 
    ...:         print( 'found a result of length 2: ', results.get(key)) 
    ...:                                                                        
found a result of length 2:  ['bob', 'cindy']
AirSquid
  • 10,214
  • 2
  • 7
  • 31
0
lst1 = [['John', 1], ['Fred', 1], ['Carolyn', 1], ['Kenneth', 3], ['Ronald', 3], ['Dorothy', 2], ['Joyce', 2], ['Julia', 3], ['Deborah', 1], ['Jonathan', 2], ['Aaron', 2], ['Marie', 1], ['Adam', 2], ['Kevin', 2], ['Alice', 2], ['Jerry', 1], ['Kimberly', 1], ['Lawrence', 1], ['Louis', 2], ['Anthony', 1], ['Carolyn', 3], ['Edward', 2], ['Samuel', 3], ['Rachel', 1], ['Kathleen', 1], ['Fred', 3], ['Fred', 3], ['Gerald', 2], ['Donna', 2], ['Keith', 3], ['Matthew', 3], ['Stephanie', 1]]
lst2 = [['Heather', 2], ['Evelyn', 1], ['Norma', 1], ['Evelyn', 3], ['Harry', 3], ['Sean', 1], ['Anna', 1], ['Jerry', 3], ['Anna', 3], ['Julia', 1], ['Dorothy', 2]]

from itertools import groupby

def my_print(lst):
    d = {v: list(g) for v, g in groupby(sorted(lst, key=lambda k: k[-1]), lambda v: v[-1])}
    while True:
        try:
            i1 = d[1].pop(0)
            i2 = d[2].pop(0)
            i3 = d[3].pop(0)
            print('{} {} {}'.format(i1[0], i2[0], i3[0]))
        except IndexError:
            break

my_print(lst1)
print('*' * 80)
my_print(lst2)

Prints:

John Dorothy Kenneth
Fred Joyce Ronald
Carolyn Jonathan Julia
Deborah Aaron Carolyn
Marie Adam Samuel
Jerry Kevin Fred
Kimberly Alice Fred
Lawrence Louis Keith
Anthony Edward Matthew
********************************************************************************
Evelyn Heather Evelyn
Norma Dorothy Harry
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
0

Group names by second elements into a dictionary and then zipped them together.

def gum(l):
    g = {}
    for n, k in l:
        g.setdefault(k, []).append(n)
    return zip(*g.values())

l1 = [['John', 1], ['Fred', 1], ['Carolyn', 1],
      ['Kenneth', 3], ['Ronald', 3], ['Dorothy', 2],
      ['Joyce', 2], ['Julia', 3], ['Deborah', 1],
      ['Jonathan', 2], ['Aaron', 2], ['Marie', 1],
      ['Adam', 2], ['Kevin', 2], ['Alice', 2],
      ['Jerry', 1], ['Kimberly', 1], ['Lawrence', 1],
      ['Louis', 2], ['Anthony', 1], ['Carolyn', 3],
      ['Edward', 2], ['Samuel', 3], ['Rachel', 1],
      ['Kathleen', 1], ['Fred', 3], ['Fred', 3],
      ['Gerald', 2], ['Donna', 2], ['Keith', 3],
      ['Matthew', 3], ['Stephanie', 1]]

l2 = [['Heather', 2], ['Evelyn', 1], ['Norma', 1],
      ['Evelyn', 3], ['Harry', 3], ['Sean', 1],
      ['Anna', 1], ['Jerry', 3], ['Anna', 3],
      ['Julia', 1], ['Dorothy', 2]]

print '\n\n'.join('\n'.join(' '.join(n) for n in l) for l in [gum(l1), gum(l2)])

Output:

John Dorothy Kenneth
Fred Joyce Ronald
Carolyn Jonathan Julia
Deborah Aaron Carolyn
Marie Adam Samuel
Jerry Kevin Fred
Kimberly Alice Fred
Lawrence Louis Keith
Anthony Edward Matthew

Evelyn Heather Evelyn
Norma Dorothy Harry
Andrei Odegov
  • 2,925
  • 2
  • 15
  • 21
0

Using numpy is another way to do it:

import math
import numpy as np

data = [['John', 1], ['Fred', 1], ['Carolyn', 1], ['Kenneth', 3], ['Ronald', 3], ['Dorothy', 2], ['Joyce', 2], ['Julia', 3], ['Deborah', 1], ['Jonathan', 2], ['Aaron', 2], ['Marie', 1], ['Adam', 2], ['Kevin', 2], ['Alice', 2], ['Jerry', 1], ['Kimberly', 1], ['Lawrence', 1], ['Louis', 2], ['Anthony', 1], ['Carolyn', 3], ['Edward', 2], ['Samuel', 3], ['Rachel', 1], ['Kathleen', 1], ['Fred', 3], ['Fred', 3], ['Gerald', 2], ['Donna', 2], ['Keith', 3], ['Matthew', 3], ['Stephanie', 1]]

array = np.array([x[0] for x in data])
array = np.resize(array,(3,math.ceil(len(array)/3))).T

[" ".join(x) for x in array]

Output:

['John Marie Samuel',
 'Fred Adam Rachel',
 'Carolyn Kevin Kathleen',
 'Kenneth Alice Fred',
 'Ronald Jerry Fred',
 'Dorothy Kimberly Gerald',
 'Joyce Lawrence Donna',
 'Julia Louis Keith',
 'Deborah Anthony Matthew',
 'Jonathan Carolyn Stephanie',
 'Aaron Edward John']
Sebastien D
  • 4,369
  • 4
  • 18
  • 46