如何使用python中的word tokenize函数删除列表中的数字?我得到的是输出,但我需要的是没有数字的输出。

问题描述 投票:0回答:1

我不需要列表编号(即)0,1等。我需要打印没有编号的元素

    import pandas as pd
    from nltk.tokenize import word_tokenize
    import csv
    # define punctuation
    my_str=pd.read_csv("ef.csv")
    punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~...'''
    word_tokens = word_tokenize(str(my_str))
    #mystr=str(my_str)
    # remove punctuation from the string
    no_punct = [char for char in word_tokens if not char in punctuations]
    no_punct=[]
    for char in word_tokens:
      if char not in punctuations:
          #no_punct = no_punct + char
           no_punct.append(char)

如何使用python中的word tokenize函数删除列表中的数字?我得到的是输出,但我需要的是没有数字的输出。

python pandas csv nltk
1个回答
1
投票

好吧,可以用简单的Python来完成......。

sentence=['Raghavan', 'teaching', 'is', 'excellent', '0', 'Sankar', 'is', 'good', 'at', 'teaching', '1', 'Darwin', 'is', 'extraordinary', 'in', 'teaching']
for i in sentence:
    try:
        if str(int(float(i))).isnumeric():
            sentence.remove(i)
    except:
        pass
print(sentence)
# output - ['Raghavan', 'teaching', 'is', 'excellent', 'Sankar', 'is', 'good', 'at', 'teaching', 'Darwin', 'is', 'extraordinary', 'in', 'teaching']
© www.soinside.com 2019 - 2024. All rights reserved.