Hi
I need to write a program and part it's functionality is to delete unique items in an array. That is, delete items in the array that only occur once.
I also need to delete the first occurrence of repeated items, and I'm pretty sure the solution to these two will go hand in hand.
Thanks
akadeco
6 9995 bvdet 2,851
Expert Mod 2GB
akadeco,
Are you asking for help with your assignment? We can't help until you show some effort to solve this yourself.
BV
Moderator
(I guess the assignment is too small to be a school assignment) -
a = [1,2,3,4,3,4,4,4,5,6]
-
b = {}
-
i = 0
-
for c in range(len(a)):
-
if(b.has_key(a[i]) == False):
-
b[a[i]] = 1
-
a.remove(a[i])
-
else:
-
i+=1
-
print a
-
-kudos @akadeco bvdet 2,851
Expert Mod 2GB
Here's another way that creates a new list. - >>> a = [1,2,3,4,3,4,4,4,5,6]
-
>>> b = []
-
>>> for j, item in enumerate(a):
-
... if a.count(item) > 1 and j > a.index(item):
-
... b.append(item)
-
...
-
>>> b
-
[3, 4, 4, 4]
-
>>>
-
a = [1,2,3,4,3,4,4,4,5,6]
-
b=[]
-
for c in a:
-
if(a.count(c) >=2 and b.count(c) < a.count(c)-1):
-
b.append(c)
-
print b
-
Can we avoid using the list 'b'?
-kudos
bvdet 2,851
Expert Mod 2GB
With a list comprehension: - >>> a = [item for j, item in enumerate(a) if a.count(item) > 1 and j > a.index(item)]
-
>>> a
-
[3, 4, 4, 4]
-
>>>
@Kudos thank you so much that is exactly what I needed!
Thank you all for your swift replies
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Sims |
last post by:
Hi,
I have a table that give a unique ID to certain Articles.
To give the ID i simply use the Auto count of the DB.
But what happens when i delete a record? i am now missing an ID.
How can i...
|
by: Colin Steadman |
last post by:
I have a multi-dimensional array that I want to delete
items from. To do this I display a form and the user
clicks some tick boxes.
Then on the delete page I check which tick boxes were...
|
by: Jonan |
last post by:
Hello,
For several reasons I want to replace the built-in memory management with
some custom built. The mem management itlsef is not subject to my question -
it's ok to the point that I have...
|
by: nescio |
last post by:
hello,
i have an array and i don't know the content of it, but i want only unique
values.
in php there is a function to do this, but how must i do this in javascript?
i have tried a lot and...
|
by: |
last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily
complicated than the other languages I know. I feel this is because there
aren't many good official resources out there to...
|
by: Brian Tkatch |
last post by:
I'm looking for a simple way to unique an array of strings. I came up
with this. Does it make sense? Am i missing anything? (Testing seems to
show it to work.)
Public Function Unique(ByVal...
|
by: JH Programmer |
last post by:
Hi,
is there any ways that allow us to delete an element in between?
say
int_val: 1
int_val: 2
int_val: 3
|
by: pezholio |
last post by:
Hi,
I'm trying to put together a page that deletes records from a database based on if an item is unticked, I've got the existing items in an array (let's call it array1 for the sake of argument):...
|
by: mark4asp |
last post by:
Stack of limited size containing unique items?
Hi guys,
How would I implement a stack of limited size containing unique items?
For example. Suppose my stack has . I add 2 to it and it is now...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |