如何在Python中的列表的每一行中反转元组?

时间:2017-04-30 02:24:28

标签: python list loops tuples

我有一个很长的文本文件,但这里是我列表中缩进的元组版本。

[[(2, 4, 4)], [(5, 3, 3)], [(6, 2, 9)],
 [(3, 5, 7)], [(4, 9, 10)], [(8, 10, 3)],
 [(2, 5, 6)], [(6, 7, 2)], [(8, 5, 4)]]

这是我的代码,使用reverse()

for i in range(len(image_2dlist)):
    image_2dlist.reverse()

print(image_2dlist)

打印

[[(8, 5, 4)], [(6, 7, 2)], [(2, 5, 6)],
 [(8, 10, 3)], [(4, 9, 10)], [(3, 5, 7)],
 [(6, 2, 9)], [(5, 3, 3)], [(2, 4, 4)]]

但我需要它看起来像这样

[[(6, 2, 9)], [(5, 3, 3)], [(2, 4, 4)],
 [(8, 10, 3)], [(4, 9, 10)], [(3, 5, 7)],
 [(8, 5, 4)], [(6, 7, 2)], [(2, 5, 6)]]

3 个答案:

答案 0 :(得分:2)

L = [[(2, 4, 4)], [(5, 3, 3)], [(6, 2, 9)],
    [(3, 5, 7)], [(4, 9, 10)], [(8, 10, 3)],
    [(2, 5, 6)], [(6, 7, 2)], [(8, 5, 4)]]

In [28]: list(itertools.chain.from_iterable([L[i:i+3][::-1] for i in range(0, len(L), 3)]))
Out[28]: 
[[(6, 2, 9)],
 [(5, 3, 3)],
 [(2, 4, 4)],
 [(8, 10, 3)],
 [(4, 9, 10)],
 [(3, 5, 7)],
 [(8, 5, 4)],
 [(6, 7, 2)],
 [(2, 5, 6)]]

答案 1 :(得分:0)

a=[[(2, 4, 4)], [(5, 3, 3)], [(6, 2, 9)],
 [(3, 5, 7)], [(4, 9, 10)], [(8, 10, 3)],
 [(2, 5, 6)], [(6, 7, 2)], [(8, 5, 4)]]  

'''Initialize the 2d list of dimention 3x3'''
 b = [[None for _ in range(3)] for _ in range(3)]
 k,j=0,0

'''convert a-1d list to b-2d list'''
for i in range(3):
        for j in range(3):
            b[i][j]=a[k]
            k=k+1

'''swap 0th and 2nd element'''   
for i in range(3):
        b[i][0],b[i][2]=b[i][2],b[i][0]

'''convert b-2d list to a-1d list'''
 for i in range(3):
        for j in range(3):
            a[k]=b[i][j]
            k=k+1
print a

输出:

 [[(6, 2, 9)], [(5, 3, 3)], [(2, 4, 4)], [(8, 10, 3)], [(4, 9, 10)], [(3, 5, 7)], [(8, 5, 4)], [(6, 7, 2)], [(2, 5, 6)]]    

答案 2 :(得分:0)

ll = [  [(2, 4, 4)], [(5, 3, 3)], [(6, 2, 9)],
         [(3, 5, 7)], [(4, 9, 10)], [(8, 10, 3)],
         [(2, 5, 6)], [(6, 7, 2)], [(8, 5, 4)]
     ]

<强>阶段1

start , end , step = 0 , 3, 3 
while end <= len(ll):
  print('Start:',start,'End :',end,'Step:',step)
  print(ll[start:end])
  start = start + step
  end = end + step

-

#Start: 0 End : 3 Step: 3
# [[(2, 4, 4)], [(5, 3, 3)], [(6, 2, 9)]]
#Start: 3 End : 6 Step: 3
#[[(3, 5, 7)], [(4, 9, 10)], [(8, 10, 3)]]
#Start: 6 End : 9 Step: 3
#[[(2, 5, 6)], [(6, 7, 2)], [(8, 5, 4)]]

<强> Stage2的

start , end , step = 0 , 3, 3 
while end <= len(ll):
  print(sorted(ll[start:end] ,reverse=True , key=lambda lt : lt[0][0]))
  start = start + step
  end = end + step

<强> -

# sorted(ll[start:end]) =>  [[(2, 4, 4)], [(5, 3, 3)], [(6, 2, 9)]]
# lt = [(2, 4, 4)]
# lt[0] = (2, 4, 4) 
# lt[0][0] = 2

<强> stage3的

start , end , step = 0 , 3, 3 
while end <= len(ll):
  out = sorted(ll[start:end] ,reverse=True , key=lambda lt : lt[0][0])
  for item in out:
    resultList.append(item)
  start = start + step
  end = end + step

打印结果

start , end , step = 0 , 3, 3 
while end <= len(resultList):
  print(resultList[start:end])
  start = start + step
  end = end + step

<强> RESULT

[[(6, 2, 9)], [(5, 3, 3)], [(2, 4, 4)], 
  [(8, 10, 3)], [(4, 9, 10)], [(3, 5, 7)], 
   [(8, 5, 4)], [(6, 7, 2)], [(2, 5, 6)]]