IronPython for C#(二)

python中有4中不同的数据容器,那分别对应着C#中的哪种数据结构呢?

PythonPython描述C#
列表list有序可变的,其中的每个值类型可以不一样List<object>,Array,HashSet<object>
元组tuple有序但是值不可改变,值类型可以不一样List<object>,Array,HashSet<object>
字典dict键值对存在,键是唯一的,键和值的类型都可以不一样Dictionary<object, object>
集合set无序不能出现重复值,值类型可以不一样List<object>,Array,HashSet<object>

1.列表

list1 = [1,"2",3]
def getlist(list2):
    return list2[0]
using System;
using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
using IronPython.Runtime;
class Program
{
   static void Main(string[] args)
   {
       ScriptRuntime pyRuntime = Python.CreateRuntime();
       dynamic scope = pyRuntime.UseFile("test.py");
       List list = scope.list1;
       List<object> list1 = list.ToList<object>();
       HashSet<object> hashSet = list.ToHashSet();
       Array array = list.ToArray();
       object list0 = scope.getlist(new int[] { 5, 2 });
    }
}

 2.元组

tuple1 = (1,"2",3)
def gettuple(tuple2):
    return tuple2[0]
using System;
using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
using IronPython.Runtime;
class Program
{
    static void Main(string[] args)
    {
        ScriptRuntime pyRuntime = Python.CreateRuntime();
        dynamic scope = pyRuntime.UseFile("test.py");
        PythonTuple pTuple = scope.tuple1;
        List<object> list = pTuple.ToList();
        HashSet<object> hashSet = pTuple.ToHashSet();
        Array array = pTuple.ToArray();
        object res = scope.gettuple(new int[] { 5, 2 });
    }
}

 3.字典

dict1 = {1:2,"3":4,5:"6","7":"8"}
def getdict(dict2):
    return dict2["3"]
using System;
using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
using IronPython.Runtime;
class Program
{
    static void Main(string[] args)
    {
        ScriptRuntime pyRuntime = Python.CreateRuntime();
        dynamic scope = pyRuntime.UseFile("test.py");
        PythonDictionary pDict = scope.dict1;
        Dictionary<object, object> dict = pDict.ToDictionary(k => k.Key, v => v.Value);
        object res = scope.getdict(new Dictionary<object, object>() { { "3", 5 } });
    }
}

 4.集合

set1 = {1,2,"3",2}
def getset(set2):
    return set2[0]
using System;
using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
using IronPython.Runtime;
class Program
{
    static void Main(string[] args)
    {
        ScriptRuntime pyRuntime = Python.CreateRuntime();
        dynamic scope = pyRuntime.UseFile("test.py");
        SetCollection pSet = scope.set1;   //这里会返回3个值,因为set会去重
        List<object> list = pSet.ToList();
        Array array = pSet.ToArray();
        HashSet<object> hashSet = pSet.ToHashSet();
        object res = scope.getset(new List<object>() { "3", 5, 6, 1, "3" });
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bridge_go

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值