Python 如何从字符串列表中删除所有数字?

Python 如何从字符串列表中删除所有数字?,python,list,Python,List,我有一个包含一串数字和字母的列表: list = ["North East, 22.5, 13.6", "South West, 19.8, 34.2"] 我如何删除这些数字并获得: words = ["North East", "South West"] 我试过: numbers = "123456789" for item in list: if item in numbers:

我有一个包含一串数字和字母的列表:

list = ["North East, 22.5, 13.6", "South West, 19.8, 34.2"]
我如何删除这些数字并获得:

words = ["North East", "South West"]
我试过:

numbers = "123456789"
for item in list:
    if item in numbers:
        list.remove(item)

首先,您不应该使用
list
作为变量名,因为这是内置类型的名称,并且使用它作为变量名,您正在覆盖内置
list
类型

要回答此问题,可以使用
str.split()
方法:

>字符串列表=[“NE,22.5,13.6”,“SW,19.8,34.2”]
>>>
>>>过滤的_字符串=[]
>>>对于\u字符串列表\u中的字符串:
...     对于string.split(“,”)中的元素:
...         if元素.isalpha():
...             筛选的\u字符串。追加(元素)
...
>>>过滤字符串
['NE','SW']
>>>

只是对@blorgon的答案稍加修改

directions = ["North East, 22.5, 13.6", "South West, 19.8, 34.2"]

parsed_directions = []
for d in directions:
  words = ""
  for c in d:
    if c.isalpha() or c is " ":
      words = words + c
  parsed_directions.append(words.strip()) 
    
print(parsed_directions) # output: ['North East', 'South West']

也许有人能找到一种不用嵌套循环的方法来实现这一点…

我可以根据整个数据集的结构提出两种方法

案例1:所有数据都是
“名称,其他我们不关心的内容”
。只要抓住第一个逗号的内容

data = ["North East, 22.5, 13.6", "South West, 19.8, 34.2"]
desired = ["North East", "South West"]
output = [e.split(',')[0] for e in data]
output
['North East', 'South West']
案例2:逗号和数字与单词混合。使用正则表达式提取整个单词,然后将它们粘贴到一起

more_data = ["West 77, 3.14 Pies, Spam 123 Cake", "North East, 22.5, 13.6", "South West, 19.8, 34.2"]
more_desired = ["West Pies Spam Cake", "North East", "South West"]
[' '.join(re.findall(r'[a-zA-Z]+', e)) for e in more_data]
['West Pies Spam Cake', 'North East', 'South West']

案例1-如果顺序相同-
['string'、'number'、'number']
则使用-

list = ["North East, 22.5, 13.6", "South West, 19.8, 34.2"]
list = [item.split(',')[0] for item in list]
情况2-如果顺序不同,且元素之间用“,”分隔

A部分-

在这里,我只是从字符串中去掉了额外的空格,然后删除了标点符号,并测试了特定项是否可以转换为数字数据。如果可以转换,则从列表中删除该特定项

import string
from itertools import chain
my_list = ["North East, 22.5, 13.6", "South West, 19.8, 34.2"]
my_list = [[s for s in item.split(',') if not s.strip().translate(str.maketrans('', '', string.punctuation)).isnumeric()] for item in my_list]
my_list = list(chain(*my_list))
比如说-

my_list = [" 22.5, 13.6, North East", "South West, 19.8, 34.2, test"] #notice one extra element at 2nd index and change of order in 1st. 

**Output**
['North East', 'South West', ' test'] # notice the change here 
第2部分-

如果你想保留原来的表格-

import string
from itertools import chain
my_list = ["22.5, 13.6,North East", "South West, 19.8, 34.2, test"]
my_list = [','.join(s for s in item.split(',') if not s.strip().translate(str.maketrans('', '', string.punctuation)).isnumeric()) for item in my_list] # notice ‘,’.join here
输出-

['North East', 'South West, test'] # this will strip off all the numbers from the list

提示:为什么所需的输出不是
[“NE,,,,,,,,,,,,,,,,,,,,]
?您的输入总是遵循相同的模式吗?这里只有一个例子,没有任何解释。您的字符串中似乎包含以逗号分隔的信息以及地理坐标。如果它们总是采用
“指南针方向、十进制数、十进制数”
的形式,那么解决方案将与任何混合了数字、字母、逗号、空格和句号的字符串非常不同。这是一个很好的答案,因为它涵盖了OP描述的情况(似乎是一致的模式)和更现实的情况(数据不一致,正则表达式更合适)。这是你第二次提请大家注意你回答的一个离题问题,对此我表示感谢。不过,以后投票结束这样的问题可能会更容易些?:-PFair够了。我在meta上读到过,赏金法更可取。实际上我想我误解了。我知道你是在专门谈论离题问题,应该立即结束,而不是回答。