小编ved*_*905的帖子

从列表条目中连接python字符串

假设我有一个字符串列表,我想将它们组合成一个由下划线分隔的单个字符串.我知道我可以使用循环来做到这一点,但python做了很多没有循环的事情.python中有什么东西已经有这个功能吗?例如,我有:

string_list = ['Hello', 'there', 'how', 'are', 'you?']
Run Code Online (Sandbox Code Playgroud)

我想制作一个单独的字符串:

'Hello_there_how_are_you?'
Run Code Online (Sandbox Code Playgroud)

我尝试过的:

mystr = ''    
mystr.join(string_list+'_')
Run Code Online (Sandbox Code Playgroud)

但这给出了一个"TypeError:只能连接列表(不是"str")列表".我知道这样简单,但不是很明显.

python string python-2.7

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

使用非 bash shell 安装 Conda

由于未知的“历史”原因,我的工作默认为所有 linux 和 mac 机器使用 tcsh。我们正在尝试使用 Conda 向公众公开我们的大量代码。但 Conda 似乎只是 bash 的实现,这对于世界其他地方来说不是问题。我无法将其安装在 tcsh shell 中,并且在 Conda故障排除指南中找到了一些对 zsh 的引用,但我无法判断 Conda 是否仅是 bash 实现,或者它是否实际上可以在其他 shell 中工作。虽然我可以轻松地从 tcsh 终端窗口启动 bash,但这只是一个小烦恼。

所以我想知道的是:Conda 是否可以在其他 shell 类型中工作,如果不能,为什么?

python bash shell anaconda

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

C使用信号量和线程打印乒乓球

我不确定我是否理解信号量和线程,因此我决定尝试一个相对简单的示例。我正在尝试有2个线程来交替打印,一个线程“ ping”另一个线程“ pong”,每个线程通知另一个线程是通过使用信号量完成的。但是,当我实现下面的代码时,它会先打印ping几百次,再打印几次pong,并稍作暂停。

#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
sem_t pingsem;

void ping(){
    printf("Ping started\n");
    while(1){
        sem_wait(&pingsem);
        printf("ping\n");
        sem_post(&pingsem);
    }
}

void pong(){
    printf("Pong started\n");
    while(1){
        sem_wait(&pingsem);
        printf("pong\n");
        sleep(1);
        sem_post(&pingsem);
    }
}

int main(){
    sem_destroy(&pingsem);  //make sure the semaphore starts dead
    sem_init(&pingsem, 0, 1);  //initialize semaphore
    pthread_t ping_thread, pong_thread;  //start the threading
    pthread_create(&ping_thread, NULL, ping, NULL);
    pthread_create(&pong_thread, NULL, pong, NULL);
    pthread_join(ping_thread,NULL);
    pthread_join(pong_thread,NULL);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我编译使用:

gcc stest.c -o stest -lpthread -lrt

没有错误或警告,但是当我运行它时,我得到:

$ ./stest
Ping …
Run Code Online (Sandbox Code Playgroud)

c multithreading semaphore

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

CUDA C求和2D数组的1维并返回

我是GPU编程的新手(而且在C中相当生疏)所以这可能是一个相当基本的问题,我的代码中有一个明显的错误.我想要做的是采用二维数组并找到每一行的每列的总和.所以,如果我有一个包含以下内容的2D数组:

0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 2 4 6 8 10 12 14 16 18
Run Code Online (Sandbox Code Playgroud)

我想得到一个包含以下内容的数组:

45
45
90
Run Code Online (Sandbox Code Playgroud)

到目前为止我的代码没有返回正确的输出,我不知道为什么.我猜这是因为我没有正确处理内核中的索引.但是我可能没有正确使用内存,因为我从一个过于简化的1维示例中进行了调整,而CUDA编程指南(第3.2.2节)对于1之间的初学者来说是一个相当大且不太好描述的跳转和2维数组.

我的错误尝试:

#include <stdio.h>
#include <stdlib.h>


// start with a small array to test
#define ROW 3
#define COL 10

__global__ void collapse( int *a, int *c){
    /*
       Sum along the columns for each row of the 2D array.
    */
    int total = …
Run Code Online (Sandbox Code Playgroud)

c arrays indexing cuda nvidia

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

使用Astropy在FITS表中挑选一行

我"只是"想要阅读(天文学)FITS表,并通过其名称选择一个对象的所有信息::

from astropy.io import fits
dr7q = fits.open('Shen_dr7_bh_May_2010.fits')
tbdata = dr7q[1].data
w = tbdata[tbdata['SDSS_NAME'] == 'J000006.53+003055.2']

print(tbdata[w])
Run Code Online (Sandbox Code Playgroud)

给出一个

IndexError: arrays used as indices must be of integer (or boolean) type
Run Code Online (Sandbox Code Playgroud)

试:

mask = (tbdata['SDSS_NAME'] == 'J000006.53+003055.2')
print(mask)
Run Code Online (Sandbox Code Playgroud)

然后给出一个数组原始FITS表文件的大小.这在IDL中是直截了当的.为什么这么难?!?!

python astronomy idl-programming-language fits astropy

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