我找不到元组在我上一个代码中的存在位置。
bus_stop_ID
的每个元素都是元组。
回顾一下,您有一个变长元组列表,并希望找到整个结构中最常见的元素。
这听起来像是一个完美的使用
collections.Counter
的案例:
>>> from collections import Counter
>>> bus_stop_ID = [('id1', 'id2', 'id3'), ('id1',), ('id1', 'id3', 'id5')]
>>> [(stop_id, count)] = sum(map(Counter, bus_stop_ID), Counter()).most_common(1)
>>> stop_id
'id1'
>>> count
3
这里:
map(Counter, bus_stop_ID)
将每条路线的车站ID进行单独计数;
sum(..., Counter())
在所有路线中汇总计数;
.most_common(1)
返回计数最多的车站ID(作为一个包含两个元素的二元组列表,其中第一个元素是车站ID,第二个元素是它的计数)。
另一种方法是先使用sum()
将所有路线连接起来,然后再进行计数:
>>> [(stop_id, count)] = Counter(sum(bus_stop_ID, ())).most_common(1)
这种方法较为简短,但效率较低,因为它需要将所有路由连接成一个(潜在的巨大)元组。
一种略显花哨的方法是通过使用 itertools.chain()
来规避这种低效性:
>>> Counter(itertools.chain(*bus_stop_ID)).most_common(1)
[('id1', 3)]
这只是简单地迭代第一路线中的所有公交站点ID,然后是第二个路线,以此类推,在此过程中进行计数。
出于好奇,我在一个任意选择的包含1万个元组的列表上测试了这三种方法,结果非常有趣:
In [11]: %timeit sum(map(Counter, bus_stop_ID), Counter()).most_common(1)
1 loop, best of 3: 235 ms per loop
In [12]: %timeit Counter(sum(bus_stop_ID, ())).most_common(1)
1 loop, best of 3: 2.64 s per loop
In [13]: %timeit Counter(itertools.chain(*bus_stop_ID)).most_common(1)
10 loops, best of 3: 17.3 ms per loop
collections.Counter
和most_common
来代替你的代码,因为你的代码看起来非常复杂。但我们没有你的数据。一个小样本比我们没有的csv文件要好得多。 - Jean-François Fabreset
函数不接受key
参数(或任何关键字参数),而且括号不匹配。 - Karl Knechtel