小编K. *_* C 的帖子

类可以包含自身的实例作为数据容器吗?

python类可以包含自身的实例作为数据容器可能看起来像这样吗?

class A:
    def __init__(self, val):
        self.a = A(val)
        self.val = val

aa = A(2) 
#this will cause RuntimeError: maximum recursion depth exceeded
Run Code Online (Sandbox Code Playgroud)

我的目的是使用这个类作为数据容器包含一个副本,如果它被用于减少deepcopy操作.它可以用作"撤销"链,有机会在必要时获取初始值.

这样的行动有可能吗?

python recursion class

7
推荐指数
2
解决办法
6711
查看次数

需要SQL语句专注于表的组合,但条目始终具有唯一ID

我需要SQL代码来解决表组合问题,如下所述:

表旧数据:表旧

    name     version    status    lastupdate      ID
    A        0.1        on        6/8/2010        1
    B        0.1        on        6/8/2010        2
    C        0.1        on        6/8/2010        3
    D        0.1        on        6/8/2010        4
    E        0.1        on        6/8/2010        5
    F        0.1        on        6/8/2010        6
    G        0.1        on        6/8/2010        7
Run Code Online (Sandbox Code Playgroud)

表新数据:表新

    name     version    status    lastupdate     ID         
    A        0.1        on        6/18/2010                
                                                           #B entry deleted
    C        0.3        on        6/18/2010                #version_updated
    C1       0.1        on        6/18/2010                #new_added
    D        0.1        on        6/18/2010                
    E        0.1        off       6/18/2010                #status_updated
    F        0.1        on        6/18/2010                
    G        0.1 …
Run Code Online (Sandbox Code Playgroud)

mysql sql sqlite combinations unique

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

Twisted non-blocking execute reactor.run()?

I am using twisted reactor to non-block reading sockets input. however, I want to run another loop after reactor starting running

.....
reactor.listenTCP(12345, MyFactory())
reactor.run()

# ... blah blah socket input related code
while 1:
    ...
    ...
    if something:
       reactor.stop()
Run Code Online (Sandbox Code Playgroud)

Problem is after reactor.run() the while loop will not working. May I know rather than using threading in main te = Thread(target=reactor.run, args=(False,)).start(), any other way can make reactor.run() working on non-blocking?

Thank you.

python nonblocking reactor

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

python dict按键的值删除重复值?

一个字典

dic = {
 1: 'a', 
 2: 'a', 
 3: 'b', 
 4: 'a', 
 5: 'c', 
 6: 'd', 
 7: 'd', 
 8: 'a', 
 9: 'a'}
Run Code Online (Sandbox Code Playgroud)

我想删除重复值只保留一个K/V对,关于这些重复值的"关键"选择,可以是max或min,也可以通过随机选择其中一个重复项的键.

我不想使用ak/v交换,因为它无法控制密钥选择.

以值"a"为例

 1: 'a', 
 2: 'a', 
 4: 'a', 
 8: 'a', 
 9: 'a'
Run Code Online (Sandbox Code Playgroud)

最大键为{9:'a'},最小值为{1:'a'},随机将选择其中任何一个.

并且,如果键是其他类型的哈希值,例如字符串,那么如何做这样的选择呢?

谁能和我分享一个想法?

谢谢!

python dictionary duplicates

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

链接执行的python函数列表?

在python中我定义了函数:

def foo_1(p): return p + 1
def foo_2(p): return p + 1
def foo_3(p): return p + 1
def foo_4(p): return p + 1
def foo_5(p): return p + 1
Run Code Online (Sandbox Code Playgroud)

我需要执行这些功能,因为链可能是这样的:

foo_1(foo_2(foo_3(foo_4(foo_5(1)))))
Run Code Online (Sandbox Code Playgroud)

我是否可以知道是否可以将函数推入列表然后将这些函数作为链执行,也许我可以给出执行序列?

lf = [Null,foo_1,foo_2,foo_3,foo_4,foo_5]  # Null is for +1 issue here

def execu(lst, seq, raw_para):
    # in some way

execu(lf,(1,2,3,4,5), 1)   # = foo_1(foo_2(foo_3(foo_4(foo_5(1)))))
execu(lf,(1,2,3), 1)       # = foo_1(foo_2(foo_3(1)))
execu(lf,(3,3,3), 1)       # = foo_3(foo_3(foo_3(1)))
Run Code Online (Sandbox Code Playgroud)

谢谢!

RGS,

KC

python function composition

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