小编Vis*_*hal的帖子

迭代一个列表元组

我有一个列表元组。元组中的每个列表都具有相同数量的元素。如何在 for 循环中迭代它

前任:

tupleList = ([1,2,3], ['label1', 'label2', 'label3'])
for (val, label) in <something>:
    print val, label
Run Code Online (Sandbox Code Playgroud)

应该输出:

1, label1
2, label2
3, label3
Run Code Online (Sandbox Code Playgroud)

注意:这个元组列表可以包含两个以上的列表。

PS:对于那些选择此重复的人,请检查正确解决方案的响应。它不同于遍历两个单独的列表。

python loops tuples

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

无法在单独的Task中创建BItmapImage

我正在研究WP8的成像应用程序(Lumia 920).我在xaml层中用C#编码.

我在尝试在单独的任务中创建新的BitmapImage对象时遇到问题,该任务期望使用相机应用程序生成的帧.

这是我的代码的简化版本:

public void ProcessFrames(){
    while (true)
    {
        dataSemaphore.WaitOne();
        if (nFrameCount>0)
        {
            MemoryStream ms = new MemoryStream(previewBuffer1);

            BitmapImage biImg = new BitmapImage();  // *******THROWS AN ERROR AT THIS LINE ********
            biImg.SetSource(ms);

            ImageSource imgSrc = biImg as ImageSource;
            capturedFrame.Source = imgSrc;
        }
    }
}

public MainPage()
{
    InitializeComponent();
    T1 = new Thread(ProcessFrames);
    T1.Start();
}
Run Code Online (Sandbox Code Playgroud)

现在,令人惊讶的部分是我没有在"new BitmapImage()"中得到错误,以防我在其中一个主要函数中执行相同的操作,例如:

public MainPage()
{
    InitializeComponent();
    BitmapImage biImg = new BitmapImage();    // ****** NO ERROR ***********
    T1 = new Thread(ProcessFrames);
    T1.Start();
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我理解这种行为的原因.我的要求是能够使用预览缓冲区(previewBuffer1)并将其显示在其中一个图像帧中.这需要我在单独的任务中创建一个新的BitmapImage.

c# multithreading windows-phone windows-phone-8

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

信号量无一例外地溢出到最大值

我需要在我的应用程序中使用信号量,其中包括多个线程.我的用法可能是常见的情况,但我坚持使用API​​.

在我的使用中,信号量可以从多个位置发布,而只有一个线程在信号量上等待.

现在,我要求信号量是二进制信号,即,我需要确保在多个线程同时发布到信号量的情况下,信号量计数保持为1,并且不会抛出任何错误.我怎样才能做到这一点.

简而言之,我需要以下代码才能工作.

private static Semaphore semaphoreResetMapView = new Semaphore(0, 1);  // Limiting the max value of semaphore to 1.

void threadWait(){
    while (true){
        semaphoreResetMapView.WaitOne();
        <code>
    }
}

void Main(){

    tThread = new Thread(threadWait);
    tThread.Start();

    semaphoreResetMapView.Release(1);
    semaphoreResetMapView.Release(1);
    semaphoreResetMapView.Release(1);  // Multiple Releases should not throw an error. Rather saturate the value of semaphore to 1.
}
Run Code Online (Sandbox Code Playgroud)

我将不胜感激任何帮助.

c# semaphore

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

龙卷风 - 通过websockets同时收听多个客户端

我想使用Tornado在Python中创建websocket服务器.这是API:http://tornado.readthedocs.org/en/latest/websocket.html

在API中,我没有看到获取客户端句柄的选项.如何同时处理多个客户端连接?
例如,on_message(self, message)方法直接给出消息.不包含已连接的客户端的任何句柄.
我想接收客户端请求,进行一些处理(可能需要很长时间),然后回复客户端.我正在寻找一个客户端句柄,我可以用它稍后回复.

python tornado python-2.7

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

当元组中的输出参数数量未知时,忽略多个输出

我有一个函数,它返回一个2或3个值的元组(取决于矩阵的大小).我只需要前两个,并丢弃第三个值,以防它存在.
有这方面的单线解决方案吗?

请注意(在将此问题标记为重复之前),这些解决方案不起作用:

(1)

(x,y, _) = myfunc() # Won't work since myfunc() may return only two values
Run Code Online (Sandbox Code Playgroud)

(2)

out = myfunc()     # This solution is not one-line
x = out[0]
y = out[1]
Run Code Online (Sandbox Code Playgroud)

python tuples

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

Convert list to set based on duplicate of only certain values of a tuple

In general, converting a list to set is simple, as below:

a = [1,2,3,1]
set_a = set(a)    # set([1, 2, 3])
Run Code Online (Sandbox Code Playgroud)

Now, I want to convert a list of tuples to set, only considering the first value of tuple.

a = [(1,"a1"), (2,"b2"), (3, "c3"), (1, "d4")]
set_a = some_magic(a)

# 1) set_a = set([(1,"a1"), (2,"b2"), (3, "c3")]) or
# 2) set_a = set([(1, "d4"), (2,"b2"), (3, "c3")])
# Both (1) or (2) are acceptible outputs.
Run Code Online (Sandbox Code Playgroud)

Is there a "one-line" …

python set python-3.x

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

在Windows Phone 8上创建Zoomable图像对象

我正在为Windows Phone 8开发一个与图像相关的应用程序.在完成所需的图像处理后,我当前在"图像"工具箱项目中显示输出,这似乎对我有用.这里的编码是在C#+ XAML中完成的.

现在我想改进UI并使输出图像可缩放(使用捏缩放).我想知道如何创建这样一个可以缩放的UI元素.

我知道这可能是许多应用开发者的常见要求.但我一直无法找到相同的参考.

windows-phone-8

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

加速矩阵中每个 x,y 点的角度计算

我有一个 3-d Numpy 数组flow,如下所示:

flow = np.random.uniform(low=-1.0, high=1.0, size=(720,1280,2))
# Suppose flow[0] are x-coordinates. flow[1] are y-coordinates.
Run Code Online (Sandbox Code Playgroud)

需要计算每个 x,y 点的角度。这是我如何实施它:

def calcAngle(a):
    assert(len(a) == 2)
    (x, y) = a
    # angle_deg = 0
    angle_deg = np.angle(x + y * 1j, deg=True)
    return angle_deg

fangle = np.apply_along_axis(calcAngle, axis=2, arr=flow) 
# The above statement takes 14.0389318466 to execute
Run Code Online (Sandbox Code Playgroud)

每个点的角度计算需要14.0389318466 seconds在我的 Macbook Pro 上执行。

有没有一种方法可以加快速度,可能是通过使用一些矩阵运算,而不是一次处理一个像素。

python performance numpy vectorization scipy

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

通过将元素放在括号内来创建字符串

这些通过分别定义单个元素来创建字符串:

str1 = ("a" "b")
# str1 = 'ab'

str2 = ("d"+str1)
# str2 = 'dab'

str3 = ("d" "e" "f")
# str3 = 'def'
Run Code Online (Sandbox Code Playgroud)

但是这个失败了。为什么这样?

str3 = ("d"+str1 "e")
# SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

有什么工作可以解决?

python

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