簡體   English   中英

限制 output

[英]Limiting the output

我使用 .groupdict() function 制作了一本字典,但是,我在刪除某些 output 字典時遇到了問題。 例如我的代碼看起來像這樣(tweet 是一個字符串,包含 5 個由 || 分隔的元素:

 def somefuntion(pattern,tweet):
    pattern = "^(?P<username>.*?)(?:\|{2}[^|]+){2}\|{2}(?P<botprob>.*?)(?:\|{2}|$)"
      for paper in tweet:
         for item in re.finditer(pattern,paper):
              item.groupdict()

這會產生一個 output 的形式:

{'username': 'yashrgupta ', 'botprob': ' 0.30794588629999997 '}
{'username': 'sterector ', 'botprob': ' 0.39391528649999996 '}
{'username': 'MalcolmXon ', 'botprob': ' 0.05630123819 '}
{'username': 'ryechuuuuu ', 'botprob': ' 0.08492567222000001 '}
{'username': 'dpsisi ', 'botprob': ' 0.8300337045 '}

但我希望它只返回 botprob 高於 0.7 的字典。 我該怎么做呢?

具體來說,正如@WiktorStribizew 指出的那樣,只需跳過您不想要的迭代:

pattern = "^(?P<username>.*?)(?:\|{2}[^|]+){2}\|{2}(?P<botprob>.*?)(?:\|{2}|$)"
  for paper in tweet:
     for item in re.finditer(pattern,paper):
          item = item.groupdict()
          if item["botprob"] < 0.7:
              continue
          print(item)

這可以包裝在一個生成器表達式中以保存顯式的continue ,但在不增加閱讀難度的情況下(在本例中)已經足夠了。

更新,因為您顯然在 function 中:

pattern = "^(?P<username>.*?)(?:\|{2}[^|]+){2}\|{2}(?P<botprob>.*?)(?:\|{2}|$)"
  items = []
  for paper in tweet:
     for item in re.finditer(pattern,paper):
          item = item.groupdict()
          if float(item["botprob"]) > 0.7:
              items.append(item)
              
  return items

或者使用理解:

groupdicts = (item.groupdict() for paper in tweet for item in re.finditer(pattern, paper))
return [item for item in groupdicts if float(item["botprob"]) > 0.7]

我希望它只返回 botprob 高於 0.7 的字典。

entries = [{'username': 'yashrgupta ', 'botprob': ' 0.30794588629999997 '},
           {'username': 'sterector ', 'botprob': ' 0.39391528649999996 '},
           {'username': 'MalcolmXon ', 'botprob': ' 0.05630123819 '},
           {'username': 'ryechuuuuu ', 'botprob': ' 0.08492567222000001 '},
           {'username': 'dpsisi ', 'botprob': ' 0.8300337045 '}]

filtered_entries = [e for e in entries if float(e['botprob'].strip()) > 0.7]
print(filtered_entries)

output

[{'username': 'dpsisi ', 'botprob': ' 0.8300337045 '}]

暫無
暫無

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

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