如果我有一套:
{'NYC', 'Ames', 'LA', 'Houston', '500', '1000', '3000',
'SanFrancisco', '300', '200', 'Detroit', 'Austin'}
Run Code Online (Sandbox Code Playgroud)
如何从集合中删除所有数字字符串?
要明确我想要这个:
{'NYC', 'Ames', 'LA', 'Houston', 'SanFrancisco', 'Detroit', 'Austin'}
Run Code Online (Sandbox Code Playgroud)
基于Meccano的答案,您还可以使用set comprehension来使用.isdigitstr方法缩短代码:
old_set = {'NYC', 'Ames', 'LA', 'Houston', '500', '1000', '3000',
'SanFrancisco', '300', '200', 'Detroit', 'Austin'}
new_set = {elem for elem in old_set if not elem.isdigit()}
print(new_set)
# output:
# {'NYC', 'Ames', 'LA', 'Houston', 'SanFrancisco', 'Detroit', 'Austin'}
Run Code Online (Sandbox Code Playgroud)