亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python - 從嵌套列表中提取值

Python - 從嵌套列表中提取值

桃花長相依 2022-09-20 16:04:26
我有一個列表,如下所示:[{'id': 'id_123',  'type': 'type_1',  'created_at': '2020-02-12T17:45:00Z'}, {'id': 'id_124',  'type': 'type_2',  'created_at': '2020-02-12T18:15:00Z'}, {'id': 'id_125',  'type': 'type_1',  'created_at': '2020-02-13T19:43:00Z'}, {'id': 'id_126',  'type': 'type_3',  'created_at': '2020-02-13T07:00:00Z'}]我試圖找出發生多少次,以及該列表中最早的時間戳是什么type : type_1created_attype_1
查看完整描述

5 回答

?
慕哥9229398

TA貢獻1877條經驗 獲得超6個贊

我們可以通過幾個步驟來實現這一目標。

要查找發生的次數,我們可以將內置過濾器與 itemgetter 結合使用。type_1

from operator import itemgetter


def my_filter(item):

    return item['type'] == 'type_1'


key = itemgetter('created_at')


items = sorted(filter(my_filter, data), key=key)

print(f"Num records is {len(items)}")

print(f"Earliest record is {key(items[0])}")


Num records is 2

Earliest record is 2020-02-12T17:45:00Z

相反,您可以使用生成器理解,然后對生成器進行排序。


gen = (item for item in data if item['type'] == 'type_1')

items = sorted(gen, key=key)

# rest of the steps are the same...


查看完整回答
反對 回復 2022-09-20
?
蠱毒傳說

TA貢獻1895條經驗 獲得超3個贊

您可以使用列表理解來獲取您感興趣的所有子列表,然后按“created_at”進行排序。


l = [{'id': 'id_123',

  'type': 'type_1',

  'created_at': '2020-02-12T17:45:00Z'},

 {'id': 'id_124',

  'type': 'type_2',

  'created_at': '2020-02-12T18:15:00Z'},

 {'id': 'id_125',

  'type': 'type_1',

  'created_at': '2020-02-13T19:43:00Z'},

 {'id': 'id_126',

  'type': 'type_3',

  'created_at': '2020-02-13T07:00:00Z'}]


ll = [x for x in l if x['type'] == 'type_1']

ll.sort(key=lambda k: k['created_at'])

print(len(ll))

print(ll[0]['created_at'])

輸出:


2

02/12/2020 17:45:00


查看完整回答
反對 回復 2022-09-20
?
慕妹3146593

TA貢獻1820條經驗 獲得超9個贊

您可以使用 生成所有type_1s的列表,然后它們使用 with 對值進行相應的排序list_comprehensionsortdatetime.strptime


from datetime import datetime


# Generate a list with only the type_1s' created_at values

type1s = [val['created_at'] for val in vals if val['type']=="type_1"]


# Sort them based on the timestamps

type1s.sort(key=lambda date: datetime.strptime(date, "%Y-%m-%dT%H:%M:%SZ"))


# Print the lowest value

print(type1s[0])


#'2020-02-12T17:45:00Z'


查看完整回答
反對 回復 2022-09-20
?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

您可以使用以下函數獲取所需的輸出:


from datetime import datetime


def sol(l):

    sum_=0

    dict_={}


    for x in l:

        if x['type']=='type_1':

            sum_+=1

            dict_[x['id']]=datetime.strptime(x['created_at'], "%Y-%m-%dT%H:%M:%SZ")


    date =sorted(dict_.values())[0]

    for key,value in dict_.items():

        if value== date: id_=key

    return sum_,date,id_


sol(l)

此函數給出類型 ='type_1'的次數,分別給出相應的最小日期及其 ID。


希望這有幫助!


查看完整回答
反對 回復 2022-09-20
?
縹緲止盈

TA貢獻2041條經驗 獲得超4個贊

這是使用 和 的一種方法。filtermin


前任:


data = [{'id': 'id_123',

  'type': 'type_1',

  'created_at': '2020-02-12T17:45:00Z'},

 {'id': 'id_124',

  'type': 'type_2',

  'created_at': '2020-02-12T18:15:00Z'},

 {'id': 'id_125',

  'type': 'type_1',

  'created_at': '2020-02-13T19:43:00Z'},

 {'id': 'id_126',

  'type': 'type_3',

  'created_at': '2020-02-13T07:00:00Z'}]


onlytype_1 = list(filter(lambda x: x['type'] == 'type_1', data))

print(len(onlytype_1))

print(min(onlytype_1, key=lambda x: x['created_at']))

藝術


temp = {}

for i in data:

    temp.setdefault(i['type'], []).append(i)


print(len(temp['type_1']))

print(min(temp['type_1'], key=lambda x: x['created_at']))

輸出:


2

{'id': 'id_123', 'type': 'type_1', 'created_at': '2020-02-12T17:45:00Z'}


查看完整回答
反對 回復 2022-09-20
  • 5 回答
  • 0 關注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號