【问题标题】:Create a list of dict using other list of dict data in c#使用 c# 中的其他 dict 数据列表创建 dict 列表
【发布时间】:2020-05-14 10:59:18
【问题描述】:

我是 C# 新手。谁能帮我解决我的问题?

我的问题是

我在某个 x 变量中有一个 dict 列表。我的字典看起来像下面这样。

x = [
        {
            "name": "rtu_dnp3",
            "ipv4": "10.46.224.109/32",
            "loopback": "yes",
            "sap": "1/3/1",
            "type": "serial"
        },
        {
            "name": "rtu_ea",
            "ipv4": "10.46.224.109/32",
            "loopback": "yes",
            "sap": "1/3/2",
            "type": "serial"
        }
]

使用这本字典我想创建另一个字典。我在 python 中很容易做到这一点。

y = []
for i in x:
    dict = {}
    dict["name"] = i["name"]
    dict["ipv4"] = i["ipv4"]
    y.append(dict)

那么任何人都可以帮助我如何在 C# 代码中进行操作吗?所以我可以玩这个字典来创建我自己的格式。

【问题讨论】:

标签: c# list dictionary


【解决方案1】:

您可以将元组用于您的字典,例如,

Dictionary<string, Tuple<string, string, string, string>> yourDict = new Dictionary<string, Tuple<string, string, string, string>>();
yourDict.Add("rtu_dnp3", new Tuple<string, string, string, string>("10.46.224.109/32", "yes", "1/3/1", "serial"));
yourDict.Add("rtu_ea", new Tuple<string, string, string, string>("10.46.224.109/32", "yes", "1/3/2", "serial"));

或者你可以创建你的模型而不是像元组一样,

Dictionary<string, YourModel> yourDict = new Dictionary<string, YourModel>();
yourDict.Add("rtu_dnp3", new YourModel("10.46.224.109/32", "yes", "1/3/1", "serial"));
yourDict.Add("rtu_ea", new YourModel("10.46.224.109/32", "yes", "1/3/2", "serial"));

public class YourModel
{
    public string ipv4 { get; set; }
    public string loopback { get; set; }
    public string sap { get; set; }
    public string type { get; set; }

    public YourModel(string ipv4, string loopback, string sap, string type)
    {
        this.ipv4 = ipv4;
        this.loopback = loopback;
        this.sap = sap;
        this.type = type;
    }
}

在这种情况下,字符串是您的 key,YourModel 是您的 value

- 更新

Dictionary 保存一个 TKey 和一个 TValue。所以你应该使用一些结构或类(你的类或像元组这样的预定义类)来保留多个参数。让我们继续使用元组示例,

你可以像这样循环复制你的字典,

Dictionary<string, Tuple<string, string, string, string>> yourDict = new Dictionary<string, Tuple<string, string, string, string>>();
yourDict.Add("rtu_dnp3", new Tuple<string, string, string, string>("10.46.224.109/32", "yes", "1/3/1", "serial"));
yourDict.Add("rtu_ea", new Tuple<string, string, string, string>("10.46.224.109/32", "yes", "1/3/2", "serial"));

var newDict = new Dictionary<string, Tuple<string, string, string, string>>();

foreach (var item in yourDict)
{
    newDict.Add(item.Key,item.Value);
}

【讨论】:

  • 嗨@Furkan Öztürk,您可以考虑我的python 代码并尝试使用循环而不使用任何类来给出一个简单的答案。对我有帮助
  • 我希望这会有所帮助。此外,您应该提供更多关于您的案例的详细信息,以便我们提供帮助@DhananjayaDN
【解决方案2】:

考虑下一个C# 代码示例。我认为它相当于您提供的Python代码:

var x = new List<Dictionary<string, string>>
{
    new Dictionary<string, string>
    {
        ["name"] = "rtu_dnp3",
        ["ipv4"] = "10.46.224.109/32",
        ["loopback"] = "yes",
        ["sap"] = "1/3/1",
        ["type"] = "serial"
    },
    new Dictionary<string, string>
    {
        ["name"] = "rtu_ea",
        ["ipv4"] = "10.46.224.109/32",
        ["loopback"] = "yes",
        ["sap"] = "1/3/2",
        ["type"] = "serial"
    }
};

var y = new List<Dictionary<string, string>>();

foreach (Dictionary<string, string> i in x)
{
    var dict = new Dictionary<string, string>();
    dict["name"] = i["name"];
    dict["ipv4"] = i["ipv4"];
    y.Add(dict);
}

这里是complete sample,它还将结果字典列表y 打印到输出窗口。

这里是了解C#'s集合ListDictionary的链接。

【讨论】:

    猜你喜欢
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    相关资源
    最近更新 更多