在元组列表中使用 bisect? [英] Using bisect in a list of tuples?

查看:56
本文介绍了在元组列表中使用 bisect?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何在元组列表中使用 bisect例如

I'm trying to figure out how to use bisect in a list of tuples for example

[(3, 1), (2, 2), (5, 6)]

如何根据每个元组中的 [1] 将该列表平分?

How can I bisect this list according to the [1] in each tuple?

list_dict [(69, 8), (70, 8), ((65, 67), 6)]
tup1,tup2 (69, 8) (70, 8)
list_dict [((65, 67), 6)]
fst, snd ((65, 67),) (6,)

我正在插入以平分

idx = bisect.bisect(fst, tup1[1]+tup2[1])

这给了我 不可排序的类型:int() <元组()

推荐答案

您可以将值分离到单独的列表中.

You can separate out the values into separate lists.

from bisect import bisect

data = [(3, 1), (2, 2), (5, 6)]
fst, snd = zip(*data)
idx = bisect(fst, 2)

但是请注意,要使 bisect 起作用,您的数据确实应该排序...

Note however, that for bisect to work, your data really should be ordered...

这篇关于在元组列表中使用 bisect?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆