第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何使用 python while 循環(huán)打印地址為伯明翰的所有名字?

如何使用 python while 循環(huán)打印地址為伯明翰的所有名字?

慕的地6264312 2023-07-11 13:44:41
dictionary_list = [    {"firstname": "Steven", "lastname": "Walker", "address": "Birmingham"},    {"firstname": "James", "lastname": "Kay", "address": "Birmingham"},    {"firstname": "Alice", "lastname": "Gibson", "address": "Birmingham"},    {"firstname": "John", "lastname": "Doe", "address": "Birmingham"},    {"firstname": "evan", "lastname": "spencer", "address": "Derby"},    {"firstname": "Dean", "lastname": "Davis", "address": "Leeds"},    {"firstname": "Jade", "lastname": "wilson", "address": "Birmingham"},    {"firstname": "Emma", "lastname": "James", "address": "Birmingham"},    {"firstname": "Alex", "lastname": "Windsor", "address": "Birmingham"},    {"firstname": "Rebekah", "lastname": "Daphney", "address": "London"},]如何使用python while循環(huán)打印地址為伯明翰的所有名字?我不知道從哪里開始。
查看完整描述

3 回答

?
心有法竹

TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊

只是因?yàn)槟笫褂脀hile循環(huán)實(shí)現(xiàn):


i = 0

while i < len(dictionary_list):

    d = dictionary_list[i]

    if d['address'] == 'Birmingham':

        print(d['firstname'])

    i += 1


查看完整回答
反對(duì) 回復(fù) 2023-07-11
?
慕妹3242003

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超6個(gè)贊

您應(yīng)該從 pandas 庫(kù)開始,它是在 python 上開始操作數(shù)據(jù)最直觀的:


    import pandas as pd

    df=pd.DataFrame(dictionary_list)

    df2 = df[df['address']=="Birmingham"]

    print(df2["firstname"])


查看完整回答
反對(duì) 回復(fù) 2023-07-11
?
阿波羅的戰(zhàn)車

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊

您首先不應(yīng)該使用 awhile loop因?yàn)槟那闆r沒(méi)有意義,您最好應(yīng)該使用這樣for loop的:


for d in dictionary_list:

    if d['address'] == 'Birmingham':

        print([d['firstname'])

但你可以按照alani 的建議,簡(jiǎn)單地在一行中完成此操作:


print([d['firstname'] for d in dictionary_list if d['address'] == 'Birmingham'])


查看完整回答
反對(duì) 回復(fù) 2023-07-11
  • 3 回答
  • 0 關(guān)注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)