logo

Array Pythonissa | Sarja 2 (tärkeät toiminnot)

Array Pythonissa | Sarja 1 (Johdanto ja toiminnot)

Array Pythonissa | Sarja 2

Alla on muutamia hyödyllisiä toimintoja, jotka Python tarjoaa taulukoille:

Array Type Code -funktio

Tämä funktio palauttaa tietotyypin, jolla taulukko alustetaan. Tässä esimerkissä käytämme arr.typecode-koodia selvittääksemme taulukon alustuksen tietotyypin.



Python3
# importing 'array' for array operations import array # initializing array with array values arr= array.array('i'[1 2 3 1 2 5]) # using typecode to print datatype of array print ('The datatype of array is : ') print (arr.typecode) 

Lähtö
The datatype of array is : i

Array itemsize Function

Tämä funktio palauttaa koko tavuina a s yhtenäinen taulukkoelementti. Tässä esimerkissä käytämme itemsize-funktiota selvittääksemme taulukkoelementin koon tavuina.

Python3
# importing 'array' for array operations import array # initializing array with array values arr= array.array('i'[1 2 3 1 2 5]) # using itemsize to print itemsize of array print ('The itemsize of array is : ') print (arr.itemsize) 

Lähtö
The itemsize of array is : 4

puskuri_info() Pythonissa

Palauttaa monikon, joka edustaa osoite, johon matriisi on tallennettu, ja siinä olevien elementtien lukumäärä. Tässä esimerkissä käytämme buffer_info() tehdäksesi saman.

Python3
# importing 'array' for array operations import array # initializing array with array values arr= array.array('i'[1 2 3 1 2 5]) # using buffer_info() to print buffer info. of array print ('The buffer info. of array is : ') print (arr.buffer_info()) 

Lähtö
The buffer info. of array is : (140491260368688 6)

count() Pythonissa

Python count () funktio laskee esiintymien lukumäärän taulukossa mainitusta argumentista.



expand() Pythonissa

Tämä toiminto liittää argumenteissaan mainitun kokonaisen taulukon määritettyyn taulukkoon. Tässä esimerkissä käytämme Extend()-funktiota toisen taulukon lisäämiseen.

Python3
# importing 'array' for array operations import array # initializing array with array values arr1 = array.array('i'[1 2 3 1 2 5]) arr2 = array.array('i'[1 2 3]) # using extend() to add array 2 elements to array 1  arr1.extend(arr2) print ('The modified array is : ') for i in range (09): print (arr1[i] end=' ') 

Lähtö
The modified array is : 1 2 3 1 2 5 1 2 3 

Array fromlist() -funktio

Tämä toiminto on tottunut Liitä sen argumentissa mainittu luettelo taulukon loppuun. Tässä esimerkissä käytämme fromlist()-komentoa listan lisäämiseen taulukon loppuun.

Python3
# importing 'array' for array operations import array # initializing array with array values arr = array.array('i'[1 2 3 1 2 5]) li = [1 2 3] # using fromlist() to append list at end of array arr.fromlist(li) # printing the modified array print ('The modified array is : 'end='') for i in range (09): print (arr[i]end=' ') 

Lähtö
The modified array is : 1 2 3 1 2 5 1 2 3 

tolist() Pythonissa

Tätä funktiota käytetään taulukon muuntamiseen luetteloksi. Tässä esimerkissä käytämme tolist()-funktiota taulukon muuntamiseen listaksi.



Python3
# importing 'array' for array operations import array # initializing array with array values arr = array.array('i'[1 2 3 1 2 5]) # using tolist() to convert array into list li2 = arr.tolist() # printing the new list print ('The new list created is : 'end='') for i in range (0len(li2)): print (li2[i]end=' ') 

Lähtö
The new list created is : 1 2 3 1 2 5