[英]Removing key values pairs from a list of dictionaries
I have a list of dictionaries such as:我有一个字典列表,例如:
[{'mykey1':'myvalue1', 'mykey2':'myvalue2'}, {'mykey1':'myvalue1a', 'mykey2':'myvalue2a'}]
I need to remove all key values pairs from all dictionaries where the key is equal to mykey1.我需要从键等于 mykey1 的所有字典中删除所有键值对。 I could do this by looping through and using the del statement, but I am wondering how I would create a new list using list comprehensions or lambdas which would just remove all key value pairs where the key was mykey1.
我可以通过循环并使用 del 语句来做到这一点,但我想知道如何使用列表推导或 lambdas 创建一个新列表,它只会删除键为 mykey1 的所有键值对。
Many thanks非常感谢
I have a list of dictionaries such as:我有一个字典列表,例如:
[{'mykey1':'myvalue1', 'mykey2':'myvalue2'}, {'mykey1':'myvalue1a', 'mykey2':'myvalue2a'}]
I need to remove all key values pairs from all dictionaries where the key is equal to mykey1.我需要从所有键等于mykey1的字典中删除所有键值对。 I could do this by looping through and using the del statement, but I am wondering how I would create a new list using list comprehensions or lambdas which would just remove all key value pairs where the key was mykey1.
我可以通过遍历并使用del语句来做到这一点,但我想知道如何使用列表推导或lambda创建一个新列表,该列表将删除键为mykey1的所有键值对。
Many thanks非常感谢
I have a list of dictionaries such as:我有一个字典列表,例如:
[{'mykey1':'myvalue1', 'mykey2':'myvalue2'}, {'mykey1':'myvalue1a', 'mykey2':'myvalue2a'}]
I need to remove all key values pairs from all dictionaries where the key is equal to mykey1.我需要从所有键等于mykey1的字典中删除所有键值对。 I could do this by looping through and using the del statement, but I am wondering how I would create a new list using list comprehensions or lambdas which would just remove all key value pairs where the key was mykey1.
我可以通过遍历并使用del语句来做到这一点,但我想知道如何使用列表推导或lambda创建一个新列表,该列表将删除键为mykey1的所有键值对。
Many thanks非常感谢
I have a list of dictionaries such as:我有一个字典列表,例如:
[{'mykey1':'myvalue1', 'mykey2':'myvalue2'}, {'mykey1':'myvalue1a', 'mykey2':'myvalue2a'}]
I need to remove all key values pairs from all dictionaries where the key is equal to mykey1.我需要从所有键等于mykey1的字典中删除所有键值对。 I could do this by looping through and using the del statement, but I am wondering how I would create a new list using list comprehensions or lambdas which would just remove all key value pairs where the key was mykey1.
我可以通过遍历并使用del语句来做到这一点,但我想知道如何使用列表推导或lambda创建一个新列表,该列表将删除键为mykey1的所有键值对。
Many thanks非常感谢
I have a list of dictionaries such as:我有一个字典列表,例如:
[{'mykey1':'myvalue1', 'mykey2':'myvalue2'}, {'mykey1':'myvalue1a', 'mykey2':'myvalue2a'}]
I need to remove all key values pairs from all dictionaries where the key is equal to mykey1.我需要从所有键等于mykey1的字典中删除所有键值对。 I could do this by looping through and using the del statement, but I am wondering how I would create a new list using list comprehensions or lambdas which would just remove all key value pairs where the key was mykey1.
我可以通过遍历并使用del语句来做到这一点,但我想知道如何使用列表推导或lambda创建一个新列表,该列表将删除键为mykey1的所有键值对。
Many thanks非常感谢
I have a list of dictionaries such as:我有一个字典列表,例如:
[{'mykey1':'myvalue1', 'mykey2':'myvalue2'}, {'mykey1':'myvalue1a', 'mykey2':'myvalue2a'}]
I need to remove all key values pairs from all dictionaries where the key is equal to mykey1.我需要从所有键等于mykey1的字典中删除所有键值对。 I could do this by looping through and using the del statement, but I am wondering how I would create a new list using list comprehensions or lambdas which would just remove all key value pairs where the key was mykey1.
我可以通过遍历并使用del语句来做到这一点,但我想知道如何使用列表推导或lambda创建一个新列表,该列表将删除键为mykey1的所有键值对。
Many thanks非常感谢
It's nice to have a one liner list comprehensions but the performance might not be as good as del operation.有一个单行列表理解很好,但性能可能不如 del 操作。 See my test results below:
在下面查看我的测试结果:
%%timeit
dl = [
{"foo": 1, "bar": 2},
{"foo": 1, "bar": 2},
{"foo": 1, "bar": 2},
{"foo": 1, "bar": 2},
{"foo": 1, "bar": 2},
{"foo": 1, "bar": 2},
{"foo": 1, "bar": 2},
{"foo": 1, "bar": 2}
]
dl1 = [{key: value for key, value in d.items() if key != 'foo'} for d in dl]
2.78 µs ± 62.7 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
每个循环 2.78 µs ± 62.7 ns(平均值 ± 标准偏差。7 次运行,每次 100,000 次循环)
%%timeit
dl = [
{"foo": 1, "bar": 2},
{"foo": 1, "bar": 2},
{"foo": 1, "bar": 2},
{"foo": 1, "bar": 2},
{"foo": 1, "bar": 2},
{"foo": 1, "bar": 2},
{"foo": 1, "bar": 2},
{"foo": 1, "bar": 2}
]
for row in dl:
del row['foo']
640 ns ± 13.7 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
每个循环 640 ns ± 13.7 ns(平均值 ± 标准偏差。7 次运行,每次 1,000,000 次循环)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.