Jupyter Notebook折叠输出的内容实例

一、问题描述

当Jupyter Notebook的输出内容很多时,为了屏幕可以显示更多的代码行,我需要将输出的内容进行折叠。

二、解决方法

1、鼠标操作

(1)鼠标左键双击输出单元格的左侧灰色区域。

(2)展开:鼠标左键单机下方的灰色区域即可。如下图所示:

2、快捷键操作

(1)按Esc键

(2)按字母O

(3)展开:同上。

补充知识:Python 找出出现次数超过数组长度一半的元素实例

利用问题的普遍性和特殊性来求解,代码如下:

import unittest
from datetime import datetime

class GetFreqNumbersFromList(unittest.TestCase):
  def setUp(self):
    print("\n")
    self.start_time = datetime.now()
    print(f"{self._testMethodName} start: {self.start_time}")

  def tearDown(self):
    self.end_time = datetime.now()
    print(f"{self._testMethodName} end: {self.end_time}")
    exec_time = (self.end_time - self.start_time).microseconds
    print(f"{self._testMethodName} exec_time: {exec_time}")

  def normal_solution(self, _list, _debug=False):
    """
    普遍性解法
    利用字典记录每个元素出现的次数——然后找出元素出现次数超过数组长度一半的元素
    普遍性解法针对任何次数的统计均适用而不光只是针对出现次数超过数组长度一半的情况
    """
    _target = len(_list) // 2
    _dict = {}
    for _member in _list:
      if _member not in _dict:
        _dict.setdefault(_member, 1)
      else:
        _dict[_member] += 1
    _ret = [_member for _member in _dict if _dict[_member] > _target]
    if _debug:
      print(_ret)
    return _ret

  def specific_solution(self, _list, _debug=False):
    """
    特殊性解法
    假设有两个元素出现的次数都超过数组长度一半就会得出两个元素出现的次数超出了数组长度的矛盾结果——所以超过数组长度一半的元素是唯一的
    排序后在数组中间的一定是目标解
    特殊性解法只能针对元素出现次数超过数组长度一半的情况
    """
    _list.sort()
    if _debug:
      print(_list[len(_list) // 2])
    return _list[len(_list) // 2]

  def test_normal_solution(self):
    actual_result = self.normal_solution([2,2,2,2,2,2,1,1,1,1,1], False)
    self.assertEqual(actual_result[0], 2)

  def test_specific_solution(self):
    actual_result = self.specific_solution([2,2,2,2,2,2,1,1,1,1,1], False)
    self.assertEqual(actual_result, 2)

if __name__ == "__main__":
  # 找出出现次数超过数组长度一半的元素
  suite = unittest.TestSuite()
  suite.addTest(GetFreqNumbersFromList('test_normal_solution'))
  suite.addTest(GetFreqNumbersFromList('test_specific_solution'))
  runner = unittest.TextTestRunner()
  runner.run(suite)

测试结果:

在一篇文章看到这个LeetCode上的问题,自己动手写写♪(・ω・)ノ

以上这篇Jupyter Notebook折叠输出的内容实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 解决Jupyter notebook中.py与.ipynb文件的import问题

    在jupyter notebook中,因为其解析文件的方式是基于json的,所以其默认保存的文件格式不是.py而是.ipynb.而.ipynb文件并不能简单的import进.py或者.ipynb文件中,这就为开发带来了极大不便.因为在jupyter notebook中,一定要是在默认的.ipynb下才能有一系列的特性支持,比如自动补全,控制台等待,而.py文件只能通过文本编辑器修改,非常非常不便. 因为.ipynb可以import .py的module,所以其中一个解决方法是将已经写好的.ipy

  • jupyter notebook 的工作空间设置操作

    Jupyter notebook 安装后,启动后,默认的工作空间是当前用户目录.为了方便对文档进行管理,往往需要自行设置工作空间.下面介绍一种便捷的工作空间设置方法. 对 Jupyter notebook 快捷方式进行修改.右击 jupyter notebook 快捷方式 -> 属性 -> 把"目标"中的 %USERPROFILE% 替换成你想要的目录,eg:D:\python-workspace. 接下来双击 Jupyter notebook 运行,就可以见证效果. 补充

  • jupyter notebook 添加kernel permission denied的操作

    为什么要手动添加核? 因为使用公司的服务器,最好不要直接使用anaconda自带的python,更不要使用系统下自带的python,如果每个人都使用同一个python,可能会给别人的工作带来"致命的伤害". 怎么添加? 正常情况: python -m ipykernel install --name your_env_name (your_env_name 代表你的python环境的名字) 如果出现 error13 permiss denied:/usr/local/share/jup

  • 浅谈JupyterNotebook导出pdf解决中文的问题

    1.将ipynd编译成tex 建议将其放在桌面处理 ipython nbconvert -to latex pdf.ipynb 2.修改tex 双击打开转换的文件在\documentclass{article}后面插入 \usepackage{fontspec, xunicode, xltxtra} \setmainfont{Microsoft YaHei} \usepackage{ctex} 3.编译tex 生成pdf xelate pdf.tex 补充知识:Jupyter notebook

  • Jupyter Notebook的连接密码 token查询方式

    换用非默认浏览器时需要输入密码或token 查询方法: 在XX:\AnacondaXX\Scripts下 运行 jupyter-notebook.exe list 可得token 密码:(设成了用不了..???) 在jupyter notebook正常开的文件里打 in[1]from notebook.auth import passwd in[2]passwd() 补充知识:Anaconda3中自带Jupyter notebook如何查找token 最近在使用Anaconda3学习tensor

  • jupyter notebook tensorflow打印device信息实例

    juypter notebook中直接使用log_device_placement=True打印不出来device信息 # Creates a graph. with tf.device('/device:CPU:0'): a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a') b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name

  • 快速解决jupyter notebook启动需要密码的问题

    jupyter notebook安装完成之后需要密码,还有某些情况下也会出现需要输入密码的情况 解决方法如下: 1.在运行界面输入 jupyter notebook list 2.之后运行界面会输出token值,将其复制到密码栏中 补充知识:Python 遇到NameError: name '_name_' is not defined这样的错误 今天练习写Python主函数的时候,遇到了NameError: name 'name' is not defined 这样的错误.>因为name是一个

  • Jupyter Notebook折叠输出的内容实例

    一.问题描述 当Jupyter Notebook的输出内容很多时,为了屏幕可以显示更多的代码行,我需要将输出的内容进行折叠. 二.解决方法 1.鼠标操作 (1)鼠标左键双击输出单元格的左侧灰色区域. (2)展开:鼠标左键单机下方的灰色区域即可.如下图所示: 2.快捷键操作 (1)按Esc键 (2)按字母O (3)展开:同上. 补充知识:Python 找出出现次数超过数组长度一半的元素实例 利用问题的普遍性和特殊性来求解,代码如下: import unittest from datetime im

  • jupyter notebook中美观显示矩阵实例

    我就废话不多说了,还是直接看代码吧! from IPython.display import display,Latex,Math %matplotlib inline from IPython.core.interactiveshell import InteractiveShell sh = InteractiveShell.instance() def number_to_str(n,cut=5): ns=str(n) format_='{0:.'+str(cut)+'f}' if 'e'

  • jupyter notebook清除输出方式

    在 jupyter notebook参数化运行python时,怕输出太多文件太大,想及时清除 notebook 的输出. 在别人代码里看到用 easydl 的 clear_output().调用很简单: from easydl import clear_output print('before') clear_output() # 清除输出 print('after') 查它源码:clear_output def clear_output(): """ clear outpu

  • ipython jupyter notebook中显示图像和数学公式实例

    1. # 可以使用LaTeX表示数学公式 # 可以使用LaTeX表示数学公式 from IPython.display import Latex Latex(r"$\sqrt{x^2+y^2}$") 2. # SymPy的表达式也可以显示为LaTex %load_ext sympyprinting from sympy import * x, y = symbols("x,y") sqrt(x**2+y**2) 3. # 用Image类显示"jupyter

  • 查看jupyter notebook每个单元格运行时间实例

    打开jupyter notebook, 进入这儿: 搜索框里搜索time,并选中Execute Time,大功告成!!! 最后是这样的, 很方便有木有(如果不行可以尝试重启一下jupyter notebook). 之前有见过其他方法: 命令行里输入: pip install jupyter_contrib_nbextensions jupyter contrib nbextension install --user jupyter nbextension enable execute_time/

  • 解决jupyter notebook 前面书写后面内容消失的问题

    在使用jupyter notebook时,如果想改中间的内容,但有时你会发现后面的内容就没有了,不见了, 你需要重写这一行代码,这费时间. 这其实是设置的问题,在word中也会出现这种情况,是'改写'问题. 在jupyter notebook中,你只要按键盘上的 insert 键就解决这个问题了. 补充知识:Jupyter 重新导入修改后的自定义包 Jupyter 经常遇到这样一个问题,就是在已有的 notebook 中导入了自定义的 itools.py 包文件,但是在编辑 notebook 中

  • Jupyter Notebook输出矢量图实例

    相信大家都很熟悉在 Jupyter Notebook 上面用 Matplotlib 了,但是不知道大家看到画出来那一坨糊糊的东西会不会跟我一样浑身难受.实际上,只要多加一行配置,就能够让 Matplotlib 在 Jupyter Notebook 上面输出矢量图了: import matplotlib import matplotlib.pyplot as plt %matplotlib inline %config InlineBackend.figure_format = 'svg' 上面的

  • Jupyter notebook 输出部分显示不全的解决方案

    在我更换了jupyter主题后,输出部分总是显示不全,差两个字符:Github上已经有人提出了这个问题,并有了解决方案,亲测有效. 在这个路径,打开custom文件夹 打开custom.css文件: replace the current div.output_area with the following in the custom css file: div.output_area { display: -webkit-box; padding: 13px; } 这个13px,可能有的人改了

  • jupyter notebook实现显示行号

    Jupyter Notebook默认不显示行号,可是当我们代码报错时,发现会显示自己多少行出现错误. eg: 这时候我们总不能一行行去数吧,因此,为了方便我们调试排错.我们需要让jupyter notebook显示行号.具体方法如下: 菜单栏View------Toggle Line Numbers 就这么简单..... 补充知识:解决jupyter notebook在输出行数太大时出现滚动条滑动窗口 而不一次性显示全部输出 暂时发现有两种方式: 1.鼠标点击方式: 先选中代码单元; 然后点击菜

随机推荐