如何仅从列表中提取英语单词?

2024-10-03 23:24:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从以下列表中仅提取英语单词:

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']

从这个列表中,我需要的单词是["Test", "For", "SO", "PDF"]

我尝试了以下方法:

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

有人设法把这些词提取出来了吗


Tags: xe2x10x00x04x00ax00sx00rx05
3条回答

如果你知道你想搜索什么,就搜索吧

# 'a' is your data list 

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

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

在列表中搜索的单词的输出索引:

253
254
255
256
257

但是如果你想搜索所有需要听写的英语单词,那么就搜索每个英语单词

# 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)
    

输出:

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

您可以在某种程度上使用^{} library来检查单词在给定语言中是否有效。在检查语言有效性之前,您需要检查

  • 单词不是空的,并且长度超过一个字符
  • 这个词只由字母组成

因此,在Python中,需要首先安装pyenchant库(pip install 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']

似乎你事先知道从列表中提取什么,所以我给你一些想法:

# 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)

让我知道这是否适合你

相关问题 更多 >