python TypeError: 'NoneType' object is not iterable

时间:2023-03-09 16:43:16
python TypeError: 'NoneType' object is not iterable

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是不可迭代的。所以就报了这么一个错误。