我在struct Foo中有一个“平面”数据数组,其中包含成员父级和子级:
struct Foo { string parent, string child }
他们可能有这样的数据:
"parent1", "child1"
"parent1", "child2"
"parent2", "child3"
"parent2", "child4"
我想强迫他们加入“关系”结构:
struct Bar { string parent, string[] children }
并且如此填充:
"parent1"
"child1"
"child2"
"parent2"
"child3"
"child4"
我可以通过循环做得很好,但希望提高我的linq技能......我敢肯定必须有办法吗?谢谢(4.5框架)
答案 0 :(得分:4)
使用GroupBy:
def remove_last_odd(numbers):
idx = len(numbers)
last_odd = False
while not last_odd and idx > 0:
idx -= 1
last_odd = (numbers[idx] % 2 != 0)
return numbers[0:idx]