浏览文章
文章信息
Python纯手工实现笛卡尔乘积(作用:解析爬虫规格产品依次点击规格产品)
964
示例代码:拿去修改一下,就可以使用了
import copy attributes = [ [ 'red', 'blue', 'pink' ], [ 's', 'm', 'l', 'xl' ] , [ 'a', 'b', 'c' ], [ '1', '2', '3' ] ] def get_times(): attributes_products = 0 for attribute in attributes: attribute_length = len(attribute) if attributes_products == 0: attributes_products = attribute_length else: attributes_products *= attribute_length return attributes_products+len(attributes) click_attributes = [] old_attributes = [] def attribute_click(attributes: list, times: int): global click_attributes global old_attributes if times == 1: old_attributes = copy.deepcopy(attributes) click_attributes = copy.deepcopy(attributes) current_click = '' length = len(attributes) # 每次最多点击次数 if click_attributes: attributes = click_attributes print(click_attributes) for attribute_index, attribute in enumerate(attributes): # 前一个属性规格 pre_attribute_index = attribute_index - 1 if pre_attribute_index < 0: pre_attribute_index = 0 # 点击完一个属性则删除上一个属性 if not attribute: del attributes[pre_attribute_index][0] if not attributes[pre_attribute_index]: del attributes[pre_attribute_index-1][0] if not attributes[pre_attribute_index] and attributes[pre_attribute_index - 1]: for i in range(pre_attribute_index, length): click_attributes[i] = copy.deepcopy(old_attributes[i]) # 如果点击完了一个属性,则判断上一个属性是否还没点完,没有就回复所有下属属性 if not attribute and attributes[pre_attribute_index]: print('如果点击完了一个属性,则判断上一个属性是否还没点完,没有就回复所有下属属性') print(attributes[pre_attribute_index]) for i in range(attribute_index, length): click_attributes[i] = copy.deepcopy(old_attributes[i]) attribute = click_attributes[attribute_index] print(attributes[pre_attribute_index]) for attr_index, attr in enumerate(attribute): if attribute_index == length - 1: current_click = current_click + attr else: current_click = current_click + attr + '-' # 最后一个规格的时候删除掉已经点击的 if attribute_index == length - 1: del attribute[0] print('第 {times} 点击完成'.format(times=times)) print(current_click) print(attributes) current_click = '' click_attributes[attribute_index] = attribute break if __name__ == '__main__': for click_time in range(1, get_times()): attribute_click(attributes, click_time)