小编vka*_*l11的帖子

你需要使用__name __ = __ main__语句在python中运行任何脚本吗?

可能重复:
if __name__=="__main__":什么?Python
中的主要功能和/或__name__ == "__main__"检查有什么意义?

我只是想了解为什么你使用__name__='__main__'语句如果我们可以运行任何python脚本,即使不使用该语句.例如,我可以在不使用if __name__='__main__'语句的情况下运行下面的脚本.

def hello():
      print "hello"
      return 1234

# And here is the function being used
print hello()
Run Code Online (Sandbox Code Playgroud)

python

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

使用构造函数与设置函数

使用带参数的构造函数来初始化类的成员或者使用setter函数间接地执行它是否更好?到目前为止,我对这个问题没有得到明确的答案.任何见解将不胜感激.

oop

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

如何将具有固定列表或数组大小的 defaultdict 或字典作为值

我试图使用 d = defaultdict(array.array) 或 d = defaultdict(list) 作为字典,但我不知道如何将 array.array 或列表中的元素数量固定为 N,其中 N 是数组的大小我总是希望列表或 array.array 是。如果可能,我希望列表的所有 N 个元素都预先初始化为零。

python

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

pip install mysqlclient 在 Ubuntu 上失败,在 docker 上使用 python 2.7

当我运行下面的 docker 文件代码时

RUN apt-get update && apt-get install -y --no-install-recommends \
        ca-certificates \
        vim \
        git \
        python \
        python-pip \
        curl \
        wget \
        lsof \
        libmysqlclient-dev \
        python-dev \
        && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

#PYTHON requirements

ENV HTTPS_PROXY "https://gec-proxy-svr.homeoffice.wal-mart.com:8080/"

RUN python --version
RUN pip install pip --upgrade
RUN pip install  setuptools
RUN pip install setuptools --upgrade
RUN pip install git+https://github.com/PyMySQL/mysqlclient-python.git
RUN pip install -r requirements.txt
Run Code Online (Sandbox Code Playgroud)

我收到这个错误

Running setup.py install for mysqlclient: finished with …
Run Code Online (Sandbox Code Playgroud)

python pip docker

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

根据键中的位置对列表中的字典值进行排序

我有一个字典,其中的值是来自作为字符串的键的几个子字符串的列表。例如:

d = {"How are things going": ["going","How"], "What the hell" : ["What", "hell"], "The police dept": ["dept","police"]}
Run Code Online (Sandbox Code Playgroud)

并且我想根据列表值在键中出现的位置获取从列表值生成的列表列表。例如在上面的案例中:

output = [["How", "going"], ["What", "hell"], ["police", "dept"]]
Run Code Online (Sandbox Code Playgroud)

我没有找到一种有效的方法来做到这一点,所以我使用了一种hacky方法:

final_output = []
for key,value in d.items():
    if len(value) > 1:
       new_list = []
       for item in value:
          new_list.append(item, key.find(item)) 
       
          new_list.sort(key = lambda x: x[1]) 
       ordered_list = [i[0] for i in new_list] 
       final_ouput.append(ordered_list)
     
Run Code Online (Sandbox Code Playgroud)

python

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

如何在CUDA中执行适量的线程

dim3 DimGrid((n-1)/256 + 1, 1, 1);
dim3 DimBlock(256, 1, 1);
vecAddKernel<<<DimGrid,DimBlock>>>(d_A, d_B, d_C, n);
__global__
void vecAddkernel(float* A, float* B, float* C, int n)
{
int i = threadIdx.x + blockDim.x * blockIdx.x;
if(i<n) C[i] = A[i] + B[i];
}
Run Code Online (Sandbox Code Playgroud)

在上面的函数中假设我们有长度为n = 257的向量,我们将只分配2个块.我只是想知道调用vecAddkernel函数时第二个块中发生了什么.只有一个线程在第二个块中执行,或者所有256个线程都被执行,尽管其中255个没有输出.所以基本的问题是如何为每个vecAddKernel调用修复参数'n'?每个块是256,第一个块是256,第二个块是1吗?

cuda

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

最大加权跨越弱连通DAG的算法

是否存在一种算法来查找跨越DAG的最大权重,该DAG在有向图中弱连接,其中每个切割具有弱连接的集合(从一个集合到另一个集合至少有一个有向路径)?或者这是一个NP难题?关于这个主题的上一个问题没有指定https://mathoverflow.net/questions/31864/algorithms-for-maximum-weighted-spanning-connected-dag-directed-acyclic-graph弱或强连接,所以我想成为更确切.

algorithm graph subgraph directed-acyclic-graphs

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

是struct的默认构造函数,就像C++中的类一样

C++ struct是否也像类一样调用构造函数(默认,复制构造函数)和析构函数,或者它们遵循结构的C语言指南?所以在下面的例子中,是一个名为的默认构造函数?

Foo structObject; \\Foo is a struct
Run Code Online (Sandbox Code Playgroud)

c++

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

由于python中的"OR"操作,语法无效

straightrunfaces = 'A23456789TJQKA';
for i in range(0,10):
        if ((counts[straightrunfaces[i]] and 
             counts[straightrunfaces[i+1]] and 
             counts[straightrunfaces[i+2]] and 
             counts[straightrunfaces[i+3]])) or
            ((counts[straightrunfaces[i+1]] and 
             counts[straightrunfaces[i+2]] and 
             counts[straightrunfaces[i+3]] and 
             counts[straightrunfaces[i+4]])):



C:\datasci_course_materials\assignment3>python poker.py
  File "poker.py", line 62
    counts[straightrunfaces[i+3]])) or
                                     ^
**SyntaxError: invalid syntax**
Run Code Online (Sandbox Code Playgroud)

我不明白为什么"OR"运算符不起作用

python

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

在运算符重载中使用()

我用operator()遇到了这段代码.我以前从未见过这个(我见过+,>, - <<).有人可以解释何时应该使用它以及如何使用它?

 class sortResults
    {
    public:
        bool operator() (Result const & a, Result const & b);
    };
Run Code Online (Sandbox Code Playgroud)

c++

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

在 vi 中将文本移动到下一行

Shell脚本中有一行

f) SCREEN = TRUE
Run Code Online (Sandbox Code Playgroud)

我想要做到

f)
SCREEN = TRUE
Run Code Online (Sandbox Code Playgroud)

在 vi 中我不能很容易地做到这一点。显然,在普通文本编辑器中,输入命令可以将 f) 之后的文本移动到下一行。在 vi 中最好的方法是什么?

vi vim

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

sprintf在哪里存储文件

我正在调试具有以下行的代码:

    char file_name[128];
      sprintf(file_name, "layer-%03d-%02d-out.csv", c, i);
       CSV::SaveVector(file_name, current_vector);

int SaveVector(const std::string &filename, const Perceptron::EigenVector &v) {
  FILE *fp = fopen(filename.c_str(), "w");
  if (!fp) {
    MinLog("Failed to open file %s\n", filename.c_str());
    return FILE_ERROR;
  }

  for (int i = 0; i < v.size(); ++i) {
    fprintf(fp, "%s%.16lf", (i>0 ? " ," : ""),  static_cast<double>(v[i]));
  }

  fclose(fp);

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

该语句没有给出任何错误,但我在当前文件夹中的任何位置都找不到file_name.该文件将保存在哪里?

c++

-5
推荐指数
1
解决办法
502
查看次数

标签 统计

python ×5

c++ ×3

algorithm ×1

cuda ×1

directed-acyclic-graphs ×1

docker ×1

graph ×1

oop ×1

pip ×1

subgraph ×1

vi ×1

vim ×1