【微软的C#难学吗?和Python比起来】教程文章相关的互联网学习教程文章

什么是Python的min / max的C#等价【代码】

什么是C#的以下Python的最小/最大代码的等价:pairs = [ (2,"dog"), (1, "cat"), (3, "dragon"), (1, "tiger") ]# Returns the PAIR (not the number) that minimizes on pair[0] min_pair = min(pairs, key=lambda pair:pair[0])# this will return (1, 'cat'), NOT 1似乎C#的Enumerable.Min非常接近.但是根据its MSDN doc,它总是返回最小值VALUE(不是原始对象).我错过了什么吗? 编辑 请注意 – 由于排序(O(nlogn))在计算上比找到...

c# – 将多个参数从IronPython传递给.NET方法【代码】

我有一个.NET(C#)类:public class MyHelper {public object exec( string script, params object[] arguments ) {// execute script with passed arguments in some external enviroment} }我在我的代码中使用IronPython运行时来运行python脚本,在某些情况下应该调用“exec”方法.我想以舒适的方式调用“exec”方法.就像是:helper.exec( "someExternalFunction( {0}, {1}, {3} )", var01, var02, var03 )但我不知道如何在C#中声明...

有没有办法将C#序列化对象读入Python?【代码】

我有一个包含C#序列化对象的二进制文件. 我可以用python读取内容,但得到的结果类似于:'T\x00\x00\x00Test.Jobs.GenerateJobRequest, POC.Server\xca\x02-\xa2\x02\t\x82\x01\x06\x1a\x04myahR\x1d\x08\xfe\xff\xff\xff\xff\xff\xff\xff\xff\x01\x12\x10Data Lite Exportp\t\n\x16Do_Ko_Change-Job__ID_23\x10\x0c\x18\xa7\xb9\x18(\x012\x00:\x00H\xbc\x08')有没有办法在python中反序列化这个对象? 我同意这不是最佳解决方案,而J...

python 3.5中的async / await关键字是否受到C#中async / await的启发?【代码】

python 3.5中的async / await(语法和关键字)与C#中的async / await非常相似. C#示例:async void asyncTask(){await asyncMethod() }Python示例:async def asyncTask(): await async_method()问题:python 3.5中的async / await是否受到C#中async / await的启发?如果是,为什么?解决方法:在PEP 492(添加await和async关键字的提议)中,C#使用它们是mentioned(除了其他):Why “async” and “await” keywords async/await is not...

从C#应用程序将命令行参数传递给IronPython?【代码】

如何将命令行参数从我的C#应用??程序传递到IronPython 2.x? Google仅返回有关如何使用Iron Python 1.x执行此操作的结果.static void Main(string[] args) {ScriptRuntime scriptRuntime = IronPython.Hosting.Python.CreateRuntime();// Pass in script file to execute but how to pass in other arguments in args?ScriptScope scope = scriptRuntime.ExecuteFile(args[0]); }解决方法:您可以通过以下C#代码设置sys.argv:stati...

元组在C#中展开类似于Python【代码】

参见英文答案 > Possible to initialize multiple variables from a tuple? 7个在Python中,我们可以使用类似的语法展开元组:a, b = (1, 2)C#中是否有类似的结构?或者访问以下元素:Tuple<int, int> t = Tuple.Create(1, 2); Console.Write(t.Item1);唯一可行的方法?解决方法:元组解构(有时称为“爆炸”),即将其元素分布在多个变量上,并不是C#语言直接支持的. 您可以编写自己的扩展方法:sta...

将c#类型注入Ironpython【代码】

现在我可以使用以下代码访问IronPython中的c#类型,如下所示import clr clr.AddReference('myDLL.dll') import myType obj = myType()但我不希望脚本开发人员在Python源代码中使用clr.AddReference(‘myDLL.dll’)行,并将myDLL.dll(和/或ac#class)直接从c#注入ScriptEngine,因此前面的代码将是类似的东西:import myTypeobj = myType() 我该怎么做到这一点?解决方法:您可以使用以下解决方案解决此问题:ScriptRuntime runtime = P...

C#脚本(python)【代码】

是否存在在C#和Python中进行互操作的方法? 我知道并且已经使用过IronPython(显然,它使用dynamic关键字变得非常甜蜜),它允许在C#应用程序中解释Python脚本. 但我希望这个Python脚本能够访问C#类和方法.我知道,例如,这可以使用boost :: python和C实现,但是我应该如何(如果可能的话)使用C#来实现它? 谢谢.解决方法:那么你可以使用csc和use ctypes to access the DLL from your script将你的C#代码编译成DLL,但是如果你可以使用IronP...

[源码]Python调用C# DLL例子(Python与.Net交互)【代码】

K8Cscan C# DLL例子代码namespace CscanDLL { public class scan { public static string run(string ip) { if (string.IsNullOrEmpty(ip)) return ""; else return ip; } } } Python 调用C# DLL代码import clr #pythonnet print(‘python call K8Cscan c# dll’) clr.FindAssembly(‘netscan.dll’) clr.AddReference(‘netscan’) from CscanDLL import * print(scan.run(‘192.168.1.1’)) 编译好的DLL https:...

Python的os.path库在C#中等效【代码】

假设我在C#代码ae /a/b/c/xyz.exe中有一个二进制文件,它需要/a/b/c/hello.txt在同一目录中.如何在完整路径中获取/a/b/c/hello.txt? 在python中,我可以使用os.path.abspath(sys.argv [0])获取正在运行的程序的路径,并且我可以使用dirname()获取目录信息,并且我可以使用join()来获取新的完整路径.import sys from os.path import * newName = join(dirname(abspath(sys.arg[0]), "hello.txt")C#怎么能做同样的事情?解决方法:您可以...

C#调用Python代码

C#中,如果碰到需要调用Python代码时,一种方法是使用IronPython,不过这种方法太繁琐太累,特别是碰到Python代码中带有大量的第三方包,就会一直报错,提示缺少相应模块,这种方法太low,只支持Python2代码,果断摒弃。推荐另一种方法是用pyinstaller打包Python程序,会自动将程序中引用的第三方包也打包进去,Python3.X随便用,很方便。pyinstaller怎么安装就不用说了,下面介绍下pyinstaller打包的过程和c#调用的过程。

c# – 如何将Asp.net页面与Python项目集成?【代码】

我有一个使用Asp .Net MVC开发的网页,我有一个用Python开发的项目,它有一些使用机器学习进行预测的方法.在这种情况下,如何让我的网页与Python中的项目进行通信并使用那里实现的方法?解决方法:假设你的python文件名是script.py,你可以创建一个如下所示的批处理文件:python C:\Users\script.py 然后从ASP.NET程序中调用批处理文件(script_runner.bat),如下所示:Process.Start('C:\Users.script_runner.bat');另一种可能的解决方案...

XML-RPC C#和Python RPC服务器【代码】

在我的服务器上,我使用Python的标准示例(使用额外的Hello World方法),在客户端,我使用C#中的XML-RPC.NET库.但每次我运行我的客户端时,我都会得到一个例外,即找不到该方法.任何想法如何解决. 谢谢! Python:from SimpleXMLRPCServer import SimpleXMLRPCServer from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler# Restrict to a particular path. class RequestHandler(SimpleXMLRPCRequestHandler):rpc_paths = ('/RPC2...

在c#,c和java中创建一个python弱类型结构的强类型版本【代码】

在python我有以下内容:graph = {}graph[1] = {} graph[2] = {} graph[3] = {}graph[1][3] = graph[3]graph[2][1] = graph[1] graph[2][3] = graph[3]graph[3][2] = graph[2]这是一个表示图形的结构,我发现它很好,因为它的结构与其中一个节点的结构相同,因此我可以直接使用它来启动搜索(如深度优先).它的印刷版本是:{1: {3: {2: {1: {...}, 3: {...}}}}, 2: {1: {3: {2: {...}}}, 3: {2: {...}}}, 3: { 2: {1: {3: {...}}, 3: {.....

IronPython w / C# – 如何读取Python变量的值【代码】

我有两个python文件:mainfile.py和subfile.py mainfile.py依赖于subfile.py中的某些类型. mainfile.py看起来像这样.from subfile import * my_variable = [1,2,3,4,5]def do_something_with_subfile#Do something with things in the subfile.#Return something.我正在尝试在C#中加载mainfile.py并获取my_varaible的值,但是我在查找充分描述我正在调用的方法之间关系的资源时遇到了一些困难,我承认,我不知道关于Python. 这是我写的...

微软 - 相关标签