简体   繁体   English

如何从列表中只提取英文单词?

[英]How to extract only the english words from the list?

I tried to extract only the English words from the following list:我试图从以下列表中仅提取英文单词:

l = ['0', 'b', 'x14', 'x00', 'x1fP', 'xe0O', 'xd0', 'xea', 'i', 'x10', 'xa2', 'xd8', 'x08', 'x00', '00', 'x9d', 'x14', 'x00', 'x80', 'xcc', 'xbf', 'xb4', 'xdbLB', 'xb0', 'x7f', 'xe9', 'x9a', 'x87', 'xc6AZ', 'x005', 'x00', 'x00', 'x00', 'x00', 'x00yR', 'G', 'x10', 'x00', 'xdc', 'x05', 'xde', 'x05', 'xe2', 'x05', 'xe8', 'x05', 'xdb', 'x05', 'xea', 'x05', 'x00', 'x00', 'x00', 't', 'x00', 'x04', 'x00', 'xef', 'xbeyRnDyR', 'G', 'x00', 'x00', 'x00', 'xe5E', 'x00', 'x00', 'x00', 'x00', 'xfb', 'x05', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'xe2', 'x0e', 'x00', 'xdc', 'x05', 'xde', 'x05', 'xe2', 'x05', 'xe8', 'x05', 'xdb', 'x05', 'xea', 'x05', 'x00', 'x00', 'x1c', 'x00', 'x80', 'x001', 'x00', 'x00', 'x00', 'x00', 'x00yR', 'G', 'x10', 'x00VBS', '', '', 'x00', 'x00', 't', 'x00', 'x04', 'x00', 'xef', 'xbeyR', 'GyR', 'G', 'x00', 'x00', 'x00', 'x9e', 'xa5', 'x00', 'x00', 'x00', 'x00K', 'x02', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'xe2', 'x0e', 'x00V', 'x00B', 'x00S', 'x00', 'x00R', 'x00a', 'x00n', 'x00s', 'x00o', 'x00m', 'x00w', 'x00a', 'x00r', 'x00e', 'x00', 'x00', 'x00', 'x00d', 'x00o', 'x00n', 'x00e', 'x00', 'x00', 'x00', 'x00', 'x80', 'x001', 'x00', 'x00', 'x00', 'x00', 'x00yRmG', 'x10', 'x00VBS', '', '', 'x00', 'x00', 't', 'x00', 'x04', 'x00', 'xef', 'xbeyR', 'GyRmG', 'x00', 'x00', 'x00', 'xb6', 'xba', 'x00', 'x00', 'x00', 'x00', 'xa4', 'x01', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x98w', 'x00V', 'x00B', 'x00S', 'x00', 'x00R', 'x00a', 'x00n', 'x00s', 'x00o', 'x00m', 'x00w', 'x00a', 'x00r', 'x00e', 'x00', 'x00', 'x00', 'x00d', 'x00o', 'x00n', 'x00e', 'x00', 'x00', 'x00', 'x00', 'xa4', 'x002', 'x00c', 'xf1', 'x02', 'x00oRjX', 'Test', 'For', 'SO', 'PDF', 'pdf', 'x00t', 'x00', 't', 'x00', 'x04', 'x00', 'xef', 'xbeyR', 'GyR', 'G', 'x00', 'x00', 'x00', 'xcf', 'xbc', 'x00', 'x00', 'x00', 'x00z', 'x04', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'xd23', 'x98', 'x00D', 'x00e', 'x00f', 'x00e', 'x00n', 'x00s', 'x00e', 'x00', 'x00R', 'x00u', 'x00l', 'x00e', 'x00', 'x00', 'x00', 'x00V', 'x00B', 'x00S', 'x00', 'x00R', 'x00a', 'x00n', 'x00s', 'x00o', 'x00m', 'x00w', 'x00a', 'x00r', 'x00e', 'x00', 'x00p', 'x00d', 'x00f', 'x00', 'x00', 'x000', 'x00', 'x00', 'x00', '3']

From this list, the words I need are ["Test", "For", "SO", "PDF"] .从这个列表中,我需要的词是["Test", "For", "SO", "PDF"]

I tried the following:我尝试了以下方法:

for i in range(num_of_values):
    values = EnumValue(key, i)
    res = re.findall(r'\w+', str(values))
    print(res)

Did anyone manage to extract the words?有没有人设法提取单词?

You can get it working to some extent with pyenchant library that allows checking if a word is a valid word in a given language.您可以使用pyenchant在一定程度上使其工作,该库允许检查单词是否是给定语言中的有效单词。 Before checking linguistic validity, you need to check if在检查语言有效性之前,您需要检查是否

  • the word is not empty and is more than one char long该词不为空且长度超过一个字符
  • the word consists of letters only.这个词只由字母组成。

So, in Python, you need to install the pyenchant library first ( pip install pyenchant in the terminal/console), and then因此,在 Python 中,您需要先安装pyenchant库( pip install pyenchant在终端/控制台中安装 pyenchant),然后

import enchant
l = ['0', 'b', 'x14', 'x00', 'x1fP', 'xe0O', 'xd0', 'xea', 'i', 'x10', 'xa2', 'xd8', 'x08', 'x00', '00', 'x9d', 'x14', 'x00', 'x80', 'xcc', 'xbf', 'xb4', 'xdbLB', 'xb0', 'x7f', 'xe9', 'x9a', 'x87', 'xc6AZ', 'x005', 'x00', 'x00', 'x00', 'x00', 'x00yR', 'G', 'x10', 'x00', 'xdc', 'x05', 'xde', 'x05', 'xe2', 'x05', 'xe8', 'x05', 'xdb', 'x05', 'xea', 'x05', 'x00', 'x00', 'x00', 't', 'x00', 'x04', 'x00', 'xef', 'xbeyRnDyR', 'G', 'x00', 'x00', 'x00', 'xe5E', 'x00', 'x00', 'x00', 'x00', 'xfb', 'x05', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'xe2', 'x0e', 'x00', 'xdc', 'x05', 'xde', 'x05', 'xe2', 'x05', 'xe8', 'x05', 'xdb', 'x05', 'xea', 'x05', 'x00', 'x00', 'x1c', 'x00', 'x80', 'x001', 'x00', 'x00', 'x00', 'x00', 'x00yR', 'G', 'x10', 'x00VBS', '', '', 'x00', 'x00', 't', 'x00', 'x04', 'x00', 'xef', 'xbeyR', 'GyR', 'G', 'x00', 'x00', 'x00', 'x9e', 'xa5', 'x00', 'x00', 'x00', 'x00K', 'x02', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'xe2', 'x0e', 'x00V', 'x00B', 'x00S', 'x00', 'x00R', 'x00a', 'x00n', 'x00s', 'x00o', 'x00m', 'x00w', 'x00a', 'x00r', 'x00e', 'x00', 'x00', 'x00', 'x00d', 'x00o', 'x00n', 'x00e', 'x00', 'x00', 'x00', 'x00', 'x80', 'x001', 'x00', 'x00', 'x00', 'x00', 'x00yRmG', 'x10', 'x00VBS', '', '', 'x00', 'x00', 't', 'x00', 'x04', 'x00', 'xef', 'xbeyR', 'GyRmG', 'x00', 'x00', 'x00', 'xb6', 'xba', 'x00', 'x00', 'x00', 'x00', 'xa4', 'x01', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x98w', 'x00V', 'x00B', 'x00S', 'x00', 'x00R', 'x00a', 'x00n', 'x00s', 'x00o', 'x00m', 'x00w', 'x00a', 'x00r', 'x00e', 'x00', 'x00', 'x00', 'x00d', 'x00o', 'x00n', 'x00e', 'x00', 'x00', 'x00', 'x00', 'xa4', 'x002', 'x00c', 'xf1', 'x02', 'x00oRjX', 'Test', 'For', 'SO', 'PDF', 'pdf', 'x00t', 'x00', 't', 'x00', 'x04', 'x00', 'xef', 'xbeyR', 'GyR', 'G', 'x00', 'x00', 'x00', 'xcf', 'xbc', 'x00', 'x00', 'x00', 'x00z', 'x04', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'xd23', 'x98', 'x00D', 'x00e', 'x00f', 'x00e', 'x00n', 'x00s', 'x00e', 'x00', 'x00R', 'x00u', 'x00l', 'x00e', 'x00', 'x00', 'x00', 'x00V', 'x00B', 'x00S', 'x00', 'x00R', 'x00a', 'x00n', 'x00s', 'x00o', 'x00m', 'x00w', 'x00a', 'x00r', 'x00e', 'x00', 'x00p', 'x00d', 'x00f', 'x00', 'x00', 'x000', 'x00', 'x00', 'x00', '3']
d = enchant.Dict("en_US") 
output = [el for el in l if len(el)>1 and el.isalpha() and d.check(el)]
>>> output
# => ['Test', 'For', 'SO', 'PDF']

if you know what you want to search, just search.如果您知道要搜索的内容,只需搜索即可。

# 'a' is your data list 

search=["Test", "For", "SO", "PDF", "pdf"]

for s in search:
    print(a.index(s))

output index of the words you search in your list:您在列表中搜索的单词的 output 索引:

253
254
255
256
257

But if you want to search all the english words you need a dictonary, then search every english word.但是如果你想搜索所有的英文单词,你需要一个字典,然后搜索每个英文单词。

# This find all the occurrences for every words in the list 'a' 

#search is your list with words to search 

for s in search:
    indeces = [i for i, x in enumerate(a) if x == s]
    print(s,indeces)
    

output: output:

Test [253]
For [254]
SO [255]
PDF [256]
pdf [257]

seems you know in advance what to extract from the list so I give you some ideas:看来您事先知道要从列表中提取什么,所以我给您一些想法:

# Example 1: Search using a loop and create a new list
list_2 = []
for element in list_1:
    if 'pdf' in element:
            list_2.append(element)
            print('the element is in the list and was added to list_2 ')
    
# Example 2: If you know in advance what to extract use list comprehension
list_0 = ['Test', 'For', 'SO', 'PDF', 'pdf']
for elements in list_0: 
    if elements in list_1:
        print(elements)

# Checking if something is inside the list
for elements in list_1:
    if 'Test' in elements:
        print('The element is in the list')
        
# Return the element number in the list
index = list_1.index('Test')
print(index)

Let me know if this work for you.让我知道这是否适合您。

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

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