Jupyter Notebook折疊輸出的內(nèi)容實例
一、問題描述
當(dāng)Jupyter Notebook的輸出內(nèi)容很多時,為了屏幕可以顯示更多的代碼行,我需要將輸出的內(nèi)容進(jìn)行折疊。

二、解決方法
1、鼠標(biāo)操作
(1)鼠標(biāo)左鍵雙擊輸出單元格的左側(cè)灰色區(qū)域。

(2)展開:鼠標(biāo)左鍵單機下方的灰色區(qū)域即可。如下圖所示:

2、快捷鍵操作
(1)按Esc鍵

(2)按字母O

(3)展開:同上。
補充知識:Python 找出出現(xiàn)次數(shù)超過數(shù)組長度一半的元素實例
利用問題的普遍性和特殊性來求解,代碼如下:
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):
"""
普遍性解法
利用字典記錄每個元素出現(xiàn)的次數(shù)——然后找出元素出現(xiàn)次數(shù)超過數(shù)組長度一半的元素
普遍性解法針對任何次數(shù)的統(tǒng)計均適用而不光只是針對出現(xiàn)次數(shù)超過數(shù)組長度一半的情況
"""
_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):
"""
特殊性解法
假設(shè)有兩個元素出現(xiàn)的次數(shù)都超過數(shù)組長度一半就會得出兩個元素出現(xiàn)的次數(shù)超出了數(shù)組長度的矛盾結(jié)果——所以超過數(shù)組長度一半的元素是唯一的
排序后在數(shù)組中間的一定是目標(biāo)解
特殊性解法只能針對元素出現(xiàn)次數(shù)超過數(shù)組長度一半的情況
"""
_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__":
# 找出出現(xiàn)次數(shù)超過數(shù)組長度一半的元素
suite = unittest.TestSuite()
suite.addTest(GetFreqNumbersFromList('test_normal_solution'))
suite.addTest(GetFreqNumbersFromList('test_specific_solution'))
runner = unittest.TextTestRunner()
runner.run(suite)
測試結(jié)果:

在一篇文章看到這個LeetCode上的問題,自己動手寫寫♪(・ω・)ノ
以上這篇Jupyter Notebook折疊輸出的內(nèi)容實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
tensorflow 獲取checkpoint中的變量列表實例
今天小編就為大家分享一篇tensorflow 獲取checkpoint中的變量列表實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
使用GitHub和Python實現(xiàn)持續(xù)部署的方法
這篇文章主要介紹了使用GitHub和Python實現(xiàn)持續(xù)部署的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05
Python的SimpleHTTPServer模塊用處及使用方法簡介
這篇文章主要介紹了Python的SimpleHTTPServer模塊用處及使用方法簡介,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
python爬蟲開發(fā)之使用Python爬蟲庫requests多線程抓取貓眼電影TOP100實例
這篇文章主要介紹了python爬蟲開發(fā)之使用Python爬蟲庫requests多線程抓取貓眼電影TOP100實例,需要的朋友可以參考下2020-03-03

