简体   繁体   English

从元组列表创建元组的最有效方法

[英]Most efficient way of creating a tuple from list of tuples

I am currently using a for loop with enumerate to extract from a list of tuples below:我目前正在使用带有 enumerate 的 for 循环从下面的元组列表中提取:

[(0, 'handle', 'VARCHAR(50)', 1, None, 1), (1, 'Firstname', 'TEXT', 1, None, 0), (2, 'Surname', 'TEXT', 1, None, 0), (3, 'Callname', 'TEXT', 1, None, 0), (4, 'Gender', 'INTEGER', 1, None, 0)]

What i want is to end up with the following tuple ('handle', 'Firstname', 'Surname', 'Callname', 'Gender')我想要的是最终得到以下元组('handle', 'Firstname', 'Surname', 'Callname', 'Gender')

What would be the most efficient way of accomplishing this without enumerating through them and creating a new tuple or is this the only way?在不枚举它们并创建新元组的情况下,最有效的方法是什么,或者这是唯一的方法?

Create a new tuple by enumerating through them:通过枚举创建一个新元组:

tuple(t[1] for t in inputlist)

This uses a generator expression to pass each second element from the tuples in inputlist to the tuple() constructor .这使用生成器表达式inputlist组中的每个第二个元素inputlisttuple()构造函数

If you just need a sequence and a list would do, then use a list comprehension:如果你只需要一个序列而一个列表就可以了,那么使用列表理解:

[t[1] for t in inputlist]

Lists fit arbitrary-length, ordered, homogenous data sets (such as you have here) better than do tuples, see What's the difference between lists and tuples?列表比元组更适合任意长度的、有序的、同质的数据集(例如您在此处拥有的),请参阅列表和元组之间的区别是什么?

If raw speed is required and readability can be de-emphasised, use map() and operator.itemgetter() to move iteration and extraction to optimised C code:如果需要原始速度并且可以不强调可读性,请使用map()operator.itemgetter()将迭代和提取移动到优化的 C 代码:

from operator import itemgetter

labels_tup = tuple(map(itemgetter(1), inputlist))
labels_list = list(map(itemgetter(1), inputlist))

However, I'd avoid doing this unless extracting a bunch of strings out of a list of tuples is on a critical path and / or repeated a lot.但是,除非从元组列表中提取一堆字符串在关键路径上和/或重复很多次,否则我会避免这样做。 Readability counts!可读性很重要!

without enumerating through them and creating a new tuple不枚举它们并创建一个新的元组

You can't avoid this.你无法避免这一点。 You a) want one element from each tuple in a sequence, and b) need a tuple object as output, an immutable type.您 a) 需要序列中每个元组的一个元素,并且 b) 需要一个元组对象作为输出,一种不可变类型。 While you could write 5 separate statements indexing into inputlist to access each value, doing so would not be efficient, creates needlessly repeated code, and would break the moment your input doesn't have exactly 5 elements.虽然您可以编写 5 个单独的语句索引到inputlist以访问每个值,但这样做效率不高,会创建不必要的重复代码,并且会在您的输入不完全包含 5 个元素时中断。

Demo:演示:

>>> inputlist = [(0, 'handle', 'VARCHAR(50)', 1, None, 1), (1, 'Firstname', 'TEXT', 1, None, 0), (2, 'Surname', 'TEXT', 1, None, 0), (3, 'Callname', 'TEXT', 1, None, 0), (4, 'Gender', 'INTEGER', 1, None, 0)]
>>> tuple(t[1] for t in inputlist)
('handle', 'Firstname', 'Surname', 'Callname', 'Gender')
>>> [t[1] for t in inputlist]
['handle', 'Firstname', 'Surname', 'Callname', 'Gender']

You are looking for generator expression .您正在寻找生成器表达式

print(tuple(i[1] for i in inputlist))

Or或者

t = tuple(i[1] for i in inputlist)
print(t)

Outputs:输出:

('handle', 'Firstname', 'Surname', 'Callname', 'Gender')

A possible solution with for loop (Not recommended): for循环的可能解决方案(不推荐):

li = []
for i in inputlist:
    li.append(i[1])
print(tuple(li))

What would be the most efficient way of accomplishing this without enumerating through them and creating a new tuple or is this the only way?在不枚举它们并创建新元组的情况下,最有效的方法是什么,或者这是唯一的方法?

I am not sure why you want to avoid creating tuple but you don't need enumerate.我不确定您为什么要避免创建元组,但您不需要枚举。 May be the example given below can help:可能是下面给出的例子可以帮助:

def getElement(ndx):
    return inputlist[ndx][1]

# Get Second Element
print(getElement(2))
>>> from operator import itemgetter
>>> data = [(0, 'handle', 'VARCHAR(50)', 1, None, 1), (1, 'Firstname', 'TEXT', 1, None, 0), (2, 'Surname', 'TEXT', 1, None, 0), (3, 'Callname', 'TEXT', 1, None, 0), (4, 'Gender', 'INTEGER', 1, None, 0)]
>>> tuple(map(itemgetter(1), data))
('handle', 'Firstname', 'Surname', 'Callname', 'Gender')

This seems to be the fastest in raw speed (only slightly however - since it keeps everything in C as much as possible), and I also do like the look of this as well.这似乎是原始速度中最快的(不过只是略微 - 因为它尽可能地将所有内容保留在C中),而且我也很喜欢它的外观。 Of course you are still looping through the elements however.当然,您仍然在遍历元素。

Timings:时间:

$ python3 -m timeit -s "data = [(0, 'handle', 'VARCHAR(50)', 1, None, 1), (1, 'Firstname', 'TEXT', 1, None, 0), (2, 'Surname', 'TEXT', 1, None, 0), (3, 'Callname', 'TEXT', 1, None, 0), (4, 'Gender', 'INTEGER', 1, None, 0)]; from operator import itemgetter;" "tuple(map(itemgetter(1), data))"
500000 loops, best of 5: 477 nsec per loop
$ python3 -m timeit -s "data = [(0, 'handle', 'VARCHAR(50)', 1, None, 1), (1, 'Firstname', 'TEXT', 1, None, 0), (2, 'Surname', 'TEXT', 1, None, 0), (3, 'Callname', 'TEXT', 1, None, 0), (4, 'Gender', 'INTEGER', 1, None, 0)]; from operator import itemgetter;" "tuple(t[1] for t in data)"
500000 loops, best of 5: 566 nsec per loop
$ python3 -m timeit -s "data = [(0, 'handle', 'VARCHAR(50)', 1, None, 1), (1, 'Firstname', 'TEXT', 1, None, 0), (2, 'Surname', 'TEXT', 1, None, 0), (3, 'Callname', 'TEXT', 1, None, 0), (4, 'Gender', 'INTEGER', 1, None, 0)]*1000; from operator import itemgetter;" "tuple(map(itemgetter(1), data))"
2000 loops, best of 5: 146 usec per loop
$ python3 -m timeit -s "data = [(0, 'handle', 'VARCHAR(50)', 1, None, 1), (1, 'Firstname', 'TEXT', 1, None, 0), (2, 'Surname', 'TEXT', 1, None, 0), (3, 'Callname', 'TEXT', 1, None, 0), (4, 'Gender', 'INTEGER', 1, None, 0)]*1000; from operator import itemgetter;" "tuple(t[1] for t in data)"
1000 loops, best of 5: 212 usec per loop

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

相关问题 从元组列表中选择特定元组的大多数Python方法 - Most Pythonic Way to Select Particular Tuple from List of Tuples 从元组列表中查找空间顺序的最有效方法? - Most efficient way to find spatial order from a list of tuples? 有没有一种使用范围创建元组列表的有效方法? - Is there an efficient way of creating a list of tuples using a range? 对本身位于元组内的元组(可迭代的可迭代)求和的最有效方法是什么? - What is the most efficient way to sum across tuples that are themselves within a tuple (iterable of an iterable)? 如何从具有最频繁元组的“元组对列表”中创建“新的元组对列表”? - How to make 'a new list of tuple pairs' from 'a list of tuple pairs' with the most frequent tuples? 使用for / list理解从任意数量的其他元组创建元组 - Using for / list comprehension for creating a tuple from any amount of other tuples 从groupby返回列表的最有效方法 - Most efficient way to return list from a groupby 在Pandas中将元组分配给段的最有效方法 - Most efficient way in Pandas to assign tuples to segments 从字节创建固定大小元组的 numpy 数组的有效方法 - Efficient way of creating numpy array of fixed size tuples from bytes 优雅的方法从元组列表中提取元组,具有最小的元素值 - Elegant way to extract a tuple from list of tuples with minimum value of element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM