如何从集合中删除数字字符串?

Cod*_*117 0 python string set

如果我有一套:

{'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)

abc*_*ccd 5

基于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)