Python2.7-从列表列表返回列表,其中子列表项包含特定字符
我有一长串的名单,像这样的“人”:Python2.7-从列表列表返回列表,其中子列表项包含特定字符,python,python-2.7,Python,Python 2.7,我有一长串的名单,像这样的“人”: persons = [['Boris', 'Jones', '988991'], ['Charlie', 'Smith', '999'], ['Sue', 'White', '9']] result = [['Charlie', 'Smith', '999'], ['Sue', 'White', '9']] 我想从“人员”中提取其编号仅包含允许字符的人员: permitted = ['9'] 结果应该是这样的: persons = [['Boris',
persons = [['Boris', 'Jones', '988991'], ['Charlie', 'Smith', '999'], ['Sue', 'White', '9']]
result = [['Charlie', 'Smith', '999'], ['Sue', 'White', '9']]
我想从“人员”中提取其编号仅包含允许字符的人员:
permitted = ['9']
结果应该是这样的:
persons = [['Boris', 'Jones', '988991'], ['Charlie', 'Smith', '999'], ['Sue', 'White', '9']]
result = [['Charlie', 'Smith', '999'], ['Sue', 'White', '9']]
这是我试过的。显然,它不起作用:(
“数字”字段可以包含1到6个字符。您可以通过以下方式执行此操作:
result = [i for i in persons if all(j in permitted for j in i[-1])]
例子
希望有帮助
这里,all(…)
仅当字符串中的所有字符都在允许的范围内时才计算为True