gpt4 book ai didi

d - dlang 中是否有像 python popitem 这样的关联数组的方法?

转载 作者:行者123 更新时间:2023-12-04 11:03:34 24 4
gpt4 key购买 nike

我想从关联数组中获取任何键/值对并将其删除。
在python中它是:

key, value = assoc.popitem()

在 D 我做:
auto key = assoc.byKey.front;
auto value = assoc[key];
assoc.remove(key);

有没有更好的方法来做到这一点?是否可以在 foreach 之外使用 byKeyValue()?

DMD 2.067.1

最佳答案

Is it possible to use byKeyValue() outside foreach?



当然:
import std.stdio;

void main()
{
int[string] assoc = ["apples" : 2, "bananas" : 4];

while (!assoc.byKeyValue.empty)
{
auto pair = assoc.byKeyValue.front;
assoc.remove(pair.key);
writeln(pair.key, ": ", pair.value);
}
}

Is there better way to do this?



我认为 D 没有与 popitem 等效的库函数.

关于d - dlang 中是否有像 python popitem 这样的关联数组的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31004920/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com