Python | funkcija set().

set() metoda se uporablja za pretvorbo katerega koli ponovljivega elementa v zaporedje ponovljivih elementov z različnimi elementi, ki se običajno imenuje Set. V Pythonu je funkcija set() vgrajeni konstruktor, ki se uporablja za inicializacijo nabora ali ustvarjanje praznega. V tem članku si bomo ogledali set() v Pythonu in kako lahko iterable pretvorimo v zaporedje z edinstvenimi elementi v Python .

Sintaksa metode Python set().

Sintaksa : set (iterable)
Parametri: Vsako ponovljivo zaporedje, kot je seznam, tuple ali slovar.
Vrnitve: Prazen niz, če ni posredovan noben element. Neponavljajoči se element, ki ga je mogoče ponoviti, spremenjen kot posredovan kot argument.

Kaj je funkcija Python set()?

Množica, izraz v matematiki za zaporedje, ki je sestavljeno iz različnih jezikov, se v svojem jeziku prav tako razširi z Python in ga je mogoče enostavno narediti z uporabo set(). metoda set() se uporablja za pretvorbo iterable v zaporedje z edinstvenimi elementi v Pythonu, običajno imenovano Set. Je vgrajena funkcija konstruktorja, ki se uporablja za ustvarjanje praznega niza ali inicializacijo niza z elementi.

Lastnosti metode Python set().

  • Za ustvarjanje praznega nabora niso posredovani nobeni parametri
  • Slovar je mogoče ustvariti tudi z naborom, vendar po pretvorbi ostanejo samo ključi, vrednosti pa se izgubijo.

Funkcija set() v primerih Pythona

Spodaj so načini, kako lahko uporabimo set() v Pythonu:

  • Ustvarjanje praznega niza
  • Uporaba set() s seznamom
  • Uporaba set() s Tuples
  • Ustvarjanje niza z Range
  • Pretvarjanje slovarja v komplet

Ustvarjanje nabora z uporabo funkcije set().

V tem primeru ustvarjamo a Set z uporabo funkcije set().

Python3




# we are creating an> #empty set by using set()> > s> => set> ()> print> (> 'Type of s is '> ,> type> (s))>

Izhod

Type of s is 

set() funkcija s seznamom

V tem primeru uporabljamo set() z Seznam . Tukaj bomo pretvorili iterable v zaporedje z edinstvenimi elementi v Pythonu.

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)))>

Izhod

The list before conversion is : [3, 4, 1, 4, 5] The list after conversion is : {1, 3, 4, 5} 

funkcija set() s Tuple

V tem primeru uporabljamo funkcijo set() z tuple .

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)))>

Izhod

The tuple before conversion is : (3, 4, 1, 4, 5) The tuple after conversion is : {1, 3, 4, 5} 

funkcija set() z obsegom

V tem primeru uporabljamo funkcijo set() z obseg funkcijo. Tukaj bomo pretvorili iterable v zaporedje z edinstvenimi elementi v Pythonu.

Python3




# 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))>

Izhod

The Range after conversion is : {0, 1, 2, 3, 4} 

Predstavitev metode set() s slovarjem

V tem primeru vidimo predstavitev funkcije set() z Slovar in deluje.

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)))>

Izhod

Dictionary before conversion is : {4: 'geeks', 1: 'for', 3: 'geeks'} Dictionary after conversion is : {1, 3, 4}