简体   繁体   English

如何将 dict 打印为正确的格式?

[英]How do I print the dict to the correct format?

I previously used course_rolls(records) to make the data to a dictionary below:我之前使用course_rolls(records)将数据放入下面的字典中:

{'EMT001': {2286560}, 'FMKT01': {2547053}, 'CSC001': {2955520, 2656583}, 'MGM001': {2928707, 2606735}, 'MTH002': {2786372}, 'FCOM03': {2762453, 2955520, 2564885, 2606735}, 'FMCM02': {2955520, 2928707, 2656583}, 'ENG001': {2571096, 2564885}, 'MKT001': {2571096, 2656583},'AWA001': {2286560}, 'ACC002': {2762453}, 'FMTH01': {2571096}, 'EMT003': {2762453, 2656583}, 'MEA001': {2564885, 2606735}, 'FPHY01': {2564885}, 'FBIO01': {2547053}, 'MTH001': {2286560}, 'ECO002': {2928707, 2786372}, 'FCHM01': {2286560}, 'FCOM01': {2786372}, 'ENG002': {2762453}}

Records variable contains:记录变量包含:

[(('EMT001', 'Engineering Mathematics 1'), (2286560, 'Dayton', 'Archambault')), (('FMKT01', 'Marketing'), (2547053, 'Vladimir', 'Zemanek')), (('CSC001', 'Computer Programming'), (2656583, 'Ronny', 'Ridley')), (('MGM001', 'Fundamentals of Management'), (2928707, 'Susanne', 'Eastland')), (('MTH002', 'Mathematics 2'), (2786372, 'Danella', 'Crabe')), (('FCOM03', 'Introduction to Computing'), (2564885, 'Hpone', 'Ganadry')), (('FCOM03', 'Introduction to Computing'), (2762453, 'Phelia', 'Pottle')), (('FMCM02', 'Mass Communication II (Film Studies)'), (2656583, 'Ronny', 'Ridley')), (('ENG001', 'Foundations of Engineering'), (2564885, 'Hpone', 'Ganadry')), (('MKT001', 'Principles of Marketing'), (2571096, 'Shoshanna', 'Shupe')), (('AWA001', 'Engineering Writing Skills'), (2286560, 'Dayton', 'Archambault')), (('FCOM03', 'Introduction to Computing'), (2606735, 'Aaren', 'Enns')), (('ACC002', 'Financial Accounting'), (2762453, 'Phelia', 'Pottle')), (('FMTH01', 'Advanced Mathematics I'), (2571096, 'Shoshanna', 'Shupe')), (('FCOM03', 'Introduction to Computing'), (2955520, 'Bjorn', 'Kakou')), (('EMT003', 'Mathematical Modelling and Computation'), (2762453, 'Phelia', 'Pottle')), (('MEA001', 'Mixed English Programme'), (2564885, 'Hpone', 'Ganadry')), (('MGM001', 'Fundamentals of Management'), (2606735, 'Aaren', 'Enns')), (('MEA001', 'Mixed English Programme'), (2606735, 'Aaren', 'Enns')), (('FPHY01', 'Physics'), (2564885, 'Hpone', 'Ganadry')), (('FBIO01', 'Introduction to Biology'), (2547053, 'Vladimir', 'Zemanek')), (('ENG001', 'Foundations of Engineering'), (2571096, 'Shoshanna', 'Shupe')), (('MKT001', 'Principles of Marketing'), (2656583, 'Ronny', 'Ridley')), (('MTH001', 'Mathematics 1'), (2286560, 'Dayton', 'Archambault')), (('ECO002', 'Introduction to Macroeconomics'), (2786372, 'Danella', 'Crabe')), (('FCHM01', 'Chemistry'), (2286560, 'Dayton', 'Archambault')), (('FCOM01', 'Communication Skills II'), (2786372, 'Danella', 'Crabe')), (('FMCM02', 'Mass Communication II (Film Studies)'), (2928707, 'Susanne', 'Eastland')), (('CSC001', 'Computer Programming'), (2955520, 'Bjorn', 'Kakou')), (('ENG002', 'Engineering Mechanics and Materials'), (2762453, 'Phelia', 'Pottle')), (('EMT003', 'Mathematical Modelling and Computation'), (2656583, 'Ronny', 'Ridley')), (('FMCM02', 'Mass Communication II (Film Studies)'), (2955520, 'Bjorn', 'Kakou')), (('ECO002', 'Introduction to Macroeconomics'), (2928707, 'Susanne', 'Eastland'))]

The program input is: clashes CSC001程序输入为:crashes CSC001

Expected output (Command: clashes CSC001):预期 output(命令:冲突 CSC001):

Clashes with CSC001:
In CSC001 and EMT003
  2656583: 

In CSC001 and FCOM03
  2955520: 

In CSC001 and FMCM02
  2656583: 
  2955520: 

In CSC001 and MKT001
  2656583: 

The result I get (Command: clashes CSC001):我得到的结果(命令:冲突 CSC001):

Clashes with CSC001:
In CSC001 and FCOM03  
  2955520: 

In CSC001 and FMCM02  
  2955520: 

In CSC001 and EMT003  
  2656583: 

In CSC001 and FMCM02  
  2656583: 

In CSC001 and MKT001  
  2656583: 

Program code: (clashes function need to fix)程序代码:(冲突 function 需要修复)

def parse_course_code(commands, records):
    if commands in course_rolls(records).keys():
        print('Clashes with {}:'.format(commands))
        return clashes(commands, records)
    else:
        print("Unknown course code")


def clashes(course_code, records):
    unformatted_course_rolls = course_rolls(records)
    for course, prior_student_number in sorted(unformatted_course_rolls.items()):
        for prior_student_number_single in list(prior_student_number):
            if course_code == course:
                for code, student_number in sorted(unformatted_course_rolls.items()):
                    for num in list(student_number):
                        if prior_student_number_single == num and course != code:
                            print("In {} and {}  ".format(course, code))
                            print("  {}: \n".format(num))

def main():
    while True:
        command = input('Command: ')
        if command == 'quit':
            print('Adios')
            break
        else:
            parse_course_code(commands, records)
main()

The code you provided is very complicated to follow.您提供的代码非常复杂。 You should try to use less for loops - it makes the code harder to read, and takes longer to execute (if you were performing something of larger scale).您应该尝试使用较少的 for 循环 - 它使代码更难阅读,并且执行时间更长(如果您正在执行更大规模的操作)。 Take a look at my solution below, it prints the result as you would expect and is much easier to read and understand.看看下面我的解决方案,它会按照您的预期打印结果,并且更容易阅读和理解。

Here is the refactored code (note, since you didn't provide the course_roll function, I changed course_roll(records) for the variable course_rolls_records , so I could run it locally.):这是重构的代码(注意,由于您没有提供course_roll function,我将course_roll(records)更改为变量course_rolls_records ,所以我可以在本地运行它。):

def parse_course_code(commands):
    if commands in course_rolls_records.keys():
        print('Clashes with {}:'.format(commands))
        return clashes(commands)
    else:
        print("Unknown course code")


def clashes(course_code):
    numbers_in_course = course_rolls_records[course_code]
    collisions = {}
    for course_check in course_rolls_records.keys():
        numbers_in_course_check = course_rolls_records[course_check]
        if course_check == course_code:
            # This is the course we are querying, we don't need to do anything
            pass
        elif numbers_in_course_check.intersection(numbers_in_course):
            # We enter here if there are common student numbers between courses
            # Note: if there is no intersection, bool of empty set is False
            collisions[course_check] = list(numbers_in_course_check.intersection(numbers_in_course))
            print("In {} and {}  ".format(course_code, course_check))
            for num in collisions[course_check]:
                print("  {}: ".format(num))
            print('\n')

def main():
    while True:
        command = input('Command: ')
        if command == 'quit':
            print('Adios')
            break
        else:
            parse_course_code(command)

main()

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

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