On 10/28/10 9:25 AM, Yann wrote:
On Oct 27, 10:29 pm, Minh Nguyen<[email protected]> wrote:
Hi Marusia,
On Thu, Oct 28, 2010 at 7:02 AM, m.rebolledo
<[email protected]> wrote:
Hello,
how to do the union of several lists (more than two)?
Lists are multisets, so I assume you mean to combine several lists
into one, while retaining duplicate elements. You could do so using
the list concatenation operator "+":
sage: L1 = [2, 3, 5, 7, 11]
sage: L2 = ["a", "b", "c", 13]
sage: L3 = [1/2, 1/3, 1/4]
sage: L1 + L2 + L3
[2, 3, 5, 7, 11, 'a', 'b', 'c', 13, 1/2, 1/3, 1/4]
Hi,
just as a sidenote, this might make people write this:
L = sum( a list of list, [] )
which is correct but quite inefficient.
But you could try using sage's balanced_sum, which is much more
efficient (but still not as efficient as using += like your last
example, at least in this case):
sage: LL=[range(5*n,5*n+3) for n in range(100)]
sage: LL
[[0, 1, 2], [5, 6, 7], [10, 11, 12], [15, 16, 17], [20, 21, 22], [25,
26, 27], [30, 31, 32], [35, 36, 37], [40, 41, 42], [45, 46, 47], [50,
51, 52], [55, 56, 57], [60, 61, 62], [65, 66, 67], [70, 71, 72], [75,
76, 77], [80, 81, 82], [85, 86, 87], [90, 91, 92], [95, 96, 97], [100,
101, 102], [105, 106, 107], [110, 111, 112], [115, 116, 117], [120, 121,
122], [125, 126, 127], [130, 131, 132], [135, 136, 137], [140, 141,
142], [145, 146, 147], [150, 151, 152], [155, 156, 157], [160, 161,
162], [165, 166, 167], [170, 171, 172], [175, 176, 177], [180, 181,
182], [185, 186, 187], [190, 191, 192], [195, 196, 197], [200, 201,
202], [205, 206, 207], [210, 211, 212], [215, 216, 217], [220, 221,
222], [225, 226, 227], [230, 231, 232], [235, 236, 237], [240, 241,
242], [245, 246, 247], [250, 251, 252], [255, 256, 257], [260, 261,
262], [265, 266, 267], [270, 271, 272], [275, 276, 277], [280, 281,
282], [285, 286, 287], [290, 291, 292], [295, 296, 297], [300, 301,
302], [305, 306, 307], [310, 311, 312], [315, 316, 317], [320, 321,
322], [325, 326, 327], [330, 331, 332], [335, 336, 337], [340, 341,
342], [345, 346, 347], [350, 351, 352], [355, 356, 357], [360, 361,
362], [365, 366, 367], [370, 371, 372], [375, 376, 377], [380, 381,
382], [385, 386, 387], [390, 391, 392], [395, 396, 397], [400, 401,
402], [405, 406, 407], [410, 411, 412], [415, 416, 417], [420, 421,
422], [425, 426, 427], [430, 431, 432], [435, 436, 437], [440, 441,
442], [445, 446, 447], [450, 451, 452], [455, 456, 457], [460, 461,
462], [465, 466, 467], [470, 471, 472], [475, 476, 477], [480, 481,
482], [485, 486, 487], [490, 491, 492], [495, 496, 497]]
sage: timeit('flatten(LL)')
625 loops, best of 3: 507 盜 per loop
sage: timeit('sum(LL,[])')
625 loops, best of 3: 104 盜 per loop
sage: timeit('balanced_sum(LL,[])')
625 loops, best of 3: 32.4 盜 per loop
sage: timeit('L=[]\nfor i in LL: L.extend(i)')
625 loops, best of 3: 21.6 盜 per loop
sage: timeit('L=[]\nfor i in LL: L+=i')
625 loops, best of 3: 13.5 盜 per loop
-Jason
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org