![]() |
problem with print lists - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: problem with print lists (/thread-41291.html) |
problem with print lists - MarekGwozdz - Dec-14-2023 >>> pprint(response.choices) i try print and pprint and response is as above ( other lists also the same)How can i print this in readable format? RE: problem with print lists - menator01 - Dec-14-2023 Please use code tags when posting code. This is hard to read. RE: problem with print lists - deanhystad - Dec-14-2023 Quote:Please use code tags when posting code. This is hard to read.I believe that's the point. The part that is hard to read is the output of the pprint command. The problem has nothing to do with lists. The list only contains a single item. The problem is that the item is not printing prettily. You could try printing the item instead of the list. That would call __str__ for the item instead of __repr__. This might result in prettier output. for choice in response.choices: print(choice) RE: problem with print lists - buran - Dec-15-2023 (Dec-14-2023, 03:04 PM)MarekGwozdz Wrote: How can i print this in readable format?What exactly is considered "readable" format in this case (list of Choice object instance(s))? RE: problem with print lists - Pedroski55 - Dec-15-2023 How about some added newlines? mystring = """[Choice(finish_reason='length', index=0, message=ChatCompletionMessage(content='The time complexity of the code is O(n * k), where n is the value of parameter `n` and k is the value of parameter `k`. This is because there are two nested loops, one iterating `n` times and the other iterating `k` times. The accum variable is incremented `n *', role='assistant', function_call=None, tool_calls=None))]"""Then pprint (sounds like a stutter!) pprint(mystring) ("[Choice(finish_reason='length', index=0, " "message=ChatCompletionMessage(content='The time complexity of the code is " 'O(n * k), where n is the value of parameter `n` and k is the value of ' 'parameter `k`. This is because there are two nested loops, one iterating `n` ' 'times and the other iterating `k` times. The accum variable is incremented ' "`n *', role='assistant', function_call=None, tool_calls=None))]")Add some newlines: newstring = mystring.replace(', ', ',\n').replace('. ', '.\n') pprint(newstring) ("[Choice(finish_reason='length',\n" 'index=0,\n' "message=ChatCompletionMessage(content='The time complexity of the code is " 'O(n * k),\n' 'where n is the value of parameter `n` and k is the value of parameter `k`.\n' 'This is because there are two nested loops,\n' 'one iterating `n` times and the other iterating `k` times.\n' "The accum variable is incremented `n *',\n" "role='assistant',\n" 'function_call=None,\n' 'tool_calls=None))]')Readable?? |