Hexo

凡事预则立,不预则废


  • Home

  • Tags

  • Archives

  • Navigation

  • Search

Ubuntu-Jupyter——修改默认路径

Ubuntu下修改Jupyter的默认路径

  • 经测试,按照本文的方法修改后不一定生效
  • 一个比较优雅的方法是每次进入指定路径,从该路径启动jupyter notebook
    • 这种方法可确保Jupyter根目录为指定目录

查看配置文件

  • 首先查看是已经生成Jupyter的配置文件,默认新装的Jupyter根目录为~/, 是没有配置文件的
    1
    2
    # 查看配置文件命令
    cat ~/.jupyter/jupyter_notebook_config.py

生成配置文件

  • 如果没有配置文件,则首先需要生成新的配置文件

    1
    2
    # 生成配置文件命令
    jupyter notebook --generate-config
    • root用户执行时可能需要按照要求加上参数

编辑配置文件有两种方式

相对路径方式

打开配置文件修改相对目录c.NotebookApp.default_url,这里默认为/tree, 修改成自己想要的地址即可

1
2
3
# /tree表示当前路径pwd, 如下则表示当前终端路径下的/Coding路径
c.NotebookApp.default_url = '/tree/Coding'
# 这种情况以后打开Jupyter时都需要到指定的文件目录下才行,适用于多个jupyter路径的情况

一般使用这种情况时建议直接保留相对目录/tree即可,这样可以在启动Jupyter时直接在那个文件目录下启动

绝对路径方式

打开配置文件配置绝对目录c.NotebookApp.notebook_dir

1
2
# 这种情况下无论在哪里打开都是绝对目录 	
c.NotebookApp.notebook_dir = u'/home/sanye/JupyterWorkspace'

如果两种情况都配置了,默认以第一中情况为主,因为第一个是redirect的
如果两种情况都没配置,默认等价于配置了c.NotebookApp.default_url = '/tree'

Python——list返回最大的K个元素的索引

自定义的一个有用的Python方法


返回最大的K个值的索引

引用自Stack Overflow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def get_top_k_indexes_of_list(target_list, k, is_max=True, min_value=None):
"""
get the top k indexes of elements in list
Example:
Problem: I have a list say a = [5,3,1,4,10],
and I need to get a index of top two values of the list viz 5 and 10.
Is there a one-liner that python offers for such a case?
Usage: get_top_k_indexes_of_list(target_list=a, k=2, is_max=True)
link: https://stackoverflow.com/questions/13070461/get-index-of-the-top-n-values-of-a-list-in-python
:param target_list: target list
:param k: the number of indexes
:param is_max: True means max else False means min
:param min_value: if min_value is not None
filter the indexes whose value less than or equals to min_value
:return: a list of indexes
"""
indexes = sorted(range(len(target_list)), key=lambda i: target_list[i], reverse=is_max)[:k]
result = list()
if min_value is not None:
for index in indexes:
if target_list[index] <= min_value:
break
result.append(index)
else:
result = indexes
return result

一次性索引出多个元素

1
2
3
4
5
6
7
8
9
def get_elements_from_list(target_list, indexes):
"""
get elements from target_list by indexes
:param target_list: target list
:param indexes: a list of indexes
:return: a list of elements
"""
elements = [target_list[i] for i in indexes]
return elements
1…334335336…352
San Ye

San Ye

Stay Hungry. Stay Foolish.

704 posts
53 tags
© 2026 San Ye
Powered by Hexo
|
Theme — NexT.Gemini v5.1.4