小编mzn*_*rft的帖子

如何在python中读取其他目录中的文件

我有一个文件,其名称是5_1.txt在我直接命名的目录中,
如何使用读取的指令读取该文件.

我使用以下方法验证了路径

import os
os.getcwd()
os.path.exists(direct)
Run Code Online (Sandbox Code Playgroud)

结果是
真的

x_file=open(direct,'r')  
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误:

Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
x_file=open(direct,'r')
IOError: [Errno 13] Permission denied
Run Code Online (Sandbox Code Playgroud)

我不知道为什么我看不懂文件?有什么建议吗?
谢谢 .

python

29
推荐指数
3
解决办法
15万
查看次数

在python中读取文件后返回单词列表

我有一个名为的文本文件test.txt.我想阅读它并从文件中返回所有单词列表(删除换行符).

这是我目前的代码:

def read_words(test.txt):
    open_file = open(words_file, 'r')
    words_list =[]
    contents = open_file.readlines()
    for i in range(len(contents)):
         words_list.append(contents[i].strip('\n'))
    return words_list    
    open_file.close()  
Run Code Online (Sandbox Code Playgroud)

运行此代码会生成以下列表:

['hello there how is everything ', 'thank you all', 'again', 'thanks a lot']
Run Code Online (Sandbox Code Playgroud)

我希望列表看起来像这样:

['hello','there','how','is','everything','thank','you','all','again','thanks','a','lot']
Run Code Online (Sandbox Code Playgroud)

python string list

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

如何查找python中代码的统计信息和执行时间

我正在研究 python,并遇到了一些查找代码的统计信息和执行时间的概念

假设我有以下代码

from time import gmtime, strftime
import timeit


def calculation():
     a = 2
     b = 3
     res = a + b
     return  res

if 'name' == 'main' :
    exec_time = timeit.timeit(calculation)
    print exec_time
Run Code Online (Sandbox Code Playgroud)

结果:

0.2561519145965576
Run Code Online (Sandbox Code Playgroud)

所以从上面的代码我可以找到代码的执行时间,但是如何在python中找到代码的统计信息呢?

最后我的意图如下

  1. 如何在python中查找代码的统计信息
  2. 如何在python中找到整个代码的执行时间
  3. 代码统计实际上意味着什么?

编辑后的代码:

例如我的文件中有上面的代码test.py

现在我已经使用下面的命令运行了上面的文件

python -m cProfile test.py
Run Code Online (Sandbox Code Playgroud)

结果 :

sh-4.2$ python -m cProfile test.py
         4 function calls in 0.001 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.001    0.001    0.001    0.001 test.py:1(<module>)
        1 …
Run Code Online (Sandbox Code Playgroud)

python statistics execution-time

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

CUDA中的矩形矩阵乘法

在这个作业中,我需要完成代码,使用CUDA C将两个矩形矩阵相乘.完成代码后,我提交并且当矩阵为正方形时,解决方案对于数据集是正确的,而结果与预期不匹配当矩阵不是正方形时的值.

这是我添加缺少部分后的代码:

#include    <wb.h>

#define wbCheck(stmt) do {                             \
    cudaError_t err = stmt;                            \
    if (err != cudaSuccess) {                          \
        wbLog(ERROR, "Failed to run stmt ", #stmt);    \
        return -1;                                     \
    }                                                  \
} while(0)

// Compute C = A * B
__global__ void matrixMultiply(float * A, float * B, float * C,
               int numARows, int numAColumns,
               int numBRows, int numBColumns,
               int numCRows, int numCColumns) {
   //@@ Insert code to implement matrix multiplication here
   int Row = blockIdx.y * …
Run Code Online (Sandbox Code Playgroud)

cuda

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

简单的字符串连接

如果我x='wow'在Python中有一个字符串,我可以使用该__add__函数将此字符串与其自身连接,如下所示:

x='wow'  
x.__add__(x)  
'wowwow'
Run Code Online (Sandbox Code Playgroud)

我怎么能用C++做到这一点?

c++

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

标签 统计

python ×3

c++ ×1

cuda ×1

execution-time ×1

list ×1

statistics ×1

string ×1