Python Forum
I there a more efficient way of printing ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I there a more efficient way of printing ?
#1
Hi, I need that my values be printed in a standard way for a data logger and I'm trying to be printed like this :
(1, 2, 3)
It's sounded like a very simple thing for me but I haven't managed to deal whit it.
I looked on the net and I found the sep= function which allows you to separate with a point but I want my values to be printed with parentheses at the beginning and the end, how should I do that?

Since now I've come with that:
print(("("),  x , y, z, (")"), sep=",")
Not very efficient and not even working properly!
Reply
#2
Use string formatting in Python 3.6--> we did get f-string.
x = 5
y = 50
z = 999

print(f'{x, y, z}')
Output:
(5, 50, 999)
Capitaine_Flam likes this post
Reply
#3
(Nov-30-2020, 06:21 PM)snippsat Wrote: Use string formatting in Python 3.6--> we did get f-string.
x = 5
y = 50
z = 999

print(f'{x, y, z}')
Output:
(5, 50, 999)

Thank you! I will check this article!
Reply
#4
(Nov-30-2020, 08:10 PM)Capitaine_Flam Wrote:
(Nov-30-2020, 06:21 PM)snippsat Wrote: Use string formatting in Python 3.6--> we did get f-string.
x = 5
y = 50
z = 999

print(f'{x, y, z}')
Output:
(5, 50, 999)

Thank you! I will check this article!
I discovered that this function doesn't work on micropython so it's not working for my data logger, I should have mentioned it sorry about this.
Do you know another method that will work on micropython?

I saw this package https://pypi.org/project/future-fstrings/ but I'm not sure how to install it on my micropython chip which is a microbit, they are running a special version.
Reply
#5
(Nov-30-2020, 08:34 PM)Capitaine_Flam Wrote: Do you know another method that will work on micropython?
Try with .foramt() method,it should works as it was new in Python 2.6.
x = 5
y = 50
z = 999

print('({}, {}, {})'.format(x, y, z))
# Or
print('{}'.format((x, y, z)))
Output:
(5, 50, 999) (5, 50, 999)
Reply
#6
while f-strings and format will allow for much more custom print format, for what you want you can just print the tuple

x = 5
y = 50
z = 999
print((x, y, z))
Output:
(5, 50, 999)
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
#7
Complete different approach, not only about printing/representation. Requires Python 3.7+
The dataclass decorator makes a nice representation from a class and some other convenient stuff. In addition, usually you want to keep points together in a tuple, a sequence or mapping. If you have more than one point, it's better to keep x, y and z together in one object.

from dataclasses import dataclass

@dataclass
class Point:
    x: int
    y: int
    z: int


point1 = Point(1, 2, 3)
point2 = Point(x=-5, y=10, z=5)

print(point1)
print(point2)
If you want to sort Points by x, y and z, dataclass needs the extra argument order. The Argument frozen will prevent assigning new objects to the Point object. Then the dataclass behaves like a tuple.


from dataclasses import dataclass

@dataclass(order=True, frozen=True)
class Point:
    x: int
    y: int
    z: int


point1 = Point(1, 2, 3)
point2 = Point(x=-5, y=10, z=5)

sorted_points = sorted([point1, point2])
print(sorted_points)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
Instead of dataclass same can be achieved with collections.namedtuple
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  A more efficient code titanif 2 492 Oct-17-2023, 02:07 PM
Last Post: deanhystad
  Cleaning my code to make it more efficient BSDevo 13 1,360 Sep-27-2023, 10:39 PM
Last Post: BSDevo
  Making a function more efficient CatorCanulis 9 1,828 Oct-06-2022, 07:47 AM
Last Post: DPaul
  A more efficient way of comparing two images in a python vukan 0 2,019 Mar-17-2020, 11:39 AM
Last Post: vukan
  how to make iterative search more efficient renergy 2 2,268 Jan-03-2020, 03:43 PM
Last Post: stullis
  Simple problem. looking for an efficient way silverchicken24 3 2,325 Oct-14-2019, 07:13 PM
Last Post: Larz60+
  Most efficient way to define sub keys of a dictionary? wrybread 1 2,105 Feb-21-2019, 12:23 AM
Last Post: snippsat
  Efficient way of iterating a list of records anguis 4 3,039 Feb-19-2019, 03:39 AM
Last Post: scidam
  Is there a more efficient way to code this project? gkiranseo 1 2,687 Sep-25-2018, 09:51 AM
Last Post: Larz60+
  Computationally efficient recording + searching? IAMK 1 2,352 Apr-30-2018, 01:23 AM
Last Post: IAMK

Forum Jump:

User Panel Messages

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