小编ger*_*jke的帖子

将包含单个字符串的列表列表展平为整数列表

我有一个包含字符串列表的列表看起来像

allyears
#[['1916'], ['1919'], ['1922'], ['1912'], ['1924'], ['1920']]
Run Code Online (Sandbox Code Playgroud)

我需要有这样的输出:

#[1916, 1919, 1922, 1912, 1924, 1920]
Run Code Online (Sandbox Code Playgroud)

一直在尝试这个:

for i in range(0, len(allyears)): 
    allyears[i] = int(allyears[i]) 
Run Code Online (Sandbox Code Playgroud)

但我有错误

>>> TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
Run Code Online (Sandbox Code Playgroud)

python

10
推荐指数
3
解决办法
378
查看次数

通过 google colab 使用 GPU 连接到本地运行时

我的目标是通过 jupyter 笔记本从本地计算机连接到 goocle colab gpu

从文档中完成了这些事情:

pip install jupyter_http_over_ws

jupyter serverextension enable --py jupyter_http_over_ws

使用以下命令打开 jupyter:

jupyter notebook --NotebookApp.allow_origin='https://colab.research.google.com' --port=8888 --NotebookApp.port_retries=0

当我从终端使用我的 url 连接到 colab 后,如下所示:

在此输入图像描述

之后我的终端里就有了这个

[I 18:12:04.374 NotebookApp] 302 GET /?token=HERE IS MY TOKEN (MY IP) 0.000000ms

其实我不知道是什么302 GET

最后,如果我os.getcwd()在 Colab 中使用,它会显示我电脑上的本地目录,并且print(torch.cuda.is_available())False

所以我已经从我的机器连接到 Colab,但目标是反之亦然,在我的本地机器上获取 GPU。所以也许我做错了什么。

python jupyter-notebook google-colaboratory

6
推荐指数
1
解决办法
1万
查看次数

在计数操作python中显示列表中的重复项

我有corpus_text文本字符串,然后我将其转换为带有单词分割的列表

我需要计算所有单词,但我的算法只计算唯一的

corpus_test = 'cat dog tiger tiger tiger cat dog lion'
corpus_test = [[word.lower() for word in corpus_test.split()]]
word_counts = defaultdict(int)
for rowt in corpus_test:
    for wordt in rowt:
        word_counts[wordt] += 1

        v_count = len(word_counts.keys())

        words_list = list(word_counts.keys())

        word_index = dict((word, i) for i, word in enumerate(words_list))

        index_word = dict((i, word) for i, word in enumerate(words_list))
Run Code Online (Sandbox Code Playgroud)

我想向你展示这个算法的输出

v_count
#4

words_list
#['cat', 'dog', 'tiger', 'lion']

word_counts
#defaultdict(int, {'cat': 2, 'dog': 2, 'tiger': 3, 'lion': 1})

word_index
#{'cat': 0, …
Run Code Online (Sandbox Code Playgroud)

python string dictionary python-3.x

5
推荐指数
1
解决办法
100
查看次数