小编may*_*ull的帖子

将字符串转换为NumPy datetime64 dtype

我程序每分钟获取一串当前时间,因为 date = '201711081750'

我想将这些字符串作为np.datetime64存储到数组中。

我想我可以将这种字符串转换为

>>> date = '201711081750'

>>> np.datetime64( date[:4] +'-'+date[4:6]+'-'+date[6:8]+' ' +date[8:10]+':'+date[10:]  , 'm' )
numpy.datetime64('2017-11-08T17:50')
Run Code Online (Sandbox Code Playgroud)

但是它看起来很复杂,我认为稍后可能会引发错误。

有更简单的方法可以做到这一点吗?

python arrays datetime numpy

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

如何获得列表中唯一的值?

如果有一个列表,其中的元素是[2,2,3,2,2].我想找到唯一的元素,这次是3.

我想我可以用count()方法和一些循环来做到这一点,但我想知道是否有更简单有效的方法来做到这一点.

python list

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

是否可以在 Python 的线程中生成一个进程?

我正在编写一个程序,该程序生成一个进程并在某些条件下重新启动该进程。例如,如果子进程在一段时间内不再向母进程发送数据,我希望母进程终止子进程并重新启动它。我以为我可以使用线程从子进程接收数据并重新启动子进程,但它不像我想的那样工作。

import numpy as np
import multiprocessing as mp
import threading
import time
from apscheduler.schedulers.background import BackgroundScheduler

pipe_in, pipe_out = mp.Pipe()

class Mother():
    def __init__(self):
        self.pipe_out = pipe_out

        self.proc = mp.Process(target = self.test_func, args=(pipe_in, ))
        self.proc.start()

        self.thread = threading.Thread(target=self.thread_reciever, args=(self.pipe_out, ))
        self.thread.start()

    def thread_reciever(self, pipe_out):
        while True:
            value = pipe_out.recv()

            print(value)
            if value == 5:
                self.proc.terminate()
                time.sleep(2)
                self.proc = mp.Process(target = self.test_func)
                self.proc.start()

    def test_func(self, pipe_in):
        for i in range(10):
            pipe_in.send(i)
            time.sleep(1)


if __name__ == '__main__':
    r = Mother()
Run Code Online (Sandbox Code Playgroud)

它打印出这个错误。 …

python windows multithreading multiprocessing python-multiprocessing

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

有没有比这更好的方法来计算两个列表的协方差?

我有两个列表,其中一个由 x 坐标组成,另一个由 y 坐标组成。

x_coordinates = [1, 2, 3, 4, 5]
y_coordinates = [1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)

例如,point 1(1,1)

我想计算两个列表的协方差,我编写了一个代码,但我认为它有些不必要的冗长和混乱。我知道我可以只使用 math.cov 来计算这个,但我想知道是否可以巧妙地编程,也许使用 map 和 lambda 函数。

公式是这样的:

(x1 - average_x)*(y1 - average_y) + ... + (xn - average_x)*(yn - average_y) / (the number of the items in one of the lists)
Run Code Online (Sandbox Code Playgroud)

编码:

import math

x_coordinates = [1, 2, 3, 4, 5]
y_coordinates = [1, 2, 3, 4, 5]
covariance_temp_sum = 0

x_mean = math.fsum(x_coordinates) / …
Run Code Online (Sandbox Code Playgroud)

python math covariance

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

如何在Python中使用带协同程序的装饰器?

我正在尝试制作一个程序,它产生两个互相交流的进程.我已经阅读了关于协同程序的内容,并认为这次采用它会很好,而且由于协程在使用之前需要启动,我认为让装饰器自动完成它会很好.

import multiprocessing as mp
import random
import time
import os
from datetime import datetime, timedelta
from functools import wraps

output, input = mp.Pipe()



def co_deco(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        cr = func(*args, **kwargs)
        cr.send(None)
        return cr
    return wrapper

class sender(mp.Process):
    def __init__(self, pipe):
        mp.Process.__init__(self)
        self.pipe = pipe

    def run(self):
        print('RECEIVER PID: ', os.getpid() )
        while True:
            self.pipe.send( random.randint(0,10) )
            time.sleep(1)

class receiver(mp.Process):
    def __init__(self, pipe):
        mp.Process.__init__(self)
        self.pipe = pipe

    def run(self):
        while True:
            self.coroutine.send( self.pipe.recv() )

    @co_deco
    def coroutine(self): …
Run Code Online (Sandbox Code Playgroud)

python coroutine python-decorators

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