Python: luo ja käytä taulukkoja - miten se toimii
Ohjelmoidessasi Pythonilla kompastuit nopeasti ryhmiin. Tämä Python-opas näyttää sinulle, kuinka luoda ja käyttää niitä. Koska ohjelmointi Pythonilla ei ole niin vaikeaa.
Luo luetteloita Pythoniin
Klassisia "taulukkoja", kuten Java, ei ole Pythonissa. Voit kuitenkin luoda ns. Luetteloita, jotka toimivat samalla tavalla. Voit myös jälkiasentaa matriiseja moduuleja kohden, mitä näytämme toisessa kappaleessa.
- Voit luoda luettelon esimerkiksi komennolla "a = [1, 2, 3]". Tämä luettelo sisältää kolme kokonaislukuarvoa.
- Voit esimerkiksi tulostaa tietyn arvon "print (a [0])" -komennolla. Tässä tapauksessa numero 1 tulostetaan, koska tietokone alkaa laskea nollasta. Voit myös muuttaa luettelomerkintöjä samalla tavalla: "a [0] = 15".
- Voit myös luoda moniulotteisia luetteloita. Esimerkki tästä on komento "b = [[1, 2, 3], [4, 5, 6]]". Komento "print (b [1] [0])" tuottaa numeron 4.
- Voit tulostaa tietyt luettelon alueet painikkeella "tulosta (a [2: 5])" tai määritellä useita luettelomerkintöjä samanaikaisesti (komento: "a [0: 2] = [0, 1]").
- Yhteydet silmukoiden kanssa ovat erittäin käytännöllisiä. "For i in::" -silmukassa otan luettelon jokaisen arvon kerran toistettuna (yksi toisensa jälkeen). Se vaihdetaan jokaisen ajon jälkeen. Mutta ole varovainen: kun iteroidaan kaksiulotteisen luettelon kanssa for-silmukalla, toistettava on myös luettelo. Täällä tarvitset silmukan silmukan silmukkaan.
- Luetteloiden lisäksi Pythonissa on myös toinen tapa käyttää taulukkoja. Tätä varten sinun on ensin asennettava sopiva moduuli:
Ryhmät Pythonissa: Asenna ensin NumPy-moduuli
Ennen kuin aloitat taulukkojen luomisen, sinun on ensin asennettava NumPy-moduuli. Koska tätä ei yleensä ole esiasennettu. Näin se toimii Windowsissa:
- Avaa komentokehote tietokoneellasi näppäinyhdistelmällä [Windows-näppäin] + [R] ja komennolla "CMD".
- Vaihda sitten Python-järjestelmäkansion Scripts-alikansioon muutoshakemistokomennolla: Voit tehdä tämän kirjoittamalla "cd C: \ Users \ käyttäjän käyttäjänimi \ AppData \ Local \ Programs \ Python \ Python36 \ Scripts". Huomaa kuitenkin, että polku tähän alikansioon voi vaihdella käyttöjärjestelmän ja Python-version mukaan. Joten tarkista etukäteen, missä kansio on.
- Suorita sitten "pip install numpy" -komento asentaaksesi NumPy.
Python-opas: luo taulukot
Nyt voit luoda helposti taulukon NumPy-moduulilla:
- Ensin on tuotava NumPy-moduuli komennolla "import numpy as np" (ilman lainausmerkkejä).
- Nyt voit luoda ensimmäisen taulukon komennolla "x = np.array ([1, 2, 3, 4])". Tässä tapauksessa taulukko tallennetaan muuttujan "x" alle.
- Voit saada tämän taulukon ulostulon testattavaksi "print (x)" -komennolla.
- Voit myös luoda taulukkoja, joissa on useita "ulottuvuuksia". Esimerkki tästä on: "y = np.matriisi ([[1, 2, 3], [4, 5, 6], [7, 8, 9]])". Jos tulostat tämän taulukon painikkeella "print (y)", näet, että 3x3-kenttä on tulostettu. Ensimmäisellä rivillä numerot 1, 2 ja 3 ovat toisessa 4, 5 ja 6 ja lopulta viimeiset 7, 8 ja 9.
- Voit halutessasi luoda np.arange (10) -komennon avulla taulukon, joka sisältää kaikki kokonaisluvut välillä 0–9.
- Komento "np.arange (2, 10, dtype = np.float)" luo puolestaan taulukon, joka sisältää kaikki kokonaisluvut välillä 2 - 9 liukulukuarvona ("kelluva"). Joten tämä sisältää numerot 2.0, 3.0, 4.0 jne.
- Esimerkiksi, voit käyttää "np.arange (2, 3, 0.1)" -komentoa luodaksesi taulukon, joka sisältää numerot välillä 2.0 - 2.9. Arvot luodaan vaiheissa 0, 1, ts. 2.0, 2.1, 2.2 jne.
- Toinen käytännöllinen komento on "np.linspace (1, 2, 3)". Tämä komento antaa taulukon, joka sisältää kolme numeroa yhden ja kahden välillä. Erityinen asia tässä on, että numerot ovat aina saman etäisyyden päässä toisistaan. Tässä tapauksessa numerot 1.0, 1.5 ja 2.0 tulostetaan.
- Lopuksi on komento "np.indices ((4, 3))". Tämä luo joukon, jolla on monia ulottuvuuksia, jokaisen ulottuvuuden ollessa edellisen muunnelma. Tässä tapauksessa luodaan kaksi ulottuvuutta, joissa neljä riviä. Molemmilla mitoilla on kolme numeroa riviä kohti.
Lisää vinkkejä Pythonista CHIP-oppaissamme
Vinkki: Python-ohjelmointikielen aihepiirisivulta löydät lisäohjeet ohjelmoinnista. Siellä me esimerkiksi osoitamme, kuinka voit käyttää etäisyyttä ja xrangea.