![](/img/trans.png)
[英]Python sort strings by count of appearances of the strings in the list
[英]Counting and comparing some repeatedly strings appearances between other strings in Python 3
我有這樣的輸入:
{"id": 123, "class": t1, "format": f1, "class-2": t1, ...}
{"id": 456, "class": t2, "format": f1, ...}
{"id": 567, "class": t1, "format": f2, "class-2": t2, "class-3": t1, ...}
...
我想要這樣的輸出:
123 = t1
456 = t2
567 = t1
...
(567分類比較t1和t2出現的次數)
我想re.search但沒有成功。 這是一個巨大的文本文件,我已將所有內容都放在同一行中,但我無法正確計算要比較的每個 id 之間的每個類出現。
我不是 100% 確定我明白你想要什么,因為你沒有任何代碼,這是我使用正則表達式的嘗試:
import re
string = """{"id": 123, "class": t1, "format": f1, "class-again": t1, ...}
{"id": 456, "class": t2, "format": f1, ...}
{"id": 567, "class": t1, "format": f2, "class-again": t2, "class-again": t1, ...} """
for id, cl in zip(re.findall('"id": (.+?),', string),
re.findall('"class": (.+?),', string)):
print('{} = {}'.format(id, cl))
輸出:
123 = t1
456 = t2
567 = t1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.