如何在字典列表中满足条件的情况下计算不同的键?

huangapple go评论123阅读模式
英文:

how to count different keys with conditions in list of dictionaries

问题

  1. 我有一个字典列表看起来像这样它们在字典中有不同的键

my_dicts = [
{"id": "1", "fixVersion": "1.2.3", "releaseDate": "2017-01-21"},
{"id": "2", "fixVersion": "2.0", "releaseDate": "2023-01-21"},
{"id": "3", "fixVersion": "2.1", "releaseDate": "2023-07-01"},
{"id": "84", "changeRequests": "123"}
]

  1. 我想要计算如果在字典中有“releaseDate”作为键并且日期在2023-01-01之后的情况下,计数id
  2. 所以这个示例的最终结果应该是2
  3. ```python
  4. 2

任何帮助都会非常感激!

  1. <details>
  2. <summary>英文:</summary>
  3. I have a list of dicts looks like this: they have different keys in the dicts

my_dicts = [
{"id": "1","fixVersion": "1.2.3","releaseDate": "2017-01-21"},
{"id": "2","fixVersion": "2.0", "releaseDate": "2023-01-21"},
{"id": "3","fixVersion": "2.1", "releaseDate": "2023-07-01"},
{"id": "84","changeRequests":"123"}
]

  1. I want to count the id if there is &quot;releaseDate&quot; as the key in the dictionaries and also the date is after 2023-01-01.
  2. so the final result should be 2 for this example

2

  1. **any help is really appreciated!!**
  2. </details>
  3. # 答案1
  4. **得分**: 3
  5. 只需将您的话翻译成Python代码即可:
  6. ```python
  7. c = sum('releaseDate' in md and md['releaseDate'] >= '2023-01-01' for md in my_dicts)

尽管您甚至可以将其简化为:

  1. c = sum(md.get('releaseDate', '0000-00-00') >= '2023-01-01' for md in my_dicts)
英文:

It's just a matter of translating your words into Python code:

  1. c = sum(&#39;releaseDate&#39; in md and md[&#39;releaseDate&#39;] &gt;= &#39;2023-01-01&#39; for md in my_dicts)

although you could even simplify it to:

  1. c = sum(md.get(&#39;releaseDate&#39;, &#39;0000-00-00&#39;) &gt;= &#39;2023-01-01&#39; for md in my_dicts)

答案2

得分: 3

你可以在推导式中使用.get()来检查是否存在并比较值:

  1. sum(d.get("releaseDate", "") > "2023-01-01" for d in my_dicts) # 2
英文:

You can use .get() to check for presence and compare values in a comprehension:

  1. sum(d.get(&quot;releaseDate&quot;,&quot;&quot;)&gt;&quot;2023-01-01&quot; for d in my_dicts) # 2

答案3

得分: 1

你可以使用get()函数,它提供了从字典中安全提取值的方式。如果字典中没有这个键(默认为None),你可以定义想要获取的结果。对于你的情况,代码如下所示:

  1. filtered_dicts = [
  2. single_dict
  3. for single_dict in my_dicts
  4. if single_dict.get("releaseDate", "") >= "2023-01-01"
  5. ]

这将给你一个满足条件的字典列表。如果你只想计算它们的数量,你可以使用一行代码并加入len()函数。

  1. len(
  2. [
  3. single_dict
  4. for single_dict in my_dicts
  5. if single_dict.get("releaseDate", "") >= "2023-01-01"
  6. ]
  7. )
英文:

You can use get() function, that provides safe extraction from the dict. You can define what do you want to get as a result, if there's no such key in a dict (it's None by default).
For your case it would look like this:

  1. filtered_dicts = [
  2. single_dict
  3. for single_dict in my_dicts
  4. if single_dict.get(&quot;releaseDate&quot;, &quot;&quot;) &gt;= &quot;2023-01-01&quot;
  5. ]

This will give you the list of dicts that satisfy your condition. If you want to count them only, you can do a one-liner with throwing in len() function.

  1. len(
  2. [
  3. single_dict
  4. for single_dict in my_dicts
  5. if single_dict.get(&quot;releaseDate&quot;, &quot;&quot;) &gt;= &quot;2023-01-01&quot;
  6. ]
  7. )

huangapple
  • 本文由 发表于 2023年8月4日 04:08:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定