python实用库记录

1:Counter对列表进行计数,找重复

from collections import Counter

a = ["61e7f868eeb03046b96906dd","61e7f861fe380309df61ddf2","61e7f861fe380309df61ddf1",
     "61e7f861fe380309df61ddf0","61e7f85b7258f7696c55f72e","61e7f85b7258f7696c55f72d","61e7f861fe380309df61ddf0"
]

b = dict(Counter(a))  #{'61e7f868eeb03046b96906dd': 1, '61e7f861fe380309df61ddf2': 1, '61e7f861fe380309df61ddf1': 1, '61e7f861fe380309df61ddf0': 2, '61e7f85b7258f7696c55f72e': 1, '61e7f85b7258f7696c55f72d': 1}
print(b)
# print ([key for key,value in b.items() if value > 1]) #只展示重复元素
# print ({key:value for key,value in b.items() if value > 1}) #展现重复元素和重复次数

# a = [{'pay_order_no': '164264954668065756', 'is_del': 0}, {'pay_order_no': '164264972082245062', 'is_del': 1}, {'pay_order_no': '164264996092254304', 'is_del': 1}, {'pay_order_no': '164265021689661895', 'is_del': 0}, {'pay_order_no': '164265063783351017', 'is_del': 0}, {'pay_order_no': '164265075567892474', 'is_del': 0}, {'pay_order_no': '164265076330341903', 'is_del': 0}, {'pay_order_no': '164265087134341611', 'is_del': 1}, {'pay_order_no': '164265089388312149', 'is_del': 1}, {'pay_order_no': '164265101568633361', 'is_del': 0}]

2:asq。query

from asq import query

a = [
    {
        "card_id": 210,
        "donate_count": 6,
        "id": 210,
        "card_name": "这是一张犯人卡",
        "card_num": "AAA6",
        "card_grade_id": 158,
        "card_rarity": 6
    },{
        "card_id": 210,
        "donate_count": 6,
        "id": 210,
        "card_name": "这是一张犯人卡",
        "card_num": "AAA6",
        "card_grade_id": 158,
        "card_rarity": 6
    }, {
        "card_id": 210,
        "donate_count": 6,
        "id": 210,
        "card_name": "这是一张犯人卡",
        "card_num": "AAA6",
        "card_grade_id": 160,
        "card_rarity": 6
    }
]

a  = query(a).group_by(lambda x:x["card_grade_id"]).select(lambda x:[x[0]["card_grade_id"],x.sum(lambda x:x["donate_count"])]).to_list()
print(a) # [158, 12][160, 6]

  

 

 

posted @ 2022-01-25 13:26  你是我的神奇  阅读(37)  评论(0编辑  收藏  举报