小编tam*_*gal的帖子

在列表中查找数字簇

我正在努力解决这个问题,因为我确信十几个for循环不是这个问题的解决方案:

有一个排序的数字列表,如

numbers = [123, 124, 128, 160, 167, 213, 215, 230, 245, 255, 257, 400, 401, 402, 430]
Run Code Online (Sandbox Code Playgroud)

我想创建一个带有数字列表的字典,其中数字的差异(彼此跟随)不超过15.所以输出将是这样的:

clusters = {
    1 : [123, 124, 128],
    2 : [160, 167],
    3 : [213, 215, 230, 245, 255, 257],
    4 : [400, 401, 402],
    5 : [430]
}
Run Code Online (Sandbox Code Playgroud)

我目前的解决方案有点难看(我必须在最后删除重复...),我确信它可以用pythonic方式完成.

这就是我现在所做的:

clusters = {}  
dIndex = 0 
for i in range(len(numbers)-1) :
    if numbers[i+1] - numbers[i] <= 15 :
        if not clusters.has_key(dIndex) : clusters[dIndex] = []
        clusters[dIndex].append(numbers[i])
        clusters[dIndex].append(numbers[i+1])
    else : …
Run Code Online (Sandbox Code Playgroud)

python list

11
推荐指数
2
解决办法
4848
查看次数

在Mac OS X(Lion)上创建全屏叠加层的最优雅方式是什么?

我正在寻找在Mac OS X下创建全屏叠加层的"最佳"方式.我想创建一个透明或半透明的叠加层,它关注鼠标事件并显示其他输入/输出元素.

此叠加层应高于所有其他GUI项(如CMD-Tab叠加层).

你知道如何有效地做到这一点吗?目前我正在玩这种代码:

int windowLevel = CGShieldingWindowLevel();
NSRect windowRect = [[NSScreen mainScreen] frame];
NSWindow *overlayWindow = [[NSWindow alloc] initWithContentRect:windowRect
                                          styleMask:NSBorderlessWindowMask
                                            backing:NSBackingStoreBuffered
                                              defer:NO
                                             screen:[NSScreen mainScreen]];

[overlayWindow setReleasedWhenClosed:YES];
[overlayWindow setLevel:windowLevel];
[overlayWindow setBackgroundColor:[NSColor colorWithCalibratedRed:0.0
                                                          green:0.0
                                                           blue:0.0
                                                          alpha:0.5]];
[overlayWindow setAlphaValue:1.0];
[overlayWindow setOpaque:NO];
[overlayWindow setIgnoresMouseEvents:NO];
[overlayWindow makeKeyAndOrderFront:nil];
Run Code Online (Sandbox Code Playgroud)

...它工作正常,但我没有选择启动任何类型的动画,如缓慢增加透明度(慢慢调暗屏幕)等.

虽然我不明白如何将此窗口置于后台,但不释放它并让它不时弹出.

那么有更好或"标准"的方法吗?

cocoa overlay appkit nswindow

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

从iTunes网络搜索中获取更大的艺术作品图像

我正在使用iTunes Store网络服务搜索API获取歌曲信息.

我唯一的问题是,艺术品的分辨率非常低(只有60x60px或100x100px可用).

artworkUrl100,artworkUrl60:与返回的媒体类型相关联的艺术作品的URL,大小为100x100像素或60x60像素.

有没有办法获得更大尺寸的专辑图像?

itunes-store itunesartwork

10
推荐指数
2
解决办法
4493
查看次数

Pylint:"本地定义的禁用"仍然会发出警告.怎么压抑他们?

我使用的软件框架有几个类,方法名称包含大写字母(由于C++包装器).这当然不是PEP8并pylint显示相应的错误C0103.我还添加C0111到列表中以忽略某些方法缺少的文档字符串,如下所示:

def Configure(self): # pylint: disable=C0103,C0111
Run Code Online (Sandbox Code Playgroud)

它有效,但是现在因为当地人的不满而得到警告:

Class: I0011 -> locally disabling C0103
Class: I0011 -> locally disabling C0111
Run Code Online (Sandbox Code Playgroud)

我该如何抑制它们?

pylint

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

实时更新Dash/plotly中的数据

我想监视一些实时数据,并允许用户在与图表交互时选择自己的范围.我创建了这个小例子(从教程中得到)并且问题是,每次我更新绘图时,一切都会重置,因为update_graph_live()返回一个新的Plotly数字.(见下面的例子)

是否可以仅更新数据,因此图形不会重新加载并重置为默认视图/设置?之前我使用的是d3.js并通过websockets发送数据,因此我可以在浏览器中过滤数据.但是我想直接用Dash来做.

import dash
from dash.dependencies import Output, Event
import dash_core_components as dcc
import dash_html_components as html
from random import random
import plotly

app = dash.Dash(__name__)
app.layout = html.Div(
    html.Div([
        html.H4('Example'),
        dcc.Graph(id='live-update-graph'),
        dcc.Interval(
            id='interval-component',
            interval=1*1000
        )
    ])
)


@app.callback(Output('live-update-graph', 'figure'),
              events=[Event('interval-component', 'interval')])
def update_graph_live():
    fig = plotly.tools.make_subplots(rows=2, cols=1, vertical_spacing=0.2)
    fig['layout']['margin'] = {
        'l': 30, 'r': 10, 'b': 30, 't': 10
    }
    fig['layout']['legend'] = {'x': 0, 'y': 1, 'xanchor': 'left'}

    fig.append_trace({
        'x': [1, 2, 3, 4, 5],
        'y': [random() …
Run Code Online (Sandbox Code Playgroud)

python plotly plotly-dash

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

NSTimer不调用方法

我现在真的很沮丧,谷歌搜索整个互联网,偶然发现,仍然没有找到解决方案.

我正在尝试实现NSTimer,但我定义的方法不会被调用.(正确设置秒数,使用断点检查).这是代码:

- (void) setTimerForAlarm:(Alarm *)alarm {
    NSTimeInterval seconds = [[alarm alarmDate] timeIntervalSinceNow];
    theTimer = [NSTimer timerWithTimeInterval:seconds 
                            target:self 
                          selector:@selector(showAlarm:)
                          userInfo:alarm repeats:NO];
}

- (void) showAlarm:(Alarm *)alarm {
    NSLog(@"Alarm: %@", [alarm alarmText]);
}
Run Code Online (Sandbox Code Playgroud)

对象"theTimer"用@property定义:

@interface FooAppDelegate : NSObject <NSApplicationDelegate, NSWindowDelegate>  {
@private

    NSTimer *theTimer;

}

@property (nonatomic, retain) NSTimer *theTimer;

- (void) setTimerForAlarm:(Alarm *)alarm;
- (void) showAlarm:(Alarm *)alarm;
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

cocoa objective-c nstimer

9
推荐指数
3
解决办法
6462
查看次数

在坚实色的背景的生气蓬勃的透明预紧器图象

我希望在内容加载之前在我的网站上显示预加载器,但是当我选择具有透明背景的动画预加载器图像时,边缘看起来非常锯齿状.如何修改图像或有没有办法让GIF动画具有透明背景?

我正在使用preloader.net的预加载器

animation transparency animated-gif

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

在Haskell中有效地解析ASCII文件

我想在Haskell中重新实现我的一些ASCII解析器,因为我认为我可以获得一些速度.然而,即使是一个简单的"grep和count"也比一个草率的Python实现要慢得多.

有人可以解释我为什么以及如何正确地做到这一点?

所以任务是,计算以字符串"foo"开头的行.

我非常基本的Python实现:

with open("foo.txt", 'r') as f:
    print len([line for line in f.readlines() if line.startswith('foo')])
Run Code Online (Sandbox Code Playgroud)

Haskell版本:

import System.IO 
import Data.List

countFoos :: String -> Int
countFoos str = length $ filter (isPrefixOf "foo") (lines str)

main = do
    contents <- readFile "foo.txt"
    putStr (show $ countFoos contents)
Run Code Online (Sandbox Code Playgroud)

time在带有17001895行的~600MB文件上运行两者都表明Python的实现速度几乎是Haskell的4倍(在我的带有PCIe SSD的MacBook Pro Retina 2015上运行):

> $ time ./FooCounter                                                           
1770./FooCounter  20.92s user 0.62s system 98% cpu 21.858 total

> $ time python foo_counter.py                                                   
1770 …
Run Code Online (Sandbox Code Playgroud)

file-io haskell text-parsing

9
推荐指数
2
解决办法
309
查看次数

在Xcode4中忽略"属性不可用" - 警告

我在工具栏项目中使用了很多"自定义标识符",这在Xcode4中很好,但在构建项目时它给了我一堆警告:

3.2之前的Interface Builder版本中的属性不可用自定义标识符

有没有办法在Xcode4中忽略这些警告?当我搜索"真实"警告和错误时,它非常混乱.

macos cocoa xcode4

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

获取给定行的NSTableCellView

NSTableCellView是子类并存储指向Object的指针.我想访问该指针,我得到的唯一信息是row数字NSInteger.

如何使用给定的方式访问NSTableCellViewtableViewrow

nstableview

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