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

时间:2017-05-13 01:06:54

标签: python string set

如果我有一套:

{'NYC', 'Ames', 'LA', 'Houston', '500', '1000', '3000',
 'SanFrancisco', '300', '200', 'Detroit', 'Austin'}

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

要明确我想要这个:

{'NYC', 'Ames', 'LA', 'Houston', 'SanFrancisco', 'Detroit', 'Austin'}

4 个答案:

答案 0 :(得分:5)

基于Meccano的答案,您还可以使用集合理解来使用<?php ob_start(); session_start(); require 'connect.inc.php'; if (!isset($_SESSION['id'])) { header("Location: http://website.com/login.php"); exit(); } ?> str方法缩短代码:

.isdigit

答案 1 :(得分:2)

您可以使用string.isdigit()来检查字符串是否为数字:

new_set = set()
for elem in old_set:
    if not elem.isdigit():
        new_set.add(elem)

答案 2 :(得分:1)

这样做:

l = {'NYC', 'Ames', 'LA', 'Houston', '500', '1000', '3000',
 'SanFrancisco', '300', '200', 'Detroit', 'Austin34'}
l = re.sub("^\d+\s|\s\d+\s|\s\d+$", " ", l)

这也删除了单词

中的数字
  

{&#39; NYC&#39;,&#39; Ames&#39;,&#39; LA&#39;,&#39; Houston&#39;,         &#39; SanFrancisco&#39;,&#39; Detroit&#39;,&#39; Austin&#39;}

答案 3 :(得分:1)

过滤掉数字并创建一个新集:

set_new = set(filter(lambda e: not e.isdigit(), set_old))
相关问题