C#调用Python(一),,python文件中未
C#调用Python(一),,python文件中未
python文件中未引入其他包、模块
一、安装IronPython包,使用的是2.7.5版本
二、源码
2.1 python 源码,实现一个快速排序功能
def quickSort(lyst): quickSortHelper(lyst, 0, len(lyst) - 1) return lystdef quickSortHelper(lyst, left, right): if left < right: pivotLocation = partition(lyst, left, right) quickSortHelper(lyst, left, pivotLocation - 1) quickSortHelper(lyst, pivotLocation + 1, right)def swap(lst, i, j): temp = lst[i] lst[i] = lst[j] lst[j] = tempdef partition(lyst, left, right): middle = (left + right) // 2 pivot = lyst[middle] lyst[middle] = lyst[right] lyst[right] = pivot boundary = left for index in range(left, right): if lyst[index] < pivot: swap(lyst, index, boundary) boundary += 1 swap(lyst, right, boundary) return boundary
2.2 C#源码
using IronPython.Hosting;using Microsoft.Scripting.Hosting;namespace TestClass{ public class CSharpCallPython { public int[] Sort(int[] arr) { string path = @"D:\PyCharm\source\Study\test.py"; ScriptRuntime pyRuntime = Python.CreateRuntime(); dynamic py = pyRuntime.UseFile(path); return py.quickSort(arr); } }}
var arr = new CSharpCallPython().Sort(new int[] { 45, 12, 87, 2, 204, 3 }); foreach (var item in arr) { Console.WriteLine(item); } Console.ReadLine();
执行结果:
C#调用Python(一)
相关内容
- python--enum,,# enum用于枚举
- Python3---内建函数---any(),,前言该文章描述了函数
- python_面向对象编程,,初始面向对象一、类的
- python读取ini配置的类封装,,此为基础封装,未考虑
- python中的next()以及iter()函数,,我们首先要知道什么是
- Python面向对象 | 组合,,软件重用的重要方式,
- Python全栈自动化系列之Python编程基础(基础语法),
- /usr/lib/python2.7/site-packages/requests/__init__.py:91: RequestsDep
- python应用:经纬度匹配,,需要安装第三方包:r
- Python2 和Python3 的差异总结,,一、基本语法差异1.
评论关闭