小编frh*_*yme的帖子

如何在Python中取消函数?

最近,我使用标准ML研究了"编程语言" ,并且我学习了currying方法(或其他东西),所以我在Python中应用它.以下是简单的功能和currying.

def range_new(x, y):
    return [i for i in range(x, y+1)]

def curry_2(f):
    return lambda x: lambda y: f(x, y)

def uncurry_2(f):
    pass # I don't know it...

print(range_new(1, 10))
curried_range = curry_2(range_new)
countup = curried_range(1)
print(countup(10))
print(curried_range(1)(10))
Run Code Online (Sandbox Code Playgroud)

结果如下.它运作良好; 与curry_2我们可以做一个新的功能(countup).但是,我想做一个未经证实的功能.但是,我不知道如何才能做到.我该怎么做?

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Run Code Online (Sandbox Code Playgroud)

python functional-programming currying

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

gensim - fasttext - 为什么 `load_facebook_vectors` 不起作用?

我尝试从fastext - wiki 词向量加载预先训练的 FastText 向量。

我的代码如下,并且运行良好。

from gensim.models import FastText
model = FastText.load_fasttext_format('./wiki.en/wiki.en.bin')
Run Code Online (Sandbox Code Playgroud)

但是,警告消息有点烦人。

gensim_fasttext_pretrained_vector.py:13: DeprecationWarning: Call to deprecated `load_fasttext_format` (use load_facebook_vectors (to use pretrained embeddings)
Run Code Online (Sandbox Code Playgroud)

该消息表示,load_fasttext_format将被弃用,因此,最好使用load_facebook_vectors.

所以我决定更改代码。我更改的代码如下所示。

gensim_fasttext_pretrained_vector.py:13: DeprecationWarning: Call to deprecated `load_fasttext_format` (use load_facebook_vectors (to use pretrained embeddings)
Run Code Online (Sandbox Code Playgroud)

但是,错误发生了,错误信息是这样的。

Traceback (most recent call last):
  File "gensim_fasttext_pretrained_vector.py", line 13, in <module>
    model = FastText.load_facebook_vectors('./wiki.en/wiki.en.bin')
AttributeError: type object 'FastText' has no attribute 'load_facebook_vectors'
Run Code Online (Sandbox Code Playgroud)

我不明白为什么会发生这些事情。我只是改变了消息所说的内容,但它不起作用。如果您对此有所了解,请告诉我。

总是,谢谢你们的帮助。

python gensim fasttext

7
推荐指数
1
解决办法
4692
查看次数

有没有办法只使用`map`而不使用`flatMap`将2D列表转换为1D列表?

我是一名 Java 初学者,我刚刚学习了mapflatMap.

当 2d List 应该转换为 1d List 时,它是如下实现的。

List<List<Integer>> list_2d = List.of(List.of(1, 2), List.of(3, 4));

List<Integer> lst1 = list_2d
    .stream()
    .flatMap(arr -> arr.stream())
    .collect(Collectors.toList());
printAll(lst1); // [1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)

但是,我认为它看起来可以不使用flatMap.

有什么方法可以使代码具有相同的逻辑,只是使用map,而不是使用flatMap

只是问,因为如果map可以全部替换flatMap,就没有理由记住flatMap。我总是追求简单和基本的东西。

java list java-stream flatmap

5
推荐指数
3
解决办法
190
查看次数

是否可以保存在 Google Slides 中以高 DPI 绘制的绘图?

最近我写研究论文,所以,我需要高分辨率的图片。

我在 Google Slides 中完成了所有研究,但 Google Slides 不支持“另存为图片”功能。

此外,如果我复制在 Google 幻灯片中绘制的绘图并将其粘贴到 Google 文档中,它不会粘贴所有内容,而只是粘贴该绘图中的“文本”。

有什么方法可以在 Google 幻灯片中保存高 DPI 图片(如 png、jpg 等)的绘图吗?

感谢您的帮助。总是。

png jpeg google-slides

4
推荐指数
1
解决办法
6470
查看次数

Markdown - 为什么不建议在 Markdown 文档中插入原始 html 元素?

我以前都是用markdown写文章的。

当我在 markdown 文档中插入 html 元素时,markdownlint 告诉我该文档违反了 markdown 规则“MD033 - 内联 html”。

只要在 Markdown 文档中使用原始 HTML,就会触发规则“MD033 - 内联 html”。

但是,我不知道为什么 Markdown 文档中不允许内联 html 元素。

我在谷歌上搜索过,但找不到。

如果读过这篇文章的人知道这一点,请告诉我。

总是谢谢。

html markdown

3
推荐指数
1
解决办法
3312
查看次数

在 jupyter notebook 中读取文本文件:UnicodeDecodeError: codec can't decode byte xx

我的代码很简单。在 python 上处理字符串时,我不知道 unicode 的事情。伤心。

f = open("~161209.txt", "r")
f.read()
Run Code Online (Sandbox Code Playgroud)

我不知道如何修复此错误代码如下:

UnicodeDecodeError: 'cp949' codec can't decode byte 0xec in position 121: illegal multibyte sequence
Run Code Online (Sandbox Code Playgroud)

python string unicode python-3.x

2
推荐指数
1
解决办法
3872
查看次数

为什么'functools.reduce'和'itertools.chain.from_itertools'在嵌套列表合并时有不同的计算时间

有时你应该嵌套合并到合并列表(它类似于np.flatten())。当列表的列表如下所示时,您应该将其展平

a = [[j for j in range(0, 10)] for i in range(0, 10000)]
Run Code Online (Sandbox Code Playgroud)

你有两种解决方案来解决它。itertools.chain.from_iterablefunctools.reduce

%timeit list(itertools.chain.from_iterable(a))
%timeit reduce(lambda x, y: x+y, a)
Run Code Online (Sandbox Code Playgroud)

你认为哪一个更快,比其他东西快多少?

itertools.chain.from_iterable快 1000 倍或更多(当列表的长度较大时)。

如果有人知道为什么会发生这种情况,请告诉我。

始终感谢您的支持和帮助。

python reduce python-itertools python-3.x functools

2
推荐指数
1
解决办法
916
查看次数

python生成器太慢而无法使用它.我为什么要用呢?什么时候?

最近我有问题,哪一个是其中最跑得最快的家伙iterator,list comprehension,iter(list comprehension)generator.然后制作如下的简单代码.

n = 1000000
iter_a = iter(range(n))
list_comp_a = [i for i in range(n)]
iter_list_comp_a = iter([i for i in range(n)])
gene_a = (i for i in range(n))

import time
import numpy as np

for xs in [iter_a, list_comp_a, iter_list_comp_a, gene_a]:
    start = time.time()
    np.sum(xs)
    end = time.time()
    print((end-start)*100)
Run Code Online (Sandbox Code Playgroud)

结果如下.

0.04439353942871094 # iterator
9.257078170776367 # list_comprehension
0.006318092346191406 # iterator of list_comprehension
7.491207122802734 # generator 
Run Code Online (Sandbox Code Playgroud)

发电机比其他东西慢.我不知道什么时候有用?

python performance list-comprehension generator

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