使用 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