我有以下嵌套循环:
for (x in xs) {
for (y in ys) {
# Do something with x and y
}
}
Run Code Online (Sandbox Code Playgroud)
我想拉平,所以我想建立两个向量的笛卡尔乘积的xs和ys并遍历结果.在Python中,这将是微不足道的:
for xy in product(xs, ys):
# x, y = xy[0], xy[1]
Run Code Online (Sandbox Code Playgroud)
但在R中,我发现的最简单的等价物看起来令人生畏:
xys <- expand.grid(xs, ys)
for (i in 1 : nrow(xys)) {
xy <- as.vector(xys[i, ])
# x <- xy[1], y <- xy[2]
}
Run Code Online (Sandbox Code Playgroud)
当然必须有更好的方法,不是吗?(为了澄清,我不想迭代索引 ......我认为必须有一种方法可以直接迭代产品中的元组.)
我的代码中有一个块,其中for循环应该根据条件向前或向后运行.
if (forwards) {
for (unsigned x = 0; x < something.size(); x++ ) {
// Lots of code
}
} else {
for (unsigned x = something.size()-1 ; x >= 0 ; x-- ) {
// Lots of code
}
}
Run Code Online (Sandbox Code Playgroud)
有没有一个很好的方法来设置它,所以我不重复两次for循环中的所有代码?
有问题的'东西'是std :: vector <>,所以也许它可以用迭代器?(我没有使用C++ 11)
我正在尝试在R中复制一个shell命令,并且无法弄清楚如何将命令串在一起.这只返回工作文件夹的内容(system()由于某种原因失败):
> shell("dir")
Volume info ..
Directory of E:\Documents\R
contents are listed..
Run Code Online (Sandbox Code Playgroud)
现在让我们尝试导航到C盘并运行dir(不使用明显的dir C:)..
> shell("cd C:")
C:\
> shell("dir")
Volume in drive E is GT
etc..
Run Code Online (Sandbox Code Playgroud)
所以似乎命令不能单独输入,因为shell不记得工作目录.所以..
> (cmd = "cd C:
+ dir")
[1] "cd C:\ndir"
> shell(cmd)
C:\
Run Code Online (Sandbox Code Playgroud)
没有运气,因为没有报告C:文件夹.我尝试过的其他方法也失败了.感谢任何想法.
在system.log中我可以看到我的进程:
thread 515376 caught burning CPU! It used more than 50% CPU
Run Code Online (Sandbox Code Playgroud)
我使用多个线程,所以我尝试在线程使用的runnable方法中打印线程id,如下所示:
void* runnable1(void* ptr)
{
pthread_t tid = pthread_self();
printf("HELLO from thread runnable1 with id : %ld\n", tid);
...
}
Run Code Online (Sandbox Code Playgroud)
但是我得到这样的id:
HELLO from thread runnable1 with id : 4488212480
Run Code Online (Sandbox Code Playgroud)
与system.log中的不同.
问题是,如何以system.log中出现的方式获取线程ID?
我有一个调用mkstemp()的程序,写了一些返回fd的东西,然后关闭fd.我希望文件保留,直到我自己删除它!使用rm命令等等.我的问题是:Linux会在关闭后删除此文件(fd)吗?
以下代码在g ++(各种版本)上编译良好,但在我的系统上使用libc ++的clang ++ - 3.4失败:
#include <map>
#include <string>
std::map<std::string, std::string> f() {
return {};
}
int main() {
auto m = f();
}
Run Code Online (Sandbox Code Playgroud)
clang标志着以下问题:
x.cpp:6:12: error: chosen constructor is explicit in copy-initialization
return {};
^~
/usr/local/Cellar/llvm34/3.4.2/lib/llvm-3.4/bin/../include/c++/v1/map:838:14: note: constructor declared here
explicit map(const key_compare& __comp = key_compare())
^
Run Code Online (Sandbox Code Playgroud)
实际上,include文件将构造函数声明为explicit.但它在我的C++ 11草案标准中没有标记.这是clang ++/libc ++中的错误吗?我无法找到相关的错误报告.
有没有一种简单的方法来检索R中非对称矩阵的上(或下)三角矩阵?对于对称矩阵,这可以使用mat[upper.tri(mat)],但对于非对称矩阵怎么样?这里上三角矩阵的定义如下:如果具有超过1/2部分的单元属于由对角线界定的矩阵的右上角,则该单元属于上三角矩阵(例如, ,图中的红色部分).
我正在尝试设置Poetry以将包部署到我们内部的 Gitlab 包注册表。根据在线的其他来源,存储库 ID 应该是https://gitlab.com/api/v4/projects/<project id>/packages/pypi,但无论我尝试什么,Poetry 都会返回
[上传
错误] HTTP 错误 404:未找到
任何人得到这个工作?
基本上,我有 2 个线程,接收和发送。我希望能够输入一条消息,每当我收到一条新消息时,它就会“打印在我正在输入的行上方”。首先我认为会起作用,您可以粘贴它,它将运行:
import multiprocessing
import time
from reprint import output
import time
import random
import sys
def receiveThread(queue):
i = 0
while True:
queue.put(i)
i+=1
time.sleep(0.5)
def sendThread(queue):
while True:
a = sys.stdin.read(1)
if (a != ""):
queue.put(a)
if __name__ == "__main__":
send_queue = multiprocessing.Queue()
receive_queue = multiprocessing.Queue()
send_thread = multiprocessing.Process(target=sendThread, args=[send_queue],)
receive_thread = multiprocessing.Process(target=receiveThread, args=[receive_queue],)
receive_thread.start()
send_thread.start()
with output(initial_len=2, interval=0) as output_lines:
while True:
output_lines[0] = "Received: {}".format(str(receive_queue.get()))
output_lines[1] = "Last Sent: {}".format(str(send_queue.get()))
Run Code Online (Sandbox Code Playgroud)
但是这里发生的是我无法发送数据。与我 put 时不同,输入不会给我一个 EOF a …
如何在 python 中生成授权码/客户端密码以用于苹果登录和设备检查?