如果我有一个简单的数据帧:
print(a)
one two three
0 A 1 a
1 A 2 b
2 B 1 c
3 B 2 d
4 C 1 e
5 C 2 f
Run Code Online (Sandbox Code Playgroud)
我可以通过发出以下命令轻松地在行上创建多索引:
a.set_index(['one', 'two'])
three
one two
A 1 a
2 b
B 1 c
2 d
C 1 e
2 f
Run Code Online (Sandbox Code Playgroud)
是否有类似的简单方法在列上创建多索引?
我想最终得到:
one A B C
two 1 2 1 2 1 2
0 a b c d e f
Run Code Online (Sandbox Code Playgroud)
在这种情况下,创建行多索引然后转置它会非常简单,但在其他示例中,我将要在行和列上创建多索引.
在后台启动交互式 python 控制台时,我遇到了一个奇怪的问题。从后台恢复解释器后,它不显示我输入的任何文本(即它只显示 >>> 提示,尽管它会解释我写的任何内容。按 [enter] 创建另一个 >>> 提示线)。
重现问题的一种简单方法是键入:
python &
fg
Run Code Online (Sandbox Code Playgroud)
如果在前台启动程序,将其置于后台,然后返回到前台,则不会出现此问题:
python
[ctrl-z]
bg
fg
Run Code Online (Sandbox Code Playgroud)
如果您想知道为什么要在后台启动交互式解释器,请考虑以下场景:
我有一个需要很长时间才能运行的模拟,但完成后,我想与结果进行交互。于是,我开始了这个程序:
python -i simulation.py &
fg #(after it's finished running)
Run Code Online (Sandbox Code Playgroud)
简单的解决方案是在前台启动它,将它移动到后台,然后将它带到前台,但我只是想知道为什么会发生这种情况。
我有以下代码,它使用并发.futures.ThreadPoolExecutor 以计量方式启动另一个程序的进程(一次不超过 30 个)。我还希望能够在 ctrl-C python 进程时停止所有工作。这段代码的工作有一个警告:我必须按 ctrl-C 两次。我第一次发送 SIGINT 时,没有任何反应;第二次,我看到“向进程发送 SIGKILL”,进程死亡,但它起作用了。我的第一个 SIGINT 发生了什么?
execution_list = [['prog', 'arg1'], ['prog', 'arg2']] ... etc
processes = []
def launch_instance(args):
process = subprocess.Popen(args)
processes.append(process)
process.wait()
try:
with concurrent.futures.ThreadPoolExecutor(max_workers=30) as executor:
results = list(executor.map(launch_instance, execution_list))
except KeyboardInterrupt:
print('sending SIGKILL to processes')
for p in processes:
if p.poll() is None: #If process is still alive
p.send_signal(signal.SIGKILL)
Run Code Online (Sandbox Code Playgroud) 与此问题类似,有什么方法可以将同一文件中定义的函数提交给 python-rq?@GG_Python 谁让我为此创建一个新问题。
用法示例:
# somemodule.py
from redis import Redis
from rq import Queue
def somefunc():
do_something()
q = Queue(connection=Redis('redis://redis'))
q.enqueue(somefunc)
Run Code Online (Sandbox Code Playgroud)
是的,我知道答案是在 someothermodule.py 中定义 somefunc ,然后在上面的代码段中定义from someothermodule import somefunc,但我真的不想。也许我对表单过于执着,但 somefunc 确实属于它排队的同一个文件(实际上, somefunc 接受一个 docker 容器名称并生成它)。我真的希望整个事情都是自包含的,而不是有两个模块。
我注意到,通过挖掘 python-rq 源代码,Queue.enqueue 实际上可以接受一个字符串而不是实际的模块,所以我希望我可以通过somemodule.somefunc,但没有那么幸运。有任何想法吗?
JSON for Modern C++使用以下语法:
json j = "{ \"happy\": true, \"pi\": 3.141 }"_json;
Run Code Online (Sandbox Code Playgroud)
我想知道他们是如何完成这件事的.
我不懂字符串文字_json语法.