Port.forumgreek.com
Θέλετε να αντιδράσετε στο μήνυμα; Φτιάξτε έναν λογαριασμό και συνδεθείτε για να συνεχίσετε.

Remove Duplicates from Array in Python

Επισκόπηση προηγούμενης Θ.Ενότητας Επισκόπηση επόμενης Θ.Ενότητας Πήγαινε κάτω

Rishu1
Αριθμός μηνυμάτων : 1
Πόντοι : 2
Ημερομηνία εγγραφής : 18/08/2023

ΔημοσίευσηRishu1 Παρ Αυγ 18, 2023 2:50 pm

I'm attempting to use Python to resolve the duplicates in an array problem. Online, I've found a few suggestions, but I'm not sure which is the most useful. In my cited essay, Python Tutorial:  [Πρέπει να είστε εγγεγραμμένοι και συνδεδεμένοι για να δείτε αυτόν το σύνδεσμο.]

Using a set is one option. One kind of data structure that only stores singular components is a set. To produce an array without duplicate elements, we can first create a set of the array's components.

Κώδικας:
def remove_duplicates(array):

seen = set()
  new_array = []
  for element in array:
    if element not in seen:
      seen.add(element)
      new_array.append(element)
  return new_array
  


Another solution is to use a hash table. A data structure that connects keys and values ​​​​is known as a hash table. Each element of the array can be converted into a unique number using a hash table. Then, by repeating the process, we may add hash table items to the new array that only have unique numbers.

Κώδικας:
def remove_duplicates(array):
  seen = {}
  new_array = []
  for element in array:
    if element not in seen:
      seen[element] = 1
      new_array.append(element)
  return new_array

Which of these tactics has the best results? Is there a more effective approach in Python to eliminate duplicates from an array?

Επισκόπηση προηγούμενης Θ.Ενότητας Επισκόπηση επόμενης Θ.Ενότητας Επιστροφή στην κορυφή

Δημιουργήστε έναν λογαριασμό ή συνδεθείτε για να απαντήσετε

Προκειμένου να απαντήσετε πρέπει να είστε μέλος.

Δημιουργία Λογαριασμού

Ενταχθείτε στην κοινότητά μας δημιουργώντας έναν λογαριασμό. Είναι πανεύκολο!


Δημιουργία ενός νέου Λογαριασμού

Σύνδεση

Έχετε ήδη έναν λογαριασμό; Κανένα πρόβλημα, συνδεθείτε εδώ.


Σύνδεση

 
Δικαιώματα σας στην κατηγορία αυτή
Δεν μπορείτε να απαντήσετε στα Θέματα αυτής της Δ.Συζήτησης