这是我的元组架构:
(name, age, weight)
UserList = (('steve', 17, 178), ('Mike', 19, 178),('Pull', 24, 200),('Adam', 15, 154))
我想检查年龄是否小于18我想用(,,)取代该用户的元组
因此最终结果将是
(('', , ), ('Mike', 19, 178),('Pull', 24, 200),('', , ))
我试过
UserList = list(UserList)
for i,e in enumerate(UserList):
if e[1] < 18:
temp=list(UserList[i])
for f, tmp in enumerate(temp):
del temp[:]
但它不起作用,任何想法或建议都将受到高度赞赏。 谢谢!
答案 0 :(得分:3)
In [13]: UserList = tuple((n, a, w) if a >= 18 else ('', None, None) for (n, a, w) in UserList)
In [14]: UserList
Out[14]: (('', None, None), ('Mike', 19, 178), ('Pull', 24, 200), ('', None, None))