For the second question the "remove" is better for me : list.remove(x) because I don't know the index of x in the list. (I don't know even if the list contains x) Thank you again!
On 27 oct, 22:29, 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] > > Via the list method extend(): > > sage: L = [] > sage: L.extend(L1) > sage: L.extend(L2) > sage: L.extend(L3) > sage: L > [2, 3, 5, 7, 11, 'a', 'b', 'c', 13, 1/2, 1/3, 1/4] > > Or use the Sage built-in function flatten(): > > sage: flatten([L1, L2, L3]) > [2, 3, 5, 7, 11, 'a', 'b', 'c', 13, 1/2, 1/3, 1/4] > > If you really want to remove duplicate elements in the final combined > list, use Set(): > > sage: L1 = [1, 2] > sage: L2 = [2, 3, 4] > sage: L3 = ["a", "b"] > sage: Set(L1 + L2 + L3) > {'a', 1, 2, 3, 4, 'b'} > sage: list(Set(L1 + L2 + L3)) > ['a', 1, 2, 3, 4, 'b'] > > > and how to > > remove some elements of the lists? > > Use the operator del: > > sage: L > [2, 3, 5, 7, 11, 'a', 'b', 'c', 13, 1/2, 1/3, 1/4] > sage: L[0] > 2 > sage: del L[0]; L > [3, 5, 7, 11, 'a', 'b', 'c', 13, 1/2, 1/3, 1/4] > sage: L[-1] > 1/4 > sage: del L[-1]; L > [3, 5, 7, 11, 'a', 'b', 'c', 13, 1/2, 1/3] > > > I know that it is probably > > elementary but I did not find it in the help... :( > > See the Python tutorial [1] for some introductory materials on using > lists. This page [2] and this page [3] provide detailed information on > operations on lists. > > [1]http://docs.python.org/tutorial/introduction.html#lists > > [2]http://docs.python.org/library/stdtypes.html#sequence-types-str-unico... > > [3]http://docs.python.org/library/stdtypes.html#typesseq-mutable > > -- > Regards > Minh Van Nguyen -- 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
