IPython Notebook:如何在不使用换行符的情况下显示()多个对象 [英] IPython Notebook: how to display() multiple objects without newline

查看:131
本文介绍了IPython Notebook:如何在不使用换行符的情况下显示()多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,当我在IPython笔记本中使用display()函数时,我在对象之间插入了换行符:

Currently when I use display() function in the IPython notebook I get newlines inserted between objects:

>>> display('first line', 'second line') 
first line
second line

但是我想要print()函数的行为,其中所有内容都保持在同一行,例如:

But I would like the print() function's behaviour where everything is kept on the same line, e.g.:

>>> print("all on", "one line")
all on one line 

是否有一种改变display行为的方法来做到这一点?

Is there a method of changing display behaviour to do this?

推荐答案

不,display无法阻止换行符,部分原因是没有防止换行的符.每个显示的对象都有自己的div可以坐在里面,并且它们垂直排列.您也许可以通过使用CSS进行调整来调整此设置,但我不建议这样做.

No, display cannot prevent newlines, in part because there are no newlines to prevent. Each displayed object gets its own div to sit in, and these are arranged vertically. You might be able to adjust this by futzing with CSS, but I wouldn't recommend that.

真正让两个对象并排显示的唯一方法是构建自己的对象,该对象封装多个显示的对象,然后显示该对象.

The only way you could really get two objects to display side-by-side is to build your own object which encapsulates multiple displayed objects, and display that instead.

例如,您的简单字符串大小写:

For instance, your simple string case:

class ListOfStrings(object):
    def __init__(self, *strings):
        self.strings = strings

    def _repr_html_(self):
        return ''.join( [
           "<span class='listofstr'>%s</span>" % s
           for s in self.strings
           ])

display(ListOfStrings("hi", "hello", "hello there"))

示例笔记本

这篇关于IPython Notebook:如何在不使用换行符的情况下显示()多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆