简体   繁体   English

我自己打印一个元组,但不打印其他变量

[英]I print a tuple on its own, but not with other variables

I am an experienced c programmer trying to come to terms with Python . 我是一位经验丰富的c程序员,试图熟悉Python

location is tuple and I can print with the following locationtuple ,我可以用以下命令打印

print(" %s %s" % (date_time, model))
print("Lat: %-.4f, Lon: %-.4f" % (location))

I tried to combine this into a single print, but get error TypeError: must be real number, not tuple with the following 我试图将其合并到单个打印中,但出现错误TypeError: must be real number, not tuple以下内容的TypeError: must be real number, not tuple

print("%s %s Lat: %-.4f, Lon: %-.4f" % (date_time, model, location))

I have tried several variations, without success. 我尝试了几种变体,但没有成功。

I can think of several ways of working around this (which I would do if I had to deliver a working program) but would like to understand an elegant method an experienced Python programmer would use. 我可以想到几种解决此问题的方法(如果必须交付一个有效的程序,我会这样做),但想了解经验丰富的Python程序员会使用的一种优雅方法。

Unpack your tuple. 打开元组的包装。

>>> date_time, model, location = 1, 2, (3, 4)
>>> print("%s %s Lat: %-.4f, Lon: %-.4f" % (date_time, model, *location))
1 2 Lat: 3.0000, Lon: 4.0000

pyformat.info is a helpful site that summarizes string formatting in Python. pyformat.info是一个有用的网站,总结了Python中的字符串格式。

In general, I suggest the usage of the new-style str.format over the % operator. 通常,我建议在%运算符上使用新型str.format Here's the relevant PEP. 这是相关的PEP。

You want to print the values in the tuple, not the tuple itself. 你想元组,而不是元组本身打印的值。 Either concatenate the tuple: 要么连接元组:

print("%s %s Lat: %-.4f, Lon: %-.4f" % ((date_time, model) + location)))

or interpolate the tuple values into the first tuple: 或将元组值插值到第一个元组中:

print("%s %s Lat: %-.4f, Lon: %-.4f" % (date_time, model, *location))

Personally, I'd not use printf formatting here at all . 就个人而言,我不是在这里用printf格式在所有 Use the more modern and powerful string formatting syntax , preferably in the form of the (very fast) formatted string literals : 使用更现代,更强大的字符串格式语法 ,最好以(非常快) 格式的字符串文字形式使用

print(f"{date_time} {model} Lat: {location[0]:-.4f}, Lon: {location[1]:-.4f}")

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

相关问题 在打印语句中标记元组变量 - Labeling tuple variables in print statement 我想在自己的值旁边打印每个 state - I want to print each state next to its own value 当需要其他变量时,如何解压缩元组? - How can I unpack tuple when other variables are needed? 如何使用 R 或 Python 将每个独特观察的所有数据打印到其自己的 PDF 或 CSV? - How can I print all data for each unique observation to its own PDF or CSV using R or Python? 如何在 python 中使用我自己的 class 中定义的变量正确打印句子 - how do I properly print a sentence using variables defined in my own class in python 如何从元组打印项目? - How can I print items from a tuple? 如果python中有任何其他元素匹配,则打印元组的第一个元素 - print first element of tuple if any other element matches in python 排列后如何从相邻的元组中打印值? - How to print values from a tuple next to each other after permutation? 当使用 popen 运行或在子进程库中运行时,如何在自己的行上打印 python 中的输入提示? - How can I print an input prompt in python on its own line when ran using popen or run in the subprocess library? 为什么我不能将两个变量附加为一个元组 - Why can I not append two variables as a tuple
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM