0

一直在挠头。

我有一个嵌套列表,格式如下:

( (value1, value2, value3), (value1, value2, value3), ... (value1, value2, value3))

现在我试图把它归结为:

( (value1, value2), (value1, value2), ... (value1, value2))

简而言之,尝试删除每个嵌套列表的第三个元素。

这在 Python 中是否可以直接实现?

4

7 回答 7

6

您可以通过列表理解简单地做到这一点:

>>> x = [(1, 2, 3), (1, 2, 3), (1, 2, 3)]
>>> x = [(a, b) for (a, b, c) in x]
[(1, 2), (1, 2), (1, 2)]
于 2013-04-25T15:48:36.997 回答
6

使用列表推导:

outerlist = [sublist[:-1] for sublist in outerlist]

这使用切片来删除每个包含的子列表的最后一个元素。

上面创建了一个list(),而不是一个tuple();以下将再次创建一个元组:

outertuple = tuple(sublist[:-1] for sublist in outertuple)

与解包和重新打包元组相比,对子列表进行切片所需的操作码更少,从而更快地为您提供结果。

使用元组的演示:

>>> outertuple = ((1, 2, 3), (4, 5, 6), (7, 8, 9))
>>> tuple(sublist[:-1] for sublist in outertuple)
((1, 2), (4, 5), (7, 8))

使用列表演示:

>>> outerlist = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> [sublist[:-1] for sublist in outerlist]
[[1, 2], [4, 5], [7, 8]]

最后但并非最不重要的一点是,列表推导的内部循环的字节码反汇编表明切片使用的操作码少于解包和重新打包:

>>> import dis
>>> def foo(x): return [sublist[:-1] for sublist in x]
... 
>>> def bar(x): return [(a, b) for (a, b, c) in x]
... 
>>> dis.dis(foo)
  1           0 BUILD_LIST               0
              3 LOAD_FAST                0 (x)
              6 GET_ITER            
        >>    7 FOR_ITER                16 (to 26)
             10 STORE_FAST               1 (sublist)
             13 LOAD_FAST                1 (sublist)
             16 LOAD_CONST               1 (-1)
             19 SLICE+2             
             20 LIST_APPEND              2
             23 JUMP_ABSOLUTE            7
        >>   26 RETURN_VALUE        
>>> dis.dis(bar)
  1           0 BUILD_LIST               0
              3 LOAD_FAST                0 (x)
              6 GET_ITER            
        >>    7 FOR_ITER                27 (to 37)
             10 UNPACK_SEQUENCE          3
             13 STORE_FAST               1 (a)
             16 STORE_FAST               2 (b)
             19 STORE_FAST               3 (c)
             22 LOAD_FAST                1 (a)
             25 LOAD_FAST                2 (b)
             28 BUILD_TUPLE              2
             31 LIST_APPEND              2
             34 JUMP_ABSOLUTE            7
        >>   37 RETURN_VALUE        

11 对 14 操作码。那是一个额外的UNPACK_SEQUENCE和两个额外STORE_FAST的操作码,以及一个BUILD_TUPLE操作码与SLICE+2.

这些差异使得拆包和打包要慢一些:

>>> import timeit
>>> test = [(42, 69, 180) for _ in xrange(1000)]
>>> timeit.timeit('f(x)', 'from __main__ import test as x, foo as f', number=10000)
1.1722910404205322
>>> timeit.timeit('f(x)', 'from __main__ import test as x, bar as f', number=10000)
1.6375460624694824
于 2013-04-25T15:48:45.047 回答
2
>>> a = ((1,2,3), (4,5,6), (7,8,9))
>>> a = tuple([(x1, x2) for (x1, x2, x3) in a])
>>> a
((1, 2), (4, 5), (7, 8))
于 2013-04-25T15:50:32.133 回答
1
nested_list= [[1,2,3],[4,5,6]]

for regular_list in nested_list:
    del regular_list[2]
于 2013-04-25T15:49:31.203 回答
1

试试这个:

[a[ :-1] for a in b]

或这个:

[a[:n]+a[n+1:] for a in b]
于 2013-04-25T15:54:03.860 回答
0

使用地图:

l = [[0,1,2], [1,2,3]]
print map(lambda l : l[:-1], l)
# Outputs [[0, 1], [1, 2]]

鉴于所有子列表的长度为 3。

如果你想要这个元组:

l = ((0,1,2), (1,2,3))
print tuple(map(lambda l : l[:-1], l))
# Outputs ((0, 1), (1, 2))
于 2013-04-25T15:53:03.823 回答
0
x = [(1, 2, 3), (1, 2, 3), (1, 2, 3)]
>>> [i[:2] for i in x ]

[(1, 2), (1, 2), (1, 2)]

于 2013-04-26T08:38:18.290 回答