简体   繁体   English

如何使用复杂列表中的数字作为标题/如何仅打印与某个值匹配的列表的一部分

[英]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: 每个列表构成一个集合的一部分(在程序的另一部分中,它一次读取3个值,如果您有2个加载的3个值,则将它们分别设置在一个集合中。例如,在我的第一个集合下面输入是:

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: 将其设置为“历史”变量,该变量将记住所有集合及其输入,直到清除为止,并存储为[[inputs] setnumber],其中外部项目是集合编号,内部项目是在该集合上接收到的输入:

[[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. 无论我多么努力或尝试多次if语句,while循环(这只会使事情变得更糟),我都无法使其表现出预期的效果。 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: 您可以使用itertools.groupby按“集合”数字对“输入”进行分组,如下所示:

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). 您的SetHistory的形状为(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. for Results, Set_Number in SetHistory:您只需在每次迭代中获取集合的编号,然后立即打印它。 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. 我建议您使用一个字典( https://docs.python.org/3/tutorial/datastructures.html?highlight=dictionary参见5.5)。

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]]                                    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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