set() metode tiek izmantota, lai jebkuru iterējamo pārvērstu iterējamo elementu secībā ar atšķirīgiem elementiem, ko parasti sauc par kopu. Programmā Python funkcija set() ir iebūvēts konstruktors, ko izmanto, lai inicializētu kopu vai izveidotu tukšu. Šajā rakstā mēs redzēsim par set() programmā Python un to, kā mēs varam pārvērst iterable par secību ar unikāliem elementiem programmā Python .
Python set() metodes sintakse
Sintakse : komplekts (atkārtojams)
Parametri: Jebkura atkārtojama secība, piemēram, saraksts, virkne vai vārdnīca.
Atgriešanās: Tukša kopa, ja netiek nodots neviens elements. Neatkārtots elements ir iterējams, mainīts kā arguments.
Kas ir Python set() funkcija?
Set, termins matemātikā secībai, kas sastāv no atšķirīgām valodām, arī tiek paplašināts tās valodā par Python un to var viegli izveidot, izmantojot set (). set() metode tiek izmantota, lai iterējamu pārveidotu par secību ar unikāliem Python elementiem, ko parasti sauc par Set. Tā ir iebūvēta konstruktora funkcija, ko izmanto, lai izveidotu tukšu kopu vai inicializētu kopu ar elementiem.
Linux uzdevumu pārvaldnieks
Python set() metodes īpašības
- Lai izveidotu tukšu kopu, netiek nodoti parametri
- Vārdnīcu var izveidot arī, izmantojot komplektu, taču pēc konvertēšanas paliek tikai atslēgas, un vērtības tiek zaudētas.
set() funkcija Python piemēros
Tālāk ir norādīti veidi, kā Python var izmantot set ()
- Tukšas komplekta izveide
- Izmantojot set() ar sarakstu
- Izmantojot set() ar Tuples
- Komplekta izveide ar diapazonu
- Vārdnīcas pārvēršana komplektā
Kopas izveide, izmantojot set() funkciju
Šajā piemērā mēs izveidojam a Iestatīt izmantojot funkciju set().
Python3
# we are creating an> #empty set by using set()> > s>=> set>()> print>(>'Type of s is '>,>type>(s))> |
>
>Izvade
Type of s is>
set() Funkcija ar sarakstu
Šajā piemērā mēs izmantojam set() ar Saraksts . Šeit mēs pārveidosim iterējamu secību ar unikāliem Python elementiem.
Python3
# working of set() on list> # initializing list> lis1>=> [>3>,>4>,>1>,>4>,>5> ]> > # Printing iterables before conversion> print>(>'The list before conversion is : '> +> str>(lis1))> > # Iterables after conversion are> # notice distinct and elements> print>(>'The list after conversion is : '> +> str>(>set>(lis1)))> |
>
>Izvade
The list before conversion is : [3, 4, 1, 4, 5] The list after conversion is : {1, 3, 4, 5}> set() Funkcija ar Tuple
Šajā piemērā mēs izmantojam funkciju set() ar korts .
Python3
# working of set() on tuple> # initializing tuple> tup1>=> (>3>,>4>,>1>,>4>,>5>)> > # Printing iterables before conversion> print>(>'The tuple before conversion is : '> +> str>(tup1))> > # Iterables after conversion are> # notice distinct and elements> print>(>'The tuple after conversion is : '> +> str>(>set>(tup1)))> |
alfabēts ar cipariem
>
>Izvade
The tuple before conversion is : (3, 4, 1, 4, 5) The tuple after conversion is : {1, 3, 4, 5}> set() Funkcija ar diapazonu
Šajā piemērā mēs izmantojam funkciju set() ar diapazons funkciju. Šeit mēs pārveidosim iterējamu secību ar unikāliem Python elementiem.
Python3
int parseint
# working of set() on range> > # initializing range> r>=> range>(>5>)> > r>=>set>(r)> # Iterables after conversion are> # notice distinct and elements> print>(>'The Range after conversion is : '> +> str>(r))> |
>
>Izvade
The Range after conversion is : {0, 1, 2, 3, 4}> Set() metodes demonstrēšana ar vārdnīcu
Šajā piemērā mēs redzam set() demonstrāciju ar Vārdnīca un tas darbojas.
Python3
# Python3 code to demonstrate the> # working of set() on dictionary> > # initializing list> dic1>=> {>4> :>'geeks'>,>1> :>'for'>,>3> :>'geeks'> }> > # Printing dictionary before conversion> # internally sorted> print>(>'Dictionary before conversion is : '> +> str>(dic1))> > # Dictionary after conversion are> # notice lost keys> print>(>'Dictionary after conversion is : '> +> str>(>set>(dic1)))> |
>
>Izvade
Dictionary before conversion is : {4: 'geeks', 1: 'for', 3: 'geeks'} Dictionary after conversion is : {1, 3, 4}>