Python: vytváranie a používanie polí - ako to funguje
Pri programovaní s programom Python rýchlo narazíte na polia. Táto príručka Python vám ukáže, ako ich vytvoriť a používať. Pretože programovanie pomocou Pythonu nie je také ťažké.
Vytvárajte zoznamy v Pythone
Klasické „polia“ ako v Java neexistujú v Pythone. Môžete však vytvoriť takzvané zoznamy, ktoré fungujú podobným spôsobom. Polia môžete dodatočne vybaviť aj modulom, ktorý vám ukážeme v druhom odseku.
- Zoznam môžete vytvoriť napríklad pomocou príkazu „a = [1, 2, 3]“. Tento zoznam obsahuje tri celočíselné hodnoty.
- Napríklad môžete použiť príkaz „print (a [0])“ na výstup konkrétnej hodnoty. V tomto prípade bude číslo 1 na výstupe, pretože počítač začne počítať od 0. Rovnakým spôsobom môžete zmeniť aj položky v zozname: "a [0] = 15".
- Môžete tiež vytvoriť viacrozmerné zoznamy. Príkladom toho je príkaz "b = [[1, 2, 3], [4, 5, 6]]". Príkaz „print (b [1] [0])“ vyvedie číslo 4.
- Niektoré oblasti zoznamu môžete vytlačiť pomocou príkazu „print (a [2: 5])“ alebo predefinovať niekoľko záznamov naraz (príkaz: „a [0: 2] = [0, 1]“).
- Zoznamy v spolupráci s pre slučky sú veľmi praktické. V slučke „pre i v a:“ preberám každú hodnotu v zozname raz, keď je iterovaná (jedna za druhou). Po každom spustení sa zmení. Ale buďte opatrní: pri iterácii nad dvojrozmerným zoznamom so slučkou for je iterovateľný aj zoznam. Tu potrebujete slučku for for v slučke for for.
- Okrem zoznamov existuje v Pythone ďalší spôsob použitia polí. Najprv musíte nainštalovať príslušný modul:
Polia v Pythone: Najskôr nainštalujte modul NumPy
Predtým, ako začnete vytvárať polia, musíte najskôr nainštalovať modul NumPy. Pretože to zvyčajne nie je predinštalované. Ako to funguje v systéme Windows:
- Na počítači otvorte príkazový riadok s kombináciou klávesov [kláves Windows] + [R] a príkaz „CMD“.
- Potom prejdite do podpriečinka „Skripty“ vášho systémového priečinka Python príkazom change directory: Ak to chcete urobiť, zadajte „cd C: \ Users \ yourusername \ AppData \ Local \ Programs \ Python \ Python36 \ Scripts“. Cesta k tomuto podpriečinku sa však môže líšiť v závislosti od verzie operačného systému a verzie Python. Najprv teda skontrolujte, kde sa priečinok nachádza.
- Potom spustite príkaz „pip install numpy“ a nainštalujte NumPy.
Sprievodca Pythonom: vytvára polia
Teraz môžete s modulom NumPy ľahko vytvoriť pole:
- Najprv musíte naimportovať modul NumPy príkazom „import numpy as np“ (bez úvodzoviek).
- Teraz môžete vytvoriť prvé pole príkazom "x = np.array ([1, 2, 3, 4])". V takom prípade sa pole uloží pod premennou „x“.
- Výstup tohto poľa môžete ľahko testovať pomocou príkazu „print (x)“.
- Môžete tiež vytvoriť polia s viacerými „rozmermi“. Príkladom toho je: "y = np. Pole ([[1, 2, 3], [4, 5, 6], [7, 8, 9]])". Ak vytlačíte toto pole s „print (y)“, uvidíte, že sa vytlačí pole 3x3. V prvom riadku sú čísla 1, 2 a 3 v druhom 4, 5 a 6 a nakoniec v posledných 7, 8 a 9.
- Ak chcete, môžete pomocou príkazu np.arange (10) vytvoriť pole, ktoré obsahuje všetky celé čísla od 0 do 9.
- Príkaz "np.arange (2, 10, dtype = np.float)" zase vytvorí pole, ktoré obsahuje všetky celé čísla od 2 do 9 ako číslo s pohyblivou rádovou čiarkou ("float"). Toto obsahuje čísla 2.0, 3.0, 4.0 atď.
- Napríklad môžete použiť príkaz „np.arange (2, 3, 0, 1)“ na vytvorenie poľa, ktoré obsahuje čísla od 2, 0 do 2, 9. Hodnoty sa vytvárajú v krokoch po 0, 1, tzn. 2, 0, 2, 1, 2, 2 atď.
- Ďalším praktickým príkazom je „np.linspace (1, 2, 3)“. Tento príkaz vydá pole, ktoré obsahuje tri čísla medzi jedným a dvoma. Zvláštnosťou je, že čísla sú vždy od seba vzdialené. V tomto prípade by boli na výstupe čísla 1.0, 1.5 a 2.0.
- Nakoniec je tu príkaz „np.indices ((4, 3))“. Takto sa vytvorí pole s mnohými rozmermi, pričom každá dimenzia je variáciou predchádzajúcej. V tomto prípade sa vytvoria dve dimenzie, každá so štyrmi riadkami. Oba rozmery majú tri čísla na riadok.
Viac tipov na Python nájdete v našich sprievodcoch CHIP
Tip: Na našej tematickej stránke o programovacom jazyku Python nájdete ďalšie návody o programovaní. Tu vám ukážeme napríklad, ako môžete použiť rozsah a rozsah.