about array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • weskam
    New Member
    • Jul 2009
    • 10

    about array

    if there are same elements in the list, how can i take them away and make the list unique..... somethings like array_unique in PHP.... i have consult this web site, but it doesn't talk about it
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Here's one way:
    Code:
    >>> some_list = [1,1,1,2,2,2,3,3,3]
    >>> unique_list = []
    >>> for item in some_list:
    ... 	if item not in unique_list:
    ... 		unique_list.append(item)
    ... 		
    >>> unique_list
    [1, 2, 3]
    >>>
    If you are using Python 2.4 or higher:
    Code:
    >>> set(some_list)
    A set can contain only unique elements. A set can be converted back to a list.

    Comment

    • kudos
      Recognized Expert New Member
      • Jul 2006
      • 127

      #3
      I would do something like this:

      Code:
      a = [1,1,1,1,2,3,3,3,3,4]
      b = {}
      for aa in a:
       b[aa] = 1
      print b.keys()
      Originally posted by weskam
      if there are same elements in the list, how can i take them away and make the list unique..... somethings like array_unique in PHP.... i have consult this web site, but it doesn't talk about it
      http://www.java2s.com/Code/Python/List/CatalogList.htm

      Comment

      • weskam
        New Member
        • Jul 2009
        • 10

        #4
        Thanks very much, both of them work

        Comment

        Working...