我正在努力了解它的用法super().从它的外观来看,可以创建两个子类,就好了.
我很想知道以下2个孩子班级之间的实际差异.
class Base(object):
def __init__(self):
print "Base created"
class ChildA(Base):
def __init__(self):
Base.__init__(self)
class ChildB(Base):
def __init__(self):
super(ChildB, self).__init__()
ChildA()
ChildB()
Run Code Online (Sandbox Code Playgroud) 我有一个很长的查询.我想在Python中将它分成几行.在JavaScript中实现它的一种方法是使用几个句子并将它们与+运算符连接(我知道,也许这不是最有效的方法,但我并不关心这个阶段的性能,只是代码可读性) .例:
var long_string = 'some text not important. just garbage to' +
'illustrate my example';
Run Code Online (Sandbox Code Playgroud)
我尝试在Python中做类似的事情,但它不起作用,所以我习惯\拆分长字符串.但是,我不确定这是否是唯一/最好/最好的方式.看起来很尴尬.实际代码:
query = 'SELECT action.descr as "action", '\
'role.id as role_id,'\
'role.descr as role'\
'FROM '\
'public.role_action_def,'\
'public.role,'\
'public.record_def, '\
'public.action'\
'WHERE role.id = role_action_def.role_id AND'\
'record_def.id = role_action_def.def_id AND'\
'action.id = role_action_def.action_id AND'\
'role_action_def.account_id = ' + account_id + ' AND'\
'record_def.account_id=' + account_id + ' AND'\
'def_id=' + def_id
Run Code Online (Sandbox Code Playgroud) 我想知道JMS队列和JMS主题之间的区别.
它说,从ActiveMQ页面
话题
在JMS中,Topic实现了发布和订阅语义.当您发布消息时,它会发送给所有感兴趣的订阅者 - 因此,零到多订阅者将收到该消息的副本.只有在经纪人收到消息时拥有有效订阅的订阅者才能获得该消息的副本.
队列
JMS队列实现负载均衡器语义.一个消费者将收到一条消息.如果在发送消息时没有可用的消费者,则将保留消费者,直到有消费者可以处理消息为止.如果消费者收到消息并且在关闭之前没有确认消息,那么该消息将被重新传递给另一个消费者.队列可以让许多消费者在可用消费者之间对消息进行负载平衡.
我希望有一个"东西",它将以与ActiveMQ代理接收消息的顺序相同的顺序向每个订阅者发送消息的副本.
有什么想法吗?
我试图了解TimeDistributed wrapper在Keras中的作用.
我得到TimeDistributed"将一个图层应用于输入的每个时间片."
但我做了一些实验并得到了我无法理解的结果.
简而言之,与LSTM层相关,TimeDistributed和Dense层具有相同的结果.
model = Sequential()
model.add(LSTM(5, input_shape = (10, 20), return_sequences = True))
model.add(TimeDistributed(Dense(1)))
print(model.output_shape)
model = Sequential()
model.add(LSTM(5, input_shape = (10, 20), return_sequences = True))
model.add((Dense(1)))
print(model.output_shape)
Run Code Online (Sandbox Code Playgroud)
对于这两种型号,我的输出形状为(无,10,1).
任何人都可以解释RNN层之后TimeDistributed和Dense层之间的区别吗?
我有以下代码:
class Test:
def __init__(self, name):
self.name = name
def __enter__(self):
print(f'entering {self.name}')
def __exit__(self, exctype, excinst, exctb) -> bool:
print(f'exiting {self.name}')
return True
with Test('first') as test:
print(f'in {test.name}')
test = Test('second')
with test:
print(f'in {test.name}')
Run Code Online (Sandbox Code Playgroud)
运行它会产生以下输出:
class Test:
def __init__(self, name):
self.name = name
def __enter__(self):
print(f'entering {self.name}')
def __exit__(self, exctype, excinst, exctb) -> bool:
print(f'exiting {self.name}')
return True
with Test('first') as test:
print(f'in {test.name}')
test = Test('second')
with test:
print(f'in {test.name}')
Run Code Online (Sandbox Code Playgroud)
但我预计它会产生:
entering first
exiting first
entering …Run Code Online (Sandbox Code Playgroud) 我正在研究GeekforGeeks网站上的教程,并注意到他们正在使用来检查数组中的点board[x,y],这是我从未见过的。我认为这行不通,但是当我运行程序时,一切都会按预期进行。
我尝试使用上面概述的方法和我更熟悉的方法(board[x][y])运行一个较小的代码示例,但是当我运行代码时,我得到了TypeError: list indices must be integers or slices, not tuple
我的代码:
board = [[1,1,1], [1,2,2], [1,2,2]]
win = 'True'
if board[1][1] == 2:
win = 'True by normal standards'
print(win)
if board[1, 1] == 2:
win = 'True by weird standards'
print(win)
print(win)
Run Code Online (Sandbox Code Playgroud)
他们的代码:
def row_win(board, player):
for x in range(len(board)):
win = True
for y in range(len(board)):
if board[x, y] != player:
win = False
continue
if win == …Run Code Online (Sandbox Code Playgroud) 我知道我可以使用它来获取完整的文件路径
os.path.dirname(os.path.realpath(__file__))
Run Code Online (Sandbox Code Playgroud)
但是我只想要文件夹的名称,我的脚本就在.如果我有my_script.py并且它位于
/home/user/test/my_script.py
Run Code Online (Sandbox Code Playgroud)
我想回"测试"我怎么能这样做?
谢谢
在Python中,有一种方法可以1.0转换为整数,1而同一函数会忽略该整数1.5并将其保留为float?
现在,int()将1.0变成,1但也将1.5向下舍入到1,这不是我想要的。
考虑以下:
>>> # list of length n
>>> idx = ['a', 'b', 'c', 'd']
>>> # list of length n
>>> l_1 = [1, 2, 3, 4]
>>> # list of length n
>>> l_2 = [5, 6, 7, 8]
>>> # first key
>>> key_1 = 'mkt_o'
>>> # second key
>>> key_2 = 'mkt_c'
Run Code Online (Sandbox Code Playgroud)
我怎么把这个烂摊子看起来像这样?
{
'a': {'mkt_o': 1, 'mkt_c': 5},
'b': {'mkt_o': 2, 'mkt_c': 6},
'c': {'mkt_o': 3, 'mkt_c': 6},
'd': {'mkt_o': 4, 'mkt_c': 7},
...
} …Run Code Online (Sandbox Code Playgroud)