小编Chr*_*nen的帖子

Python新手:创建一个空集

我有一些代码可以设置一组选定的值.我想定义一个空集并添加到它,但{}不断变成字典.我发现如果我用虚拟值填充集合我可以使用它,但它不是很优雅.有人能告诉我这样做的正确方法吗?谢谢.

inversIndex = {'five': {1}, 'ten': {2}, 'twenty': {3},
               'two': {0, 1, 2}, 'eight': {2}, 'four': {1},
               'six': {1}, 'seven': {1}, 'three': {0, 2},
               'nine': {2}, 'twelve': {2}, 'zero': {0, 1, 3},
               'eleven': {2}, 'one': {0}}

query = ['four', 'two', 'three']

def orSearch(inverseIndex, query):
    b = [ inverseIndex[c] for c in query ]
    x = {'dummy'}
    for y in b:
        { x.add(z) for z in y }
    x.remove('dummy')
    return x

orSearch(inverseIndex, query)
Run Code Online (Sandbox Code Playgroud)

{0,1,2}

python

14
推荐指数
2
解决办法
3万
查看次数

重复模式

有人可以建议如何构建一个模式来从这些数据中提取第一个连续数字列表吗?

sample = {52.2624, 54.4003, 60.7418, 61.3801, 62.6397, 61.7992,
   63.2282, "", "", "", "", "", "", "", "", "", "", 62.3921, 61.897,
   60.299, 59.053, 61.3778, 64.3724, 63.4251, 78.1912, 79.7451,
   80.4741, "", 81.324, 79.9114, 93.7509};
Run Code Online (Sandbox Code Playgroud)

我尝试过各种变化sample //. {useable : _?NumberQ .., ___} -> {useable},但无济于事.

useable = TakeWhile[sample, NumberQ] 效果很好,但我想知道如何使用模式匹配来做到这一点.

wolfram-mathematica

8
推荐指数
3
解决办法
242
查看次数

使用ExportString转换图形

ExportString可以导出EMF或GIF吗?在这个演示中streamoutput.emf以某种方式被破坏:

Quiet[DeleteFile["C:\\Temp\\thisworks.emf"]];
Quiet[DeleteFile["C:\\Temp\\streamoutput.emf"]];

graphic = Graphics[{Thick, Red, Circle[{#, 0}] & /@ Range[4],
    Black, Dashed, Line[{{0, 0}, {5, 0}}]}];
Export["C:\\Temp\\thisworks.emf", graphic, "EMF"];

file = ExportString[graphic, "EMF"];
stream = OpenWrite["C:\\Temp\\streamoutput.emf", BinaryFormat -> True];
Write[stream, file];
Close[stream];
Run Code Online (Sandbox Code Playgroud)

如果ExportString有效,我可以使用它通过NETLink传输EMF,例如

kernel.Compute("ExportString[Graphics[Rectangle[]], \"EMF\"]");
File.WriteAllText("C:\\Temp\\output.emf", kernel.Result.ToString());
Run Code Online (Sandbox Code Playgroud)

附录

有那个工作.

kernel.Compute("ExportString[Graphics[Rectangle[]],{\"Base64\",\"EMF\"}]");
byte[] decodedBytes = Convert.FromBase64String(kernel.Result.ToString());
File.WriteAllBytes("C:\\Temp\\output.emf", decodedBytes);
Run Code Online (Sandbox Code Playgroud)

wolfram-mathematica

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

由JLink或UseFrontEnd生成的Uncaught Throw

此示例例程在内核窗口中生成两个Throw :: nocatch警告消息.它们可以以某种方式处理吗?

该示例包含在C:\ Temp中创建的文件"test.m"中的代码:

Needs["JLink`"];
$FrontEndLaunchCommand = "Mathematica.exe";
UseFrontEnd[NotebookWrite[CreateDocument[], "Testing"]];
Run Code Online (Sandbox Code Playgroud)

然后在Windows命令提示符下粘贴并运行这些命令:

PATH = C:\Program Files\Wolfram Research\Mathematica\8.0\;%PATH%
start MathKernel -noprompt -initfile "C:\Temp\test.m"
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

附录

使用UseFrontEnd而不是UsingFrontEnd的原因是可能需要交互式前端来保留通常以交互方式运行的笔记本的输出和消息.例如,使用C:\ Temp\test.m修改如下:

Needs["JLink`"];
$FrontEndLaunchCommand="Mathematica.exe";
UseFrontEnd[
nb = NotebookOpen["C:\\Temp\\run.nb"];
SelectionMove[nb, Next, Cell];
SelectionEvaluate[nb];
];
Pause[10];
CloseFrontEnd[];
Run Code Online (Sandbox Code Playgroud)

和一个包含以下单个单元格的笔记本C:\ Temp\run.nb:

x1 = 0; While[x1 < 1000000,
 If[Mod[x1, 100000] == 0,
  Print["x1=" <> ToString[x1]]]; x1++];
NotebookSave[EvaluationNotebook[]];
NotebookClose[EvaluationNotebook[]];
Run Code Online (Sandbox Code Playgroud)

此代码从Windows命令提示符启动,将以交互方式运行并保存其输出.使用UsingFrontEnd或MathKernel -script"C:\ Temp\test.m"无法实现.

wolfram-mathematica mathematica-frontend

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