简体   繁体   English

如何将这个元组元组转换为元素数?

[英]How can I convert this tuple of tuples into a count of its elements?

I have this tuple of tuples: 我有这个tuple组元组:

TupleOfTuples = (('Venue1', 'Name1'), ('Venue1', 'Name2'), 
                 ('Venue2', 'Name3'), ('Venue3', 'Name4'), 
                 ('Venue3', 'Name5'), ('Venue3', 'Name6'))

I want to convert it to get a result like this: 我想将其转换为得到如下结果:

Output = (('Venue1', 2), ('Venue2', 1), ('Venue3', 3))

In this case, Output contains ('Venue1', 2) , for example, where 2 is the number of times 'Venue1' occurred in TupleOfTuples . 在这种情况下, Output包含('Venue1', 2) ,例如,其中2'Venue1'出现的TupleOfTuples

I tried using len() to count the number of occurrences, but it does not work given that TupleOfTuples is not a single tuple but a tuple of tuples. 我尝试使用len()来计算出现次数,但是由于TupleOfTuples不是单个元组而是元组的元组,所以它不起作用。

How can this be done in Python2.7? 如何在Python2.7中完成?

Use collections.Counter() to count how many occurrences you have: 使用collections.Counter()来计算您有多少次出现:

from collections import Counter

Output = Counter(t[0] for t in TupleOfTuples).items()

A Counter() is a dictionary where keys are mapped to counts; Counter()是一个字典,其中键被映射到计数; by passing in a generator expression it will do the counting for you. 通过传入生成器表达式,它将为您进行计数。 Because it is a dictionary subclass, dict.items() can then be used to produce a list of (key, count) tuples. 因为它是一个字典子类, dict.items()可以使用dict.items()来生成(key, count)元组列表。

This does produce a list ; 这确实产生了一个清单 ; simply call tuple() on that if you insist on having a tuple here. 如果你坚持在这里使用元组,只需调用tuple()即可。

Demo: 演示:

>>> from collections import Counter
>>> TupleOfTuples = ( ('Venue1', 'Name1'), ('Venue1', 'Name2'), ('Venue2', 'Name3'), ('Venue3', 'Name4'), ('Venue3', 'Name5'), ('Venue3', 'Name6') )
>>> Counter(t[0] for t in TupleOfTuples).items()
[('Venue1', 2), ('Venue3', 3), ('Venue2', 1)]

You can accomplish this quickly and easily using zip(*TupleOfTuples)[n] to get a sequence of all elements to be counted (where n is the index of the element in each TupleOfTuples tuple to count; in this case, 0 ), then iterating through the result to get a count for each unique element. 您可以使用zip(*TupleOfTuples)[n]快速轻松地完成此操作,以获取要计数的所有元素的序列(其中n是要计数的每个TupleOfTuples元组中元素的索引;在本例中为0 ),然后迭代结果以获得每个唯一元素的计数。

Here's what it looks like: 这是它的样子:

TupleOfElements = zip(*TupleOfTuples)[0]
Output = tuple((e, TupleOfElements.count(e)) for e in set(TupleOfElements))

I'll explain what's going on: 我会解释发生了什么:

zip(*TupleOfTuples)[0] takes your input sequence and transposes it . zip(*TupleOfTuples)[0]接收输入序列并对其进行转置 We want the zero'th element from each TupleOfTuples element, so we take [0] from the result. 我们希望从每一个第零元素TupleOfTuples元素,因此,我们采取[0]从结果。 We assign that sequence to TupleOfElements . 我们将该序列分配给TupleOfElements (If you wanted to count the Name* elements instead, for instance, you would use zip(*TupleOfTuples)[1] .) (例如,如果你想计算Name *元素,你可以使用zip(*TupleOfTuples)[1] 。)

tuple((e, TupleOfElements.count(e)) for e in set(TupleOfElements)) creates the Output you want by iterating through TupleOfElements and returning an element-count pair for each unique element: TupleOfElements contains all TupleOfTuples elements in the correct quantity , so we can use TupleOfElements.count(uniqueElement) will tell us how many occurrences of uniqueElement there are. tuple((e, TupleOfElements.count(e)) for e in set(TupleOfElements))通过迭代TupleOfElements并为每个唯一元素返回元素计数对来创建所需的OutputTupleOfElements包含正确数量的所有TupleOfTuples元素 ,所以我们可以使用TupleOfElements.count(uniqueElement)告诉我们有多少次出现的uniqueElement We don't need or want to recheck any specific element more than once, though, so we iterate through set(TupleOfElements) , which will contain exactly one of each element present. 但是,我们不需要或想要不止一次地重新检查任何特定元素,因此我们遍历set(TupleOfElements) ,它将包含每个元素中的一个。 We assign the result to Output , and we're done! 我们将结果分配给Output ,我们就完成了!

  • Note: This will return Output as a tuple . 注意:这将返回Output作为tuple If you want it as a list , replace the tuple(..) in the second line with [..] , keeping the contents the same. 如果你想把它作为一个list ,用[..]替换第二行中的tuple(..) ,保持内容相同。

  • On performance: This code seems to run considerably faster than Martijn's very good solution using collections.Counter -- around 3.5x faster for the example TupleOfTuples given, and about 1.25x faster in a much larger but much simpler 88,888-element test I made up to satisfy my own curiosity-- I should imagine because it replaces the dictionary-creation step with a tuple and iterator. 关于性能:这个代码似乎比使用collections.Counter Martijn非常好的解决方案运行得快得多 - 对于给出的示例TupleOfTuples快了大约3.5倍,并且在一个更大但更简单的88,888元素测试中我快了大约1.25倍为了满足自己的好奇心 - 我应该想象,因为它用元组和迭代器取代了字典创建步骤。 It may not be quite as elegant, but I'm a bit proud of it all the same. 它可能不是优雅,但我有点骄傲的这一切一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM