小编pt1*_*lol的帖子

在ipython笔记本中绘制宽度设置

我有以下情节:

声音信号

如果它们具有相同的宽度,它看起来会更好.你知道在我使用ipython笔记本时怎么做%matplotlib inline吗?

更新:

为了生成这两个数字,我使用以下函数:

import numpy as np
import matplotlib.pyplot as plt

def show_plots2d(title, plots, points, xlabel = '', ylabel = ''):
    """
    Shows 2D plot.

    Arguments:
        title : string
            Title of the plot.
        plots : array_like of pairs like array_like and array_like
            List of pairs,
            where first element is x axis and the second is the y axis.
        points : array_like of pairs like integer and integer
            List of pairs,
            where first element is x coordinate …
Run Code Online (Sandbox Code Playgroud)

python matplotlib ipython ipython-notebook

74
推荐指数
3
解决办法
11万
查看次数

在单个会话中运行 VSCode 任务

我想将本地部署命令迁移到 VSCode 任务。我需要以 root 身份运行它们。当我在终端中执行这些操作时,系统只要求我提供一次密码。

$ sudo echo hello                                                                                                                                                                                                                                                        
[sudo] password for pt12lol: 
hello
$ sudo echo hello                                                                                                                                                                                                                                                        
hello
Run Code Online (Sandbox Code Playgroud)

不幸的是,当我配置 VSCode 任务时:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Hello task",
            "type": "shell",
            "command": "sudo echo hello"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我想它在单独的会话中运行每个任务,并且每次都要求我提供密码。

> Executing task: sudo echo hello <

[sudo] password for pt12lol: 
hello

Terminal will be reused by tasks, press any key to close it.

> Executing task: sudo echo hello <

[sudo] password for pt12lol: 
hello

Terminal will be …
Run Code Online (Sandbox Code Playgroud)

linux terminal sudo root visual-studio-code

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

`yield from` 生成器 vs `yield from` 列表性能

Python 3.6.8 (default, Oct  7 2019, 12:59:55) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.9.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: def yield_from_generator(): 
   ...:     yield from (i for i in range(10000)) 
   ...:                                                                                                                                    

In [2]: def yield_from_list(): 
   ...:     yield from [i for i in range(10000)] 
   ...:                                                                                                                                    

In [3]: import timeit                                                                                                                      

In [4]: timeit.timeit(lambda: list(yield_from_generator()), number=10000)                                                                  
Out[4]: 5.3820097140014695

In [5]: timeit.timeit(lambda: list(yield_from_list()), number=10000)                                                                       
Out[5]: 4.333915593000711
Run Code Online (Sandbox Code Playgroud)

我多次运行yield from生成器并yield from列出。列表版本总是提供更好的性能,而我的直觉告诉我相反的结论 - 制作列表需要在启动时分配内存。为什么我们可以注意到这种性能差异?

python yield list generator coroutine

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

无需启动即可创建EC2实例

我想创建一些EC2实例,但在它开始之前使用为此实例生成的实例ID做一些事情.是否有任何选项aws ec2 run-instances或其他aws ec2命令用于创建AWS EC2实例而不启动它?解决方法是生成UUID以标记此实例并关联此UUID而不是实例ID,或者在运行实例之后停止,但我更喜欢一些更智能的解决方案.

amazon-ec2 amazon-web-services aws-cli

5
推荐指数
2
解决办法
637
查看次数

printf 包装器

我在 Arduino 下编码,我想开发串行打印格式化功能,所以我试图使用sprintf未知大小的缓冲区。基本上,我们可以避免谈论 Arduino 及其串行输出,而是考虑将文本写入缓冲区,然后使用printf. 我试过这个:

#include <stdio.h>
#include <stdarg.h>

void printf0( const char* format, ... ) {
    va_list args;
    va_start(args, format);
    vprintf(format, args);
    va_end( args );
}

void printf1(const char* format,...) {
  va_list args;
  va_start(args, format);
  char buf[vsnprintf(NULL, 0, format, args)];
  sprintf(buf, format, args);
  printf(buf);
  va_end(args);
}

int main()
{
    printf0("Hello, %d!\n", 15);
    printf1("Hello, %d!\n", 15);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

printf0函数是我在这里找到的一个准确示例。我的尝试是 function printf1,它产生不可预测的数字。上述程序的示例输出是:

Hello, 15!
Hello, 860799736!
Run Code Online (Sandbox Code Playgroud)

c arduino

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

在C#中调用模板中的方法

我尝试使用模板方法(foo1)中的方法(foo2),编译器说他不知道属于那个类(T)的这个方法(foo2).

什么是正确的语法,哪个编译器接受它?

private void foo1<T>(T instance)
{
    instance.foo2();
}
Run Code Online (Sandbox Code Playgroud)

c#

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

要覆盖的最小比较运算符集

我想在Python中覆盖所有比较运算符(==,!=,<,<=,>,> =),我想尽我所能.从逻辑的角度来看,定义两个任何运算符(不包括对:==和!=,<和> =,>和<=)就足够了.在Python中覆盖这些运算符的最小集合是什么?这样就够了吗?

class MyInt:
    __init__(self, num):
        self.num = num
    __eq__(self, other):
        return self.num == other.num
    __lt__(self, other):
        return self.num < other.num
Run Code Online (Sandbox Code Playgroud)

python overriding operators

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

如何使用foreach迭代更大的列表

我有两个列表,list1的大小为5的元素,list2的大小为6的元素.我想使用foreach语句迭代更大的列表大小(例如,在这种情况下为6),但问题是我没有使用if条件来检查哪个列表更大.那么我该怎么做必要的任务呢.

if (list1.Count>list2.Count) // here I donot want to use if statement
{                            // do it in 1 statement only
    Size=list1.Count;
    foreach (var item in list1)
    {
        // do something
    }
}
else 
{
    Size = list2.Count;
    foreach (var item in list2)
    {
        // do something
    }
}
Run Code Online (Sandbox Code Playgroud)

c# foreach

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