小编Joh*_*nck的帖子

'%% file test.py'在python中意味着什么?

正如标题所说.我想弄清楚'%%'是什么意思.它似乎不是占位符吗?

%%file test_theano.py
from theano import config
print 'using device:', config.device
Run Code Online (Sandbox Code Playgroud)

python ipython ipython-magic

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

返回true不起作用但返回false确实

怎么会

def allEven(n):
    for c in n:
            if c % 2 != 0:
                    return False
    return True
Run Code Online (Sandbox Code Playgroud)

工作,但

def allEven(n):
    for c in n:
            if c % 2 == 0:
                    return True
    return False
Run Code Online (Sandbox Code Playgroud)

不?对于第二个,当我输入allEven([8,0,-1,4,-6,10])时,它表示它是真的.

python

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

有没有办法检查字典是否有键,以及该键的值是否一次不是 None ?

sampleDict = {'1':None}
Run Code Online (Sandbox Code Playgroud)

要检查密钥是否存在,如果不是无,我必须这样做

if '1' in sampleDict:
    if sampleDict['1'] is not None:
        #do something
Run Code Online (Sandbox Code Playgroud)

有没有更 Pythonic 的方法可以一次性完成此操作?

python dictionary

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

在python中将数据字典转换为numpy数组

我正在尝试转换具有如下结构的数据字典:

{'name': array(['Ben','Sean,'Fred'])
 'age': array([22, 16, 35]),
 'marks': array([98, 75, 60]),
 'result': array('HD','D','C')}
Run Code Online (Sandbox Code Playgroud)

然后,我需要过滤掉字典,使其仅包含新 numpy 数组中的名称、标记和结果,以便能够在图表上绘制(我可以做到这一点,但我无法过滤列表,然后转换为 numpy)

python dictionary numpy

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

为什么在多个线程中运行的相似代码会有不同的运行时间?

我遇到了一个关于 C++ 多线程程序的非常奇怪的问题,如下所示。

#include<iostream>
#include<thread>
using namespace std;
int* counter = new int[1024];
void updateCounter(int position)
{
    for (int j = 0; j < 100000000; j++)
    {
        counter[position] = counter[position] + 8;
    }
}
int main() {
    time_t begin, end;
    begin = clock();
    thread t1(updateCounter, 1);
    thread t2(updateCounter, 2);
    thread t3(updateCounter, 3);
    thread t4(updateCounter, 4);
    t1.join();
    t2.join();
    t3.join();
    t4.join();
    end = clock();
    cout<<end-begin<<endl;  //1833
    begin = clock();
    thread t5(updateCounter, 16);
    thread t6(updateCounter, 32);
    thread t7(updateCounter, 48);
    thread t8(updateCounter, 64);
    t5.join(); …
Run Code Online (Sandbox Code Playgroud)

c++ arrays time multithreading

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

指针赋值 - uint16_t

我正在查看来自cs61c(ucb)的问题.我有以下方法:

void lfsr_calculate(uint16_t *reg) {                                      
  uint16_t result = compute_bit_val(*reg);                              
  printf("reg value: %d", *reg);                                        
  printf("bit val result: %d", result);                                 
  printf("bit val result shifted: %d", result << 16);                   
  *reg >>= 1;                                                           
  printf("bit val result shifted plus zero: %d", *reg + (result << 16));
  *reg = (uint16_t) *reg + (result << 16);                              
  printf("new reg: %d", *reg);                                          
}
Run Code Online (Sandbox Code Playgroud)

如果*reg为1,我的方法compute_bit_val返回1.打印输出为

1 

1

65536

65536

**0**
Run Code Online (Sandbox Code Playgroud)

?!?!?!我拉出我的头发,我不知道为什么最后一部分是零,由于某种原因,分配不起作用.我尝试使用和不使用铸造,它给出了相同的结果.

c pointers uint16

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

如何在c ++中声明变量我不想定义它所以它不应该消耗任何内存只是声明?

当我们写int时,编译器给int a内存,但现在我只想告诉编译器变量是整数但不想编译器给任何内存.

c++

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

用python发送文件

我已经在网上寻找了如何在python中发送文件的每个地方,但100%失败了,没人能帮上忙。是否有程序员可以帮助我将文件从客户端发送到服务器或以其他方式发送?

我可以很容易地发送txt

#!/usr/bin/python
"""
Socket Client
"""
import socket #networking library
indent = ""
server = input("server name (default is " + socket.gethostname() + "): ") or socket.gethostname()

print("connecting to server at: %s" % server)

while True:
    clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

    clientSocket.connect((server, 23000)) 



    str = input("text to send: ")

    clientSocket.send(str.encode("utf-8")) #send text as encoded bytes

    print("received: %s" % clientSocket.recv(100).decode("utf-8")) 

    clientSocket.close()

    #strRecv = clientSocket.recv(500).decode("utf-8") #receive up to 500 bytes and decode into text
    #print(strRecv)
Run Code Online (Sandbox Code Playgroud)

python tcp file send

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

标签 统计

python ×5

c++ ×2

dictionary ×2

arrays ×1

c ×1

file ×1

ipython ×1

ipython-magic ×1

multithreading ×1

numpy ×1

pointers ×1

send ×1

tcp ×1

time ×1

uint16 ×1