【发布时间】:2020-12-17 12:53:52
【问题描述】:
我有一个数组将子数组包含在dirty_pages 中的[page_name、url 和id]。该数组包含重复的子数组。
我需要将dirty_pages 中的每个subarray 解析为clean_pages,这样:
-
没有重复(重复子数组)
-
子数组中的
1st index即url 必须是唯一的! 例如,这个 url 应该计为 一个 (url/#review仍然是同一个 url):file:///home/joe/Desktop/my-projects/FashionShop/product.html#review和
file:///home/joe/Desktop/my-projects/FashionShop/product.html
我当前的尝试返回 clean_pages 和 6 个子数组(重复!),而正确答案应该是 4
# clean pages
clean_pages = []
# dirty pages
dirty_pages = [
['Fashion Shop | Free Bootstrap Themes by 365Bootstrap.com', 'file:///home/joe/Desktop/my-projects/FashionShop/index.html', '1608093980462.042'],
['Put a Sock in It Heel Boot | Nasty Gal', 'file:///home/joe/Desktop/my-projects/FashionShop/nastygal-product.html', '1608093980462.042'],
['Fashion Shop | Free Bootstrap Themes by 365Bootstrap.com', 'file:///home/joe/Desktop/my-projects/FashionShop/index.html', '1608093980462.042'],
['Fashion Shop | Free Bootstrap Themes by 365Bootstrap.com', 'file:///home/joe/Desktop/my-projects/FashionShop/index.html', '1608093980462.042'],
['Fashion Shop | Free Bootstrap Themes by 365Bootstrap.com', 'file:///home/joe/Desktop/my-projects/FashionShop/product.html', '1608093980462.042'],
['Fashion Shop | Free Bootstrap Themes by 365Bootstrap.com', 'file:///home/joe/Desktop/my-projects/FashionShop/product.html#review', '1608093980462.042'],
['Fashion Shop | Free Bootstrap Themes by 365Bootstrap.com', 'file:///home/joe/Desktop/my-projects/FashionShop/product.html#review', '1608093980462.042'],
['Fashion Shop | Free Bootstrap Themes by 365Bootstrap.com', 'file:///home/joe/Desktop/my-projects/FashionShop/index.html', '1608093980462.042'],
['Put a Sock in It Heel Boot | Nasty Gal', 'file:///home/joe/Desktop/my-projects/FashionShop/nastygal-product.html', '1608093980462.042'],
['ICONIC EXCLUSIVE - Game Over Drop Crotch Track Pants - Kids by Rock Your Kid Online | THE ICONIC | Australia', 'file:///home/joe/Desktop/my-projects/FashionShop/iconic-product.html', '1608093980462.042'],
['Put a Sock in It Heel Boot | Nasty Gal', 'file:///home/joe/Desktop/my-projects/FashionShop/nastygal-product.html', '1608093980462.042'],
['Fashion Shop | Free Bootstrap Themes by 365Bootstrap.com', 'file:///home/shahyan/Desktop/my-projects/FashionShop/index.html/#review', '1608093980462.042'],
['Fashion Shop | Free Bootstrap Themes by 365Bootstrap.com', 'file:///home/shahyan/Desktop/my-projects/FashionShop/index.html/?123', '1608093980462.042'],
['Fashion Shop | Free Bootstrap Themes by 365Bootstrap.com', 'file:///home/shahyan/Desktop/my-projects/FashionShop/index.html/', '1608093980462.042'],
]
# clean data - get unique pages for each session
for j in range(len(dirty_pages)):
page_name = dirty_pages[j][0]
page_url = dirty_pages[j][1]
page_sessionId = dirty_pages[j][2]
not_seen = False
if len(clean_pages) == 0:
clean_pages.append([page_name, page_url, page_sessionId])
else:
for i in range(len(clean_pages)):
next_page_name = clean_pages[i][0]
next_page_url = clean_pages[i][1]
next_page_sessionId = clean_pages[i][2]
if page_url != next_page_url and page_name != next_page_name \
and page_sessionId == next_page_sessionId:
not_seen = True
else:
not_seen = False
if not_seen is True:
clean_pages.append([page_name, page_url, page_sessionId])
print("$$$ clean...", len(clean_pages))
# correct answer should be 4 - as anyting after url e.g. #review is still duplicate!
更新示例 - 如果示例不清楚,请致歉(就像在 url 之后的 # 这些应该被视为一个 url)
'file:///home/joe/Desktop/my-projects/FashionShop/index.html/'
'file:///home/joe/Desktop/my-projects/FashionShop/index.html/?123'
'file:///home/joe/Desktop/my-projects/FashionShop/index.html'
【问题讨论】:
-
建议:1)编写一个函数
clean_url,它接受一个url并输出该url的标准形式(例如删除#review)2)使用pythonset来存储你的url ,因此会自动处理重复项。
标签: python python-3.x list sorting nested-lists