Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem with print lists
#1
>>> pprint(response.choices)
Output:
[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))]
i try print and pprint and response is as above ( other lists also the same)
How can i print this in readable format?
Reply
#2
Please use code tags when posting code. This is hard to read.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
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)
Reply
#4
(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))?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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??
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with print command in super() akbarza 5 607 Feb-01-2024, 12:25 PM
Last Post: deanhystad
  problem with spliting line in print akbarza 3 413 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  Problem with print variable in print.cell (fpdf) muconi 0 670 Dec-25-2022, 02:24 PM
Last Post: muconi
Question How to print multiple elements from multiple lists in a FOR loop? Gilush 6 2,972 Dec-02-2020, 07:50 AM
Last Post: Gilush
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,387 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  C to Python code conversion print problem anakk1n 1 2,198 May-22-2020, 04:15 PM
Last Post: deanhystad
  Highscore problem, need to print top 5 Camiosobro123 5 4,893 Jan-29-2020, 03:31 PM
Last Post: ThiefOfTime
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,294 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Newbie lists problem LedDiode 5 3,673 Dec-16-2018, 08:33 PM
Last Post: LedDiode
  Problem with Lists in Python DEAD_BOT 2 3,077 Aug-26-2018, 06:09 AM
Last Post: DEAD_BOT

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020