简体   繁体   English

在python中删除同时包含字母和数字的字符串

[英]remove string that contain both letters and numbers in python

I have a String 我有一个弦

c=("Snap-on Power M1302A5 Imperial,IMPRL 0.062IN")

and I need to convert above string to 我需要将上面的字符串转换为

c=("Snap-on Power Imperial,IMPRL")

ie i need to remove string that has both letters and numbers, 即我需要删除同时包含字母和数字的字符串,

How can I do this in python? 如何在python中做到这一点?

I tried with 我尝试过

c=c.apply(word_tokenize)
c = c.apply(lambda x: [item for item in x if item.isalpha()])

but got output 但是得到了输出

c=("Snap-on Power MA Imperial,IMPRL IN")

I'm not sure exactly what you want here, but it seems you want to remove words that have a digit in them. 我不确定您在这里想要的是什么,但似乎您想删除其中包含数字的单词。 In that case, you can use any() here: 在这种情况下,您可以在此处使用any()

>>> c = "Snap-on Power M1302A5 Imperial,IMPRL 0.062IN"
>>> ' '.join(w for w in c.split() if not any(x.isdigit() for x in w))
Snap-on Power Imperial,IMPRL

Also adding a regex based solution: 还添加了基于regex的解决方案:

c = "Snap-on Power M1302A5 Imperial,IMPRL 0.062IN"
only_a = []
for word in c.split():
    #print(word)
    if not re.search('\d',word):
        #print(word)
        only_a.append(word)
' '.join(only_a)

Output: 'Snap-on Power Imperial,IMPRL' 输出: 'Snap-on Power Imperial,IMPRL'

To select words without digit 选择没有数字的单词

c = ' '.join([item for item in c.split() if not any(filter(str.isdigit, item))])
# ['Snap-on', 'Power', 'Imperial,IMPRL']

This answer does the same thing as other answers 此答案与其他答案具有相同的作用

Step 1: Split the string into different words 步骤1:将字串分割成不同的字词

Step 2: Check if each word contains numbers, if it does skip the word 步骤2:检查每个字词是否包含数字,是否确实跳过了该字词

Step 3: Generate a string from the words without numbers 步骤3:根据不含数字的单词生成字符串

line = "Snap-on Power M1302A5 Imperial,IMPRL 0.062IN"

split_line = line.split(" ")

final_split_string = []

for word in split_line:
    skip_word = False
    for letter in word:
        if letter.isdigit():
            #check if the current word contains a number
            skip_word = True

    if not skip_word:
        #skip a word if it contains number
        final_split_string.append(word)

final_string = " ".join(final_split_string)

You can try something like this: 您可以尝试如下操作:

c="Snap-on Power M1302A5 Imperial,IMPRL 0.062IN"
import re
pattern=r'\d'

final=[]
for i in c.split():
    if isinstance(i.split(','),list):
        for m in i.split(','):
            if re.search(pattern,m):
                pass
            else:
                final.append(m)

print(" ".join(final))

output: 输出:

Snap-on Power Imperial IMPRL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM