简体   繁体   中英

Format the print statement in Python

I have a script that takes in a txt file with a list of attributes that go as input to the program. For each attribute, it prints out the attribute name if it satisfies the conditions of the script. However, each attribute may be satisfied multiple times and the output prints it many times. I'd like to do this now:

condition 1 satisfied by A,B,C,D
condition 2 satisfied by A,B
condition 3 satisfied by B,D

How do I do this? My current output looks like

A
A
condition 3 not satisfied by A
B
B
B
condition 1 not satisfied by C
condition 2 not satisfied by C
C
D
condition 2 not satisfied by D
D

how about this (in pseudo python code)

import collections
result = collections.defaultdict(list)
for attr in [A, B, C, D]:
  for cond in [condition1, condition2, ...]:
     if attr satisfies cond:
        result[cond].append(attr)

print result

Output:

{cond1: [A, B], cond2: [A], cond3: [C,D], ...}

I'm not sure that is what you want. You haven't stated clearly (enough) what your input looks like and what you want as output.

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