日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python如何判断列表是否为空_Python中如何检查字符串/列表是否为空

發布時間:2024/7/19 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python如何判断列表是否为空_Python中如何检查字符串/列表是否为空 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文最后更新于2018年5月5日,已超過 1 年沒有更新,如果文章內容失效,還請反饋給我,謝謝!

=Start=

緣由:

整理、記錄、備忘

正文:

參考解答:

從dict中取值時,一定要使用.get()方法,而不是數組的方式,避免KeyError異常;

在使用前判空 or 加上try..catch語句塊預防異常;

In [50]: resp = {"total": 0, "rows": []}

...: alist = resp.get('rows')

...:

In [51]: if not alist:

...: print 'hi' #當alist為空列表時會執行print

...:

hi

In [52]: if alist:

...: print 'hi'

...:

In [53]: resp.get('rows')

Out[53]: []

In [54]: resp.get('row')

In [55]: print resp.get('row')

None

In [56]: print resp.get('row', '')

In [57]:

&

In [60]: if len(resp['rows']) == 0:

...: print 'hi'

...:

hi

In [61]: if not resp.get('rows'):

...: print 'hi'

...:

hi

In [62]: if not resp['rows']:

...: print 'hi'

...:

hi

In [63]: if not resp['row']:

...: print 'hi'

...:

---------------------------------------------------------------------------

KeyError Traceback (most recent call last)

in ()

----> 1 if not resp['row']:

2 print 'hi'

KeyError: 'row'

In [64]: if not resp.get('row'):

...: print 'hi'

...:

hi

In [65]:

參考鏈接:

=END=

總結

以上是生活随笔為你收集整理的python如何判断列表是否为空_Python中如何检查字符串/列表是否为空的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。