Search result for 'django remove duplicates'
(0.0302882194519 seconds)
10 pages : 1 2 3 4 5 6 7 8 9 10 Next › Last»

magicrebirth/Remove duplicates from a list ( Django)

def remove_duplicates(seq, idfun=None):  
    # order preserving 
    if idfun is None: 
        def idfun(x): return x 
    seen = {} 
    result = [] 
    for item in seq: 
        marker = idfun(item) 
        # in old Python versions: 
        # if seen.has_key(marker) 
        # but in new ones: 
        if marker in seen: continue 
        seen[marker] = 1 
        result.append(item) 
    return result



>>> a=list('ABeeE') 
>>> f5(a) 
['A','B','e','E'] 
>>> f5(a, lambda x: x.lower()) 
['A','B','e']

Not only is it really really fast; it's also order preserving and supports an optional transform function

Mark Olson/Remove Duplicates ( VB.NET)

    Function RemoveDuplicates(byval strSource as String) As String
        Dim intCount As Integer = 0
        Dim strReturn as String = String.Empty
        For Each str As String in strSource.split(",")
            If intCount = 0 Then    
            'First Pass
                strReturn &= "," & str & ","
            'ElseIf intCount = strSource.Split(",").Length - 1 'Last Pass
            Else
            'Anything in between.
                If Not strReturn.Contains("," & str & ",")  Then
                    strReturn &= str & ","
                End If
            End If
            intCount += 1
        Next
        strReturn = LEFT(strReturn, strReturn.Length-1)
        strReturn = RIGHT(strReturn, strReturn.Length-1)

        Return strReturn
    End Function

Removes duplicates from an array of strings, input is a comma separated list of strings, it removes the duplicates and returns the values in the original order

Udo Nesshoever/Remove duplicate items ( C#)

public List<T> RemoveDuplicates<T>(List<T> list)
{
  return list != null ? new HashSet<T>(list).ToList() : null;
}


Removes duplicate items from a list

Mihir Shah/RemoveDuplicates ( C#)

private static List<int> RemoveDuplicates(IEnumerable<int> inputList)
{
     Dictionary<int, int> uniqueStore = new Dictionary<int, int>();
     List<int> finalList = new List<int>();

     foreach (int currValue in inputList)
     {
          if (!uniqueStore.ContainsKey(currValue))
          {
               uniqueStore.Add(currValue, 0);
               finalList.Add(currValue);
          }
     }
     return finalList;
}

Removes duplicate values from a list.

Brian Syvertson/Remove Duplicate Outlook Notes ( VBA)

Sub RemoveDuplicateNotes()
    Dim olApp As Outlook.Application
    Dim olNote1 As Outlook.NoteItem
    Dim olNote2 As Outlook.NoteItem
    Dim olItems As Outlook.Items
    Dim olNS As Outlook.NameSpace
    
    Set olApp = New Outlook.Application
    Set olNS = olApp.GetNamespace("MAPI")
    Set olItems = olNS.GetDefaultFolder(olFolderNotes).Items
    olItems.Sort ("Subject")
    Dim DeleteCount As Integer
    Dim z As Integer
    DeleteCount = 0
    
    For z = olItems.Count To 2 Step -1
        Set olNote1 = olItems.Item(z)
        Set olNote2 = olItems.Item(z - 1)
        DoEvents
        If olNote1.Body = olNote2.Body Then
            olNote1.Delete
            Debug.Print "Note item " & Left(olNote2.Subject, 25) & "..." & " deleted"
            DeleteCount = DeleteCount + 1
        End If
    Next
    MsgBox DeleteCount & " duplicate Outlook Notes have been removed"
End Sub


This macro will step through Outlook notes and remove duplicates based on the content of the note body.

Brian Syvertson/Remove Duplicate Contacts in Outlook ( VBA)

Sub RemoveDuplicateContacts()
    Dim StatusMessage As String
    Dim olApp As Outlook.Application
    Dim olContact1 As Outlook.ContactItem
    Dim olContact2 As Outlook.ContactItem
    Dim olItems As Outlook.Items
    Dim olNS As Outlook.NameSpace
    
    Set olApp = New Outlook.Application
    Set olNS = olApp.GetNamespace("MAPI")
    Set olItems = olNS.GetDefaultFolder(olFolderContacts).Items
    olItems.Sort ("File As")
    Dim DeleteCount As Integer
    Dim z As Integer
    DeleteCount = 0
    StatusMessage = ""
    For z = olItems.Count To 2 Step -1
        On Error GoTo GroupFound:
ContinueAfterGroup:
        Set olContact1 = olItems.Item(z)
        Set olContact2 = olItems.Item(z - 1)
        On Error GoTo Error1:
        DoEvents
        ' Check key fields to make sure this is a duplicate
        ' Compare first and last names, home phone, mobile phone, and
        ' all 3 e-mail addresses to make sure nothing gets overlooked.
        ' Assume all other fields are the same or unimportant
        If olContact1.FileAs = olContact2.FileAs _
            And olContact1.FirstName = olContact2.FirstName _
            And olContact1.LastName = olContact2.LastName _
            And olContact1.Email1Address = olContact2.Email1Address _
            And olContact1.Email2Address = olContact2.Email2Address _
            And olContact1.Email3Address = olContact2.Email2Address _
            And olContact1.HomeTelephoneNumber = olContact2.HomeTelephoneNumber _
            And olContact1.MobileTelephoneNumber = olContact2.MobileTelephoneNumber _
          Then
            'Determine whether or not addresses exist
            If olContact1.MailingAddress = olContact2.MailingAddress Then
                ' Uncomment the following line to actually perform the deletes
                'olContact1.Delete
                StatusMessage = StatusMessage & "Contact item " & olContact2.FileAs & _
                    " deleted" & vbCrLf & vbCrLf
                Debug.Print "Contact item " & olContact2.FileAs & " deleted"
                DeleteCount = DeleteCount + 1
            Else
                StatusMessage = StatusMessage & "Mailing addresses are not the same for contacts " & _
                    olContact1.FileAs & "." & vbCrLf & _
                    "Contact not deleted. You may want to manually update " & _
                    "the contact information." & vbCrLf & vbCrLf
                Debug.Print "Mailing addresses are not the same for contacts " & _
                    olContact1.FileAs & ".  Please investigate."
            End If
        End If
    Next
    MsgBox DeleteCount & " duplicate Outlook Contacts have been removed" & _
        vbCrLf & vbCrLf & StatusMessage
    Exit Sub
GroupFound:
    z = z - 1
    Resume ContinueAfterGroup:
Error1:
    MsgBox "Whoops!  Something went horribly wrong!"
End Sub


This code will check Outlook contacts and compare key fields to determine whether or not the record is duplicated. If so, the macro "deletes" the extra record. Be sure to check the comparison criteria to meet your needs.

Ramachandran Melarcode/RemoveDuplicateFromList ( C#)

 static List<RCAUserDTO> removeDuplicates(List<RCAUserDTO> inputList)
        {
            Dictionary<RCAUserDTO, int> uniqueStore = new Dictionary<RCAUserDTO, int>();
            List<RCAUserDTO> finalList = new List<RCAUserDTO>();
            foreach (RCAUserDTO currValue in inputList)
            {
                if (!uniqueStore.ContainsValue(currValue.RCAUserID))
                {
                    uniqueStore.Add(currValue, 0);
                    finalList.Add(currValue);
                }
                /*if (!uniqueStore.ContainsKey(currValue))
                {
                    
                }*/
            }
            return finalList;
        }

Remove Duplicate items from List

Renelou Esperanzate/Remove Duplicates in DataTable ( C#)

public DataTable RemoveDuplicates(DataTable tbl,
            DataColumn[] keyColumns)
    {
        int rowNdx = 0;
        while (rowNdx < tbl.Rows.Count - 1)
        {
            DataRow[] dups = FindDups(tbl, rowNdx, keyColumns);
            if (dups.Length > 0)
            {
                foreach (DataRow dup in dups)
                {
                    tbl.Rows.Remove(dup);
                }
            }
            else
            {
                rowNdx++;
            }
        }
        return tbl;
    }

This code removes duplicates on a datatable based on the columns you had specified. It came from here: http://geekswithblogs.net/ajohns/archive/2004/06/24/7191.aspx

PsychoCoder/MSSQL snippet for removing duplicate records ( mySQL)

DECLARE @Count INT
SET @Count = 0
SELECT @Count = COUNT(*)
	FROM YourTable
	GROUP BY Column1
	HAVING COUNT(*) > 1

WHILE @Count > 0
BEGIN
	SET ROWCOUNT 1

	DELETE YourTable
	FROM YourTable t1
	JOIN (SELECT Column1, Column2
		FROM YourTable
		GROUP BY Column1, Column2
		HAVING COUNT(*) > 1
	      ) t2
	ON  t1.Column1 = t2.Column1
	AND t1.Column2 = t2.Column2
	
	SET ROWCOUNT 0

	SELECT @Count = COUNT(*)
	FROM    (SELECT Column1, Column2
		 FROM YourTable
	 	 GROUP BY Column1, Column2
		 HAVING COUNT(*) > 1
		) total
END



This is a snippet I use for "de-duping" a table (removing all duplicate entries". This process doesn't use temporary tables or cursors, adding to it's efficiency

sarmanu/Remove duplicates from a STL vector ( C++)

#include <iostream>
#include <set>
#include <vector>
#include <algorithm>

// Needed for std::set 
template <typename T>
struct Compare
{
        // Add elements to set only if 
	bool operator()(const T n1, const T n2) const
	{
		return n1 != n2;
	}
};

template <typename T>
std::vector<T> removed_duplicates(const std::vector<T> &vect)
{
        // Declare a set which will contain the non-duplicates
        // from vect
	std::set<T, Compare<T> > non_dups;

        // Insert the non-duplicates to the set
	for (size_t i = 0; i < vect.size(); i++)
		non_dups.insert(vect[i]);

	std::vector<T> result; // Holds the result
        // Declare an iterator to "set", and copy the elements from
        // non_dups to result
	std::set<T, Compare<T> >::iterator itr = non_dups.begin();
	while (itr != non_dups.end())
	{
		result.push_back(*itr);
		++itr;
	}

        // Reverse the result to prevent the order changing
        // of elements in the original vector
	std::reverse(result.begin(), result.end());
	return result;
}

// Generic output using a const_iterator
template <typename T>
void output(const std::vector<T> &vect)
{
	std::vector<T>::const_iterator citer = vect.begin();
	for (citer; citer != vect.end(); ++citer)
		std::cout << *citer << " ";
	std::cout << std::endl;
}

int main()
{
        // Declare a vector an assign some data to it
	std::vector<int> vect;
	vect.push_back(13);
	vect.push_back(20);
	vect.push_back(20);
	vect.push_back(13);
	vect.push_back(10);
	vect.push_back(20);
	vect.push_back(3);
	vect.push_back(3);
        // rd is the vector holding the original vector ("vect")
        // but with non-duplicates. As you can see, the removed_duplicates
        // function is templated, so you can pass an array of any
        // primitive data type to it.
	std::vector<int> rd = removed_duplicates<int>(vect);
	output<int>(rd); // Output the vector

        // Pause the console so the user can see the output
	std::cin.get();
	return 0;
}

Ray Linder/Remove duplicate data from table in database ( SQL)

DECLARE @ID int
DECLARE @COUNT int

DECLARE CUR_DELETE CURSOR FOR
SELECT [ID],COUNT([ID]) FROM [Example] GROUP BY [ID],HAVING COUNT([ID]) > 1

OPEN CUR_DELETE

FETCH NEXT FROM CUR_DELETE INTO @ID,@COUNT
/* Loop through cursor for remaining ID */
WHILE @@FETCH_STATUS = 0
BEGIN

DELETE TOP(@COUNT -1) FROM [Example] WHERE ID = @ID

FETCH NEXT FROM CUR_DELETE INTO @ID,@COUNT
END

CLOSE CUR_DELETE
DEALLOCATE CUR_DELETE 

Remove duplicate data from table in database

Raynes/Remove duplicate characters from a string in Clojure ( Lisp)

(defn running-dups [s] (apply str (distinct s)))

This function takes a string and returns a string in the same order with all duplicate elements removed. For example (running-dups "aaabbcccd") would return "abcd".

Szymon Kowalsky/Removing duplicate rows from a table with identity column ( SQL)

--CREATE TABLE #TableWithDuplicates (PK int PRIMARY KEY IDENTITY, Col1 nvarchar(100), Col2 nvarchar(100))
--INSERT INTO #TableWithDuplicates  VALUES ('a', 'a')
--INSERT INTO #TableWithDuplicates  VALUES ('b', 'b')
--INSERT INTO #TableWithDuplicates  VALUES ('a', 'a')
--INSERT INTO #TableWithDuplicates  VALUES ('c', 'c')
--SELECT * FROM #TableWithDuplicates  

--Show all the duplicates (including those to be left in the table)
SELECT *
FROM #TableWithDuplicates 
WHERE EXISTS (
	SELECT 1
	FROM #TableWithDuplicates AS twd
	WHERE twd.Col1 = #TableWithDuplicates.Col1 AND twd.Col2 = #TableWithDuplicates.Col2
		AND twd.PK <> #TableWithDuplicates.PK)

--Delete all duplicates - leave only the ones with the hightes value of PK
DELETE FROM #TableWithDuplicates 
WHERE EXISTS (
	SELECT 1
	FROM #TableWithDuplicates AS twd
	WHERE twd.Col1 = #TableWithDuplicates.Col1 AND twd.Col2 = #TableWithDuplicates.Col2
		AND twd.PK > #TableWithDuplicates.PK)


Removes duplicates from a table, leaves the original rows being ones with highest PK value

magicrebirth/Strip/Remove HTML tags (django utils) ( Django)

# To strip/remove HTML tags from an existing string we can use the strip_tags function.

# import the strip_tags
from django.utils.html import strip_tags

# simple string with html inside.
html = '<p>paragraph</p>'
print html # will produce: <p>paragraph</p>

stripped = strip_tags(html)
print stripped # will produce: paragraph

# This is also available as a template tag:

{{ somevalue|striptags }}


# If you want to remove only specific tags you need to use the removetags


from django.template.defaultfilters import removetags
html = '<strong>Bold...</strong><p>paragraph....</p>'
stripped = removetags(html, 'strong') # removes the strong only.
stripped2 = removetags(html, 'strong p') # removes the strong AND p tags.

# Also available in template:

{{ value|removetags:"a span"|safe }}

apphp-snippets/Remove duplicate elements from array in PHP ( PHP)

<?php
$input = array("a"=>"apple", "pear", "b"=>"apple", "orange", "avocado", "banana");
print_r($input);
$result = array_unique($input);
print_r($result);
?>

This code allows to remove all duplicate elements from an array using PHP array_unique() function.