使用 Python 在字典中抓取并查找按顺序排列的单词
要解决此问题,我们需要请求模块。
要安装请求模块,需要在命令行执行此命令。
pip install requests
抓取
- 导入 requests 模块。
- 然后我们需要从 URL 获取数据。
- 使用 UTF-8 解码文本。
- 然后将字符串转换为单词列表。
按顺序查找
- 使用循环遍历单词列表。
- 然后比较每个单词相邻字符的 ASCII 值。
- 如果比较为 true,则打印按顺序排列的单词,否则存储无序单词。
示例代码
import requests
def Words_find():
my_url = ""#put thisurl of .txt files in any website
my_fetchData = requests.get(my_url)
my_wordList = my_fetchData.content
my_wordList = my_wordList.decode("utf-8").split()
return my_wordList
def wordordered():
collection = Words_find()
collection = collection[16:]
my_word = ''
for my_word in collection:
result = 'ordered'
i = 0
l = len(my_word) - 1
if (len(my_word) < 3):
continue
while i < l:
if (ord(my_word[i]) > ord(my_word[i+1])):
result = 'not ordered'
break
else:
i += 1
if (result == 'ordered'):
print(my_word,': ',result)
if __name__ == '__main__':
wordordered()
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP