1. 程式人生 > >python TypeError: 'NoneType' object is not iterable

python TypeError: 'NoneType' object is not iterable

strip() ble err pla rip lambda readline not read

list(set(map(lambda tp_id : tp_id if not (‘#‘ in tp_id) and len(tp_id.strip().replace(‘\n‘, ‘‘)) > 0
else None, open(‘tp_list.txt‘).readlines()).sort()))

為了練習map,想著把一個循環寫成一行代碼。

結構跑程序時出了一個問題:TypeError: ‘NoneType‘ object is not iterable。

找了半天錯誤(寫成一行代碼在這時候真是作死)

發現是sort的問題, list的成員方法sort()將一個list排序(會改變list),但返回None。

None是不可叠代的。所以就報了這麽一個錯誤。

python TypeError: 'NoneType' object is not iterable