簡體   English   中英

如何使用 Python 計算嵌套字典 in.json 文件中某個項目的出現次數(並可能迭代)?

[英]How can I count occurrence of an item in nested dictionaries in .json file using Python (and possibly iterate over)?

我正在嘗試一段時間,但由於我仍然是一個初學者,所以我很難過。 我有一個帶有 jsons 的文件,它們都具有以下結構:

{
   "cds":{
      "ENSLAFT00000035968.1":{
         "A":407,
         "C":312,
         "G":320,
         "T":320,
         "Y":0,
         "M":0,
         "S":0,
         "R":0,
         "W":0,
         "K":0,
         "N":0,
         "D":0,
         "B":0,
         "H":0,
         "V":0,
         "all":1359
      },
      "cdna":{
         "ENSLAFT00000034174.1":{
            "A":825,
            "C":700,
            "G":663,
            "T":584,
            "Y":0,
            "M":0,
            "S":0,
            "R":0,
            "W":0,
            "K":0,
            "N":0,
            "D":0,
            "B":0,
            "H":0,
            "V":0,
            "all":2772
         }
      }
   }

第一個鍵(cds 和 cdna)每個都有大約 1000 多個值(基因,ENSLAFT+數字)。 我想計算所有“N”次出現(如果有些有 50 次,有些有 10 次,則將它們加在一起並有 60 次)。 collections Counter sum()len()或它們的某種組合以某種方式...? 以及如何使用具有相同結構的 jsons 為我的文件夾中的每個文件創建一個這樣的循環? 這對我來說聽起來很容易,但我沒有太多經驗,到目前為止,我只能使用 pandas DataFrame 或不那么復雜的數據來計算......

我感謝任何幫助和進一步的學習建議!

您可以使用 for 循環:

data = {"cds": {"ENSLAFT00000035968.1": {"A": 407, "C": 312, "G": 320, "T": 320, "Y": 0, "M": 0, "S": 0, "R": 0, "W": 0, "K": 0, "N": 0, "D": 0, "B": 0, "H": 0, "V": 0, "all": 1359}}, "cdna": {"ENSLAFT00000034174.1": {"A": 825, "C": 700, "G": 663, "T": 584, "Y": 0, "M": 0, "S": 0, "R": 0, "W": 0, "K": 0, "N": 0, "D": 0, "B": 0, "H": 0, "V": 0, "all": 2772}}}
counter = 0
for value in data.values():
    # key would be cds or cdna, value is the dict of genes
    for gene in value.values():
        # key would be ENSLAFT00000035968.1, ...
        if 'N' in gene:
            counter += gene['N']
print(counter)

您可以檢查密鑰以僅計算一些:

counter = 0
for key, value in data.items():
    # key would be cds or cdna, value is the dict of genes
    if key == "cds":
        for gene in value.values():
            # key would be ENSLAFT00000035968.1, ...
            if 'N' in gene:
                counter += gene['N']
print(counter)

您可以通過蠻力尋找 go 在 JSON 的字符串版本中尋找正則表達式,例如

import json
import re

s = {"cds": {"ENSLAFT00000035968.1": {"A": 407, "C": 312, "G": 320, "T": 320, "Y": 0, "M": 0, "S": 0, "R": 0, "W": 0, "K": 0, "N": 0, "D": 0, "B": 0, "H": 0, "V": 0, "all": 1359}, "cdna": {"ENSLAFT00000034174.1": {"A": 825, "C": 700, "G": 663, "T": 584, "Y": 0, "M": 0, "S": 0, "R": 0, "W": 0, "K": 0, "N": 0, "D": 0, "B": 0, "H": 0, "V": 0, "all": 2772}}}}


s_str = json.dumps(s)
m = re.findall(r'"N":\s(\d+)', s_str)
print(m)  # prints ['0', '0']
print(len(m))  # prints 2

或者 go 是遞歸 function 的更清潔、更長的路線...

def rec_find(s, cur=0):
  if type(s) not in (dict, ):
    return 0
  resp = 0
  if "N" in s.keys():
    resp += 1
  for k in s.keys():
    resp += rec_find(s[k], resp)
  return resp

print(rec_find(s))  # prints 2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM