简体   繁体   中英

How do I use a number from a complex list as a heading / How do I print only part of a list that matches a certain value

I have a complex list that contains another list. Each list forms part of a set (In another part of the program it reads in 3 values at a time, if you have 2 loads of 3 values it then sets them each up in a single set. So for example below my first set of inputs were:

12, 11, 1
12, 6, 2
8, 7, 1

then my second set of input was:

6, 6, 1

So effectively each input would be part of a set:

Set 1:
12, 11, 1
12, 6, 2
8, 7, 1

Set 2:
6, 6, 1

This gets set to a "History" variable that remembers all the sets and their inputs until cleared, stored as a [[inputs]setnumber] where the outer item is the set number and the inner items are the inputs that were received on that set:

[[12, 11, 1], 1]
[[12, 6, 2], 1]
[[8, 7, 1], 1]
[[6, 6, 1], 2]

What I need to do is print them out in a format of:

Starting with set 1:
12, 11, 1
12, 6, 2
8, 7, 1

Starting with set 2: 
6, 6, 1

Instead, what I get is:

Starting with set 1
The Following appear:12,11,1
Starting with set 1
The Following appear:12,6,2
Starting with set 1
The Following appear:8,7,1
Starting with set 2
The Following appear:6,6,1

No matter how hard I try or how many times I play around with some if statements, while loops (This one just makes things worse) I cannot get it to behave like I expect it to. Currently my base code is below.

I am so sure I am missing something simple or using the wrong type of object, but for the life of me the solution has eluded me and is driving me mad.

SetHistory = [[[12, 11, 1], 1], [[12, 6, 2], 1], [[8, 7, 1], 1], [[6, 6, 1], 2]]

for Results, Set_Number in SetHistory:
    UnpackResults = [Results]
    UnpackSet = [Set_Number]
    for i in UnpackSet:
        print(f'Starting with set {Set_Number}')
        for i, v, x in UnpackResults:
            print(f'The Following appear:{i},{v},{x} ')

Let's solve this using a dictionary.

SetHistory = [[[12, 11, 1], 1], [[12, 6, 2], 1], [[8, 7, 1], 1], [[6, 6, 1], 2]]

set_dict = {}
for Results, Set_Number in SetHistory:
    if Set_Number in set_dict: 
        set_dict[Set_Number].append(Results)
    else:
        set_dict[Set_Number] = [Results]
for a_set in sorted(set_dict): 
    print('Set '+str(a_set))
    for val_list in set_dict[a_set]:
        print(*val_list)

This way, each entry in a dictionary is a set number. It contains a list of lists for that number, so you know what the set number before you process each list.

You could use itertools.groupby to group the "inputs" by your "sets" numbers like this:

from itertools import groupby
from operator import itemgetter


set_history = [[[12, 11, 1], 1], [[12, 6, 2], 1], [[8, 7, 1], 1], [[6, 6, 1], 2]]

for key, group in groupby(set_history, key=itemgetter(1)):
    print(f'Starting with set {key}:')
    for item in group:
        print(*item[0], sep=", ")

gives:

Starting with set 1:
12, 11, 1
12, 6, 2
8, 7, 1
Starting with set 2:
6, 6, 1

Using your approach, you could solve that by storing temporarily the complete "set" (it's actually a list here):

SetHistory = [[[12, 11, 1], 1], [[12, 6, 2], 1], [[8, 7, 1], 1], [[6, 6, 1], 2]]

UnpackResults = []
Set_Number_old = 1
for Results, Set_Number in SetHistory:
    if Set_Number_old == Set_Number:
        UnpackResults.append(Results)
    else:
        print('Starting with set {}'.format(Set_Number_old))
        for i, v, x in UnpackResults:
            print('The Following appear:{},{},{} '.format(i, v, x))

        Set_Number_old = Set_Number
        UnpackResults = [Results]

print('Starting with set {}'.format(Set_Number_old))
for i, v, x in UnpackResults:
    print('The Following appear:{},{},{} '.format(i, v, x))

Your SetHistory is of shape (4,2). in for Results, Set_Number in SetHistory: you just get the the number of the set in every iteration and then you immediately print it. You actually want to store all the results corresponding to that specific set and print it once you get to the next set. I recommend to use a dictionary ( https://docs.python.org/3/tutorial/datastructures.html?highlight=dictionary see 5.5) for your case.

Here's my approach based on dictionary:

d = dict()
for res, n in SetHistory:
    d[n] = d.get(n, []) + [res]

for k, v in d.items():
    print(f'set {k}:')
    print(v)

# set 1:
# [[12, 11, 1], [12, 6, 2], [8, 7, 1]]                       
# set 2:                                                     
# [[6, 6, 1]]                                    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM