cuốn sách gpt4 ai đã làm

python - 字典列表中第一次出现的二进制搜索

In lại Tác giả: Vũ trụ không gian 更新时间:2023-11-04 00:13:03 27 4
mua khóa gpt4 Nike

Vì vậy, tôi đang xử lý tập dữ liệu lớn, n>1000000. Dữ liệu chứa thông tin đặt hàng về các mặt hàng. Thứ tự ở định dạng JSON chứa một tệp có tên is_buy_order giá trị bool. Tôi muốn chia danh sách đơn hàng thành hai danh sách riêng biệt tùy thuộc vào giá trị bool có ĐÚNG VẬY 还是 SAI.

Tôi đã nghĩ ra một thuật toán có sai sót nhưng nhanh hơn việc lặp lại.

Thuật toán chia tập dữ liệu làm đôi bằng cách chọn một trục rồi kiểm tra một trong hai bên để xác định bên nào gần điểm chuyển tiếp hơn (SAI -> ĐÚNG VẬY) .Nó tiếp tục giảm một nửa cho đến khi các giá trị ở cả hai phía của trục quay khác nhau hoặc trục == 1 Cho biết không có thay đổi.

bắt đầu = thời gian.time()
order_file = open("resources/khu vực/"+x.replace(" ", "")[1:-1]+".json", 'r')
đơn hàng = order_file.readlines()
order_file.close()


item_buy, item_sell = [], []

Pivot_found = Sai
print(len(đơn hàng))

nếu len(đơn đặt hàng) > 1:
trong khi không phải là Pivot_found:
temp_orders = đơn hàng
trục = len(temp_orders)//2

nếu trục == 1:
break

if json.loads(orders[pivot].replace("\n", ""))["is_buy_order"]:
đơn hàng = đơn hàng[:pivot]
buy_sell_index -= trục
khác:
đơn hàng = đơn hàng[trục:]

if json.loads(temp_orders[pivot].replace("\n", ""))["is_buy_order"] != json.loads(temp_orders[pivot-1].replace("\n", "")) ["is_buy_order"]:
Pivot_found = Đúng


item_buy, item_sell = temp_orders[:pivot], temp_orders[pivot:]
buy_sell_index = order.index(item_sell[0])
print(x, time.time()-start, buy_sell_index)

Đây là nội dung của một tập dữ liệu bị giảm nghiêm trọng:

{"duration":90,"is_buy_order":false,"issued:"2018-06-09T01:52:42Z","location_id":1027547438558,"min_volume":1,"order_i d":5180297455,"giá":16000.0,"phạm vi":"40","system_id":30001811,"type_id":28362,"volume_remain":892,"volume_total":892}
{"duration":90,"is_buy_order":false,"issued:"2018-06-09T01:53:11Z","location_id":1027547438558,"min_volume":1,"order_i d":5180297673,"giá":100000.0,"phạm vi":"40","system_id":30001811,"type_id":28366,"volume_remain":907,"volume_total":907}
{"duration":90,"is_buy_order":false,"issued:"2018-06-09T01:53:42Z","location_id":1027547438558,"min_volume":1,"order_i d":5180297903,"giá":100000.0,"phạm vi":"40","system_id":30001811,"type_id":21815,"volume_remain":906,"volume_total":906}
{"duration":90,"is_buy_order":true,"issued:"2018-08-03T01:50:59Z","location_id":1027954902335,"min_volume":1,"order_id ":5191398100,"giá":4.0,"phạm vi":"5","system_id":30001780,"type_id":34,"volume_remain":10000000,"volume_total":10000000}
{"duration":90,"is_buy_order":true,"issued:"2018-08-05T07:30:18Z","location_id":1028168079013,"min_volume":1,"order_id ":5221892906,"giá":2250000.0,"phạm vi":"4","system_id":30001748,"type_id":25615,"volume_remain":100,"volume_total":100}
{"duration":90,"is_buy_order":true,"issued:"2018-07-21T05:23:37Z","location_id":1022958758740,"min_volume":1,"order_i d":5211030090,"giá":185.0,"phạm vi":5","system_id":30001786,"type_id":204,"volume_remain":40000,"volume_total":40000}
{"duration":90,"is_buy_order":true,"issued:"2018-08-05T07:31:23Z","location_id":1028168079013,"min_volume":1,"order_i d":5221893610,"giá":6000.0,"phạm vi":"4","system_id":30001748,"type_id":25616,"volume_remain":1000,"volume_total":1000}
{"duration":90,"is_buy_order":true,"issued:"2018-08-05T07:27:50Z","location_id":1028168079013,"min_volume":1,"order_id ":5221891669,"giá":1150000.0,"phạm vi":"4","system_id":30001748,"type_id":25619,"volume_remain":200,"volume_total":200}
{"duration":90,"is_buy_order":true,"issued:"2018-07-22T17:46:06Z","location_id":1022958758740,"min_volume":1,"order_id ":5212328909,"giá":12.0,"phạm vi":"5","system_id":30001786,"type_id":211,"volume_remain":1000000,"volume_total":1000000}
{"duration":30,"is_buy_order":true,"issued:"2018-07-19T22:18:58Z","location_id":1028168079013,"min_volume":1,"order_ id":5210158811,"giá":2000000.0,"phạm vi":"5","system_id":30001748,"type_id":16278,"volume_remain":3,"volume_total":3}
{"duration":90,"is_buy_order":true,"issued:"2018-08-05T07:32:18Z","location_id":1028168079013,"min_volume":1,"order_id ":5221894118,"giá":65000.0,"phạm vi":"4","system_id":30001748,"type_id":25606,"volume_remain":1000,"volume_total":1000}

Có thể thực hiện điều này nếu tập dữ liệu yêu cầu định dạng mới.

câu trả lời hay nhất

chia đôi Các mô-đun có cách để thực hiện việc này. Bản thân nó không hỗ trợ chức năng quan trọng nhưng bạn có thể thêm trình bao bọc xung quanh danh sách của mình:

từ bisect nhập bisect

danh sách của tôi = [
{"is_buy_order": Sai},
{"is_buy_order": Sai},
{"is_buy_order": Sai},
{"is_buy_order": Sai},
{"is_buy_order": Đúng},
{"is_buy_order": Đúng},
{"is_buy_order": Đúng},
{"is_buy_order": Đúng},
{"is_buy_order": Đúng},
{"is_buy_order": Đúng}
]


lớp KeyFuncWrapper(đối tượng):
def __init__(self, iterable, key):
self.it = có thể lặp lại
self.key = key

def __len__(self):
trả về len(self.it)

def __getitem__(self, i):
trả về self.key(self.it[i])


# in 4
in(chia đôi(
KeyFuncWrapper(my_list, lambda x: x["is_buy_order"]),
Sai, # giá trị cho hình chia đôi cần tìm
))

Điều này hoạt động vì bisect sẽ nhìn vào KeyFuncWrapper Phần tử thứ i của , bản thân nó xem xét ứng dụng của hàm chính trên phần tử thứ i của danh sách.

Về python - Tìm kiếm nhị phân cho lần xuất hiện đầu tiên trong danh sách từ điển, chúng tôi đã tìm thấy một câu hỏi tương tự trên Stack Overflow: https://stackoverflow.com/questions/51821908/

27 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress