473,379 Members | 1,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

about array

10
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
Aug 22 '09 #1

✓ answered by bvdet

Here's one way:
Expand|Select|Wrap|Line Numbers
  1. >>> some_list = [1,1,1,2,2,2,3,3,3]
  2. >>> unique_list = []
  3. >>> for item in some_list:
  4. ...     if item not in unique_list:
  5. ...         unique_list.append(item)
  6. ...         
  7. >>> unique_list
  8. [1, 2, 3]
  9. >>> 
If you are using Python 2.4 or higher:
Expand|Select|Wrap|Line Numbers
  1. >>> set(some_list)
A set can contain only unique elements. A set can be converted back to a list.

3 1614
bvdet
2,851 Expert Mod 2GB
Here's one way:
Expand|Select|Wrap|Line Numbers
  1. >>> some_list = [1,1,1,2,2,2,3,3,3]
  2. >>> unique_list = []
  3. >>> for item in some_list:
  4. ...     if item not in unique_list:
  5. ...         unique_list.append(item)
  6. ...         
  7. >>> unique_list
  8. [1, 2, 3]
  9. >>> 
If you are using Python 2.4 or higher:
Expand|Select|Wrap|Line Numbers
  1. >>> set(some_list)
A set can contain only unique elements. A set can be converted back to a list.
Aug 22 '09 #2
kudos
127 Expert 100+
I would do something like this:

Expand|Select|Wrap|Line Numbers
  1. a = [1,1,1,1,2,3,3,3,3,4]
  2. b = {}
  3. for aa in a:
  4.  b[aa] = 1
  5. print b.keys() 
  6.  
@weskam
Aug 23 '09 #3
weskam
10
Thanks very much, both of them work
Aug 23 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: J. Campbell | last post by:
I posted a question some time back about accessing a char array as an array of words. In order not to overrun the char array, I padded it with enough 0x00 bytes to ensure that when accessed as...
3
by: Joe C | last post by:
I have some code that performs bitwise operations on files. I'm trying to make the code portable on different endian systems. This is not work/school related...just trying to learn/understand. ...
5
by: Tony Johansson | last post by:
Hello experts! I have two class template below with names Array and CheckedArray. The class template CheckedArray is derived from the class template Array which is the base class This program...
3
by: J Wang | last post by:
Dear, could you tell me about the usage of "##" in preprossor give me some simple examples. thanks. I just got the example from "dissection C" as follows:
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
13
by: agentxx04 | last post by:
Hi. Our assignment was to creat a program that can find the average, median & mode of a #of integers. Here's my program: #include<stdio.h> int main() { int item; int a, b, t, mode; int...
15
by: copx | last post by:
Q1: If an array is declared static in file A is it still valid to access it from file B? I mean if a function form file A which returns a pointer to a position inside of the array is called from...
14
by: ablock | last post by:
I have an array to which i have a added a method called contains. I would like to transverse this array using for...in...I understand fully that for...in is really meant for Objects and not Arrays,...
17
by: DiAvOl | last post by:
Hello everyone, merry christmas! I have some questions about the following program: arrtest.c ------------ #include <stdio.h> int main(int agc, char *argv) {
15
by: SM | last post by:
Hello, I have another simple question about an array in PHP and a variable in PHP. This is the array: $thumbs_cat_1 = array( 'wine', 'cheese', 'ice',
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.