I think this code is very straightforward and easy to read. Good job!
Some nitpicks are,
You have a few PEP8 violations none all that concerning.
- Should add a second white line between imports and your functions
- Line too long, at your
printstatement - Continuation line under-indented for visual indent
printfstyle formatting is recommended against
Code suggestions
I would use a namedtuple instead of a dictionary, since it is much cleaner looking and easier to use. When to use a namedtuple!
Using the above suggestions and adhering PEP8 I get the following.
from re import split
from collections import namedtuple
def get_messages():
Message = namedtuple('Message', 'idx num length message')
messages = []
while True:
try:
recv = split(r'\s+', input())
yield messages.append(Message(recv[0], recv[1], recv[2], ' '.join(recv[3:])))
except EOFError:
breakreturn messages
def mainprint_messages(messages):
packetsfor =message []in messages:
for packet in get_messages():
print("{} {} {} {}"
packets.append(packet)
packets = sorted(packets, key=lambda x: .format(xmessage.idx, xmessage.num, message.length, message.message))
for packet in packets:
def printmain("{}): {} {} {}"
messages = sorted(get_messages(), key=lambda x: .format(packetx.idx, packetx.num,))
packet.length, packet.message) print_messages(messages)
if __name__ == '__main__':
main()