">
 

Python Lists : Common way to store data

Iniciado por joomlamz, Hoje at 18:25

Respostas: 0   |   Visualizações: 4

Tópico anterior - Tópico seguinte

0 Membros e 2 Visitantes estão a ver este tópico.

Python Lists : Common way to store data



Tópico: Python Lists : Common way to store data
Categoria: Tutoriais | Programação & Tecnologia
Idioma Principal: Português (Conteúdo de Tecnologia)

Descrição do Conteúdo / Informações:
-------------------------------------------------------------------------
Python has 4 built Data structures that dictate how data is stored, accessed and accessed.

1. Sets

2. Dictionaries

3. Tuples

4. Lists



Characteristics of List


1. Stored in square brackets [ ]

2. Ordered

3. Mutable

4. Can hold duplicate values



How to Access and Read Lists


Index

Lists can be accessed through index( 0 as the beginning index on the furthest left and -1 when starting from the last element )

Slicing

In slicing we aim to use indexing to extract a part of our list, but a key part to be know is that the end isn't included in our sub_list

Code : my_list [start: stop: step]



How to Unpack


Unpacking is taking all the items in a list and assigning variables in order to perform operations.

In places where you don't want to assign a variable you use an underscore



How to explore and analyze


To explore the the highest, lowest, how big our list is

len(my_list): Returns the total number of elements in the list.

max(my_list): Finds the highest value (numeric or alphabetical).

min(my_list): Finds the lowest value.

sum(my_list): Calculates the total sum of all numeric values

How to search for specific items and count how often they appear

my_list.count(x): Counts how many times the value x appears.

my_list.index(x): Finds the index position of the first occurrence of x

How to check membership and identity

x in my_list

x is my_list

How to check quality of our list

all(my_list): Find all values with real meaning, if we have missing values the feedback will be false

any(my_list): Have at least 1 value, you only get a false if all items are missing .



How to change


Append

Appends adds new data at the end of the list

my_list.append(value)

Insert

Insert adds specific values at specific position

my_list.insert(index, value)

Clear

Deletes all the items in the list

my_list.clear()

Remove by value

This removes the first items of that value

my_list.remove(value)

Deleting by position (pop)

This you need to specify the index position of item you want to delete and it returns what has been removed. You need to specify the index in order to avoid deleting all information on the list

my_list.pop(index)

Update

In this case we use indexing and the = sign , if you don't specify the index you will end up updating the whole list

my_list[index] = new_value



How to order a list in python


Sort

We can change the order of the original list using sort()

my_list.sort() will order the original list in an ascending order

my_list.sort(reverse=True) will sort the original list in a descending order

Sorted

This method , leaves the original list creates a new one copy and changes the order

new_list = sorted(my_list) creates a copy and sorts in an ascending order

new_list_desc = sorted(my_list, reverse=True) creates a copy and sorts in a descending order

Reverse

No sorting logic is involved here, it just reverses the structure of the list(last to first)

This changes the original list

my_list = ["A", "B", "C", "D"]

Reverse - changes the original list : my_list.reverse()

print(my_list) :  ['D', 'C', 'B', 'A']

Reversed

This creates a copy and reverses it

my_list = ["A", "B", "C", "D"]

new_list = list(reversed(my_list))

print(new_list) : ['D', 'C', 'B', 'A']


Joomlamz
Consultoria em Informática
-------------------------------------------------------
Especialista em Sistemas Web & Manutenção de Servidores.
A desenvolver o novo AplPortal com suporte a PHP 8.
Precisa de ajuda profissional? Contacte-me.

Tags: