合并两个对象列表
我想合并两个嵌套对象,以便将第二个对象放在第一个上的顶部。这是针对kubectl
config yaml,但它可能是任何具有list> list
s,dict
s and Simple Data类型的嵌套对象。例如:
# yaml 1:
containers:
- volumeMounts:
- name: external-stroage-1
mountPath: /mnt/stroage_1
readOnly: true
# Yaml 2
containers:
- name: cron
volumeMounts:
- name: internal-storage
mountPath: /mnt/data
合并的对象是:
containers:
- name: cron
- volumeMounts:
- name: external-stroage-1
mountPath: /mnt/stroage_1
readOnly: true
- name: internal-storage
mountPath: /mnt/data
这是我到目前为止所拥有的:
def merge(object_one, object_two):
assert type(object_one) == type(object_two), "Mismatched types"
if isinstance(object_one, dict):
for key in object_two:
if key in object_one:
object_one[key] = merge(object_one[key], object_two[key])
else:
object_one[key] = object_two[key]
elif isinstance(object_one, list):
for item in object_two:
object_one.append(item) # <<<<< when should I overwrite instead of append?
else:
return object_two
return object_one
大多数可以通过简单的递归来完成。它很容易确定应该在dict
中插入项目的位置,因为它是由密钥索引的。但是,当您拥有对象的list
时,如何确定是否应合并两个项目(如果列表订单不能保证是相同的)?又名,我如何确定列表中的项目是否需要覆盖与附加的项目?就目前而言,所有列表项目都附加了,这会导致不良合并:
containers:
- volumeMounts:
- name: external-stroage-1
mountPath: /mnt/stroage_1
readOnly: true
- name: external-stroage-2
mountPath: /mnt/stroage_2
- name: cron
volumeMounts: # This item should have been merged instead of being repeated
- name: internal-storage
mountPath: /mnt/data
I want to merge two nested objects so that the 2nd is layed out on top of the 1st. This is for a kubectl
config yaml but it could be any nested object that has a combination of list
s, dict
s and simple data types. eg:
# yaml 1:
containers:
- volumeMounts:
- name: external-stroage-1
mountPath: /mnt/stroage_1
readOnly: true
# Yaml 2
containers:
- name: cron
volumeMounts:
- name: internal-storage
mountPath: /mnt/data
And the merged object would be:
containers:
- name: cron
- volumeMounts:
- name: external-stroage-1
mountPath: /mnt/stroage_1
readOnly: true
- name: internal-storage
mountPath: /mnt/data
And here is what I have so far:
def merge(object_one, object_two):
assert type(object_one) == type(object_two), "Mismatched types"
if isinstance(object_one, dict):
for key in object_two:
if key in object_one:
object_one[key] = merge(object_one[key], object_two[key])
else:
object_one[key] = object_two[key]
elif isinstance(object_one, list):
for item in object_two:
object_one.append(item) # <<<<< when should I overwrite instead of append?
else:
return object_two
return object_one
Most of this can be done with simple recursion. Its easy to identify where an item should be inserted in a dict
since it's indexed by keys. But how do you identify if two items should be merged when you have a list
of objects (if the list order isn't guaranteed to be the same)? AKA, how do I determine if an item in a list needs to be overwritten vs appended? As it stands now, all list items are appended which leads to a bad merge:
containers:
- volumeMounts:
- name: external-stroage-1
mountPath: /mnt/stroage_1
readOnly: true
- name: external-stroage-2
mountPath: /mnt/stroage_2
- name: cron
volumeMounts: # This item should have been merged instead of being repeated
- name: internal-storage
mountPath: /mnt/data
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这最终比我希望的要多一些,并且必须对YAML结构做出一个很大的假设,如果它们有一个
dict> dict> dict
列表中的项目,则如果它们有一个相同的项目名称
键和匹配值(这在我们的kubectl yamls中很常见)。但是有了这个假设,这做出了工作:This ended up being a bit more bloated than I was hoping, and has to make a big assumption about the yaml structure, which is to assume two
dict
items within a list are the same item if they have aname
key and matching values (which is common in our kubectl yamls). But with that assumption, this did the job: