将长元组分割成更小的元组

问题描述 投票:0回答:5

我有一个长元组,比如

(2, 2, 10, 10, 344, 344, 45, 43, 2, 2, 10, 10, 12, 8, 2, 10)

我正在尝试将它分成一个元组的元组,例如

((2, 2, 10, 10), (344, 344, 45, 43), (2, 2, 10, 10), (12, 8, 2, 10))

我是 python 新手,不太擅长元组 o(2, 2, 10, 10, 344, 344, 45, 43, 2, 2, 10, 10, 12, 8, 2, 10)r 列表。我的朋友说我应该把它分开,但我就是无法得到它 -_-

我需要将元组拆分为具有 4 个元素的元组,稍后我将使用矩形通过 PIL 绘制到图像。

python list split tuples
5个回答
10
投票

有一个特定的成语:

def grouper(n, iterable):
    args = [iter(iterable)] * n
    return zip(*args)

t = (2, 2, 10, 10, 344, 344, 45, 43, 2, 2, 10, 10, 12, 8, 2, 10)
print grouper(4, t)

但是解释起来有点复杂。 itertools 收据中列出了一个稍微更通用的版本。

您也可以自己对元组进行切片

parts = (t[0:4], t[4:8], t[8:12], t[12:16])

# or as a function
def grouper2(n, lst):
    return [t[i:i+n] for i in range(0, len(t), n)]

这可能就是你朋友的意思。


4
投票
>>> atup
(2, 2, 10, 10, 344, 344, 45, 43, 2, 2, 10, 10, 12, 8, 2, 10)
>>> [ atup[n:n+4] for n,i in enumerate(atup) if n%4==0 ]
[(2, 2, 10, 10), (344, 344, 45, 43), (2, 2, 10, 10), (12, 8, 2, 10)]

0
投票

另一个可能的答案(使用生成器):

 oldTuple = (2, 2, 10, 10, 344, 344, 45, 43, 2, 2, 10, 10, 12, 8, 2, 10)
 newTuple = tuple(oldTuple[x:x+4] for x in range(0, len(oldTuple), 4))

0
投票

我不久前写过一个像 gist 这样的函数,可以在 http://gist.github.com/616853

找到
def split(input_list,num_fractions=None,subset_length=None):
   '''                                                                                                                                
   Given a list/tuple split original list based on either one of two parameters given but NOT both,                                   
   Returns generator                                                                                                                  
   num_fractions : Number of subsets original list has to be divided into, of same size to the extent possible.                       
                   In case equilength subsets can't be generated, all but the last subset                                             
                   will have the same number of elements.                                                                             
   subset_length : Split on every subset_length elements till the list is exhausted.                                                  

   '''
   if not input_list:
       yield input_list #For some reason I can't just return from here : return not allowed in generator expression                   
   elif not bool(num_fractions) ^ bool(subset_length): #Will check for both the invalid cases, '0' and 'None'.. Oh Python :)          
       raise Exception("Only one of the params : num_fractions,subset_length to be provided")
   else:
       if num_fractions: #calcluate subset_length in this case                                                                        
           subset_length = max(len(input_list)/num_fractions,1)

       for start in xrange(0,len(input_list),subset_length):
           yield input_list[start:start+subset_length]



>> list(list_split.split((2, 2, 10, 10, 344, 344, 45, 43, 2, 2, 10, 10, 12, 8, 2, 10),subset_length=4))
 [(2, 2, 10, 10), (344, 344, 45, 43), (2, 2, 10, 10), (12, 8, 2, 10)]

代码比上面给出的解决方案长,但涵盖了所有可能的序列分割条件。


0
投票

使用

dollop

from dollop import serve

t = (2, 2, 10, 10, 344, 344, 45, 43, 2, 2, 10, 10, 12, 8, 2, 10)

dollops = serve(t, 4)

print(tuple(dollops))
© www.soinside.com 2019 - 2024. All rights reserved.