^^Python. list

 

  1. list can be changed (are mutable), unlike string.
  2. nested list are possible

list construction

[]  or  list()
empty list
[a, b, c,]
square brackets, separating items with commas
[x for x in iterable]  
list comprehension
list(iterable)
type constructor

list() built-in function, create a list

list()
[]
 
list('abc')
['a', 'b', 'c']
 
list( (1, 2, 3) )  
[1, 2, 3]
passare da tupla a lista
list(range(4))
[0, 1, 2, 3]
 

Many other operations also produce lists, es sorted() built-in.

list({'c':5,'a':2,'b':4}) ->  ['c', 'a', 'b']

  la lista di un dizionario e' la lista delle chiavi, i valori non sono considerati.

 

The constructor list() builds a list whose items are the same and in the same order as iterable’s items.

 iterable may be

1: a sequence; 2: a container that supports iteration, es zip; 3: an iterator object.

If iterable is already a list, a copy is made and returned, similar to iterable[:].

list op

Lists implement all of the common and mutable sequence operations.

list sort

sorted() Built-in Function.

Lists also provide the following additional method:

sort(*, key=None, reverse=False) 

this method sorts the list in place.

 

 

 {'c':5,'a':2,'b':4}.values()    dict_values([5, 2, 4])
 
list(zip(['one', 'two', 'three'], [1, 2, 3]))
 [('one', 1), ('two', 2), ('three', 3)]
 

list comprehension.     wp/List_comprehension

  1. [f(x) for x in X]
  2. [f(x) for x in X if g(x)]
  3. [f(x) if g(x) else h(x) for x in X]
  4. [f(x, y)  for x in X  for y in Y]
  5. [f(x, y)  for x in X  for y in Y if P(y)]
  6. [f(x, y)  for x in X if P(x)  for y in Y]
  7. [f(x, y)  for x in X if P(x)  for y in Y if Q(y)]

 

  1. [c for c in range(3)]             [0, 1, 2]
    [str(c) for c in range(3)]        ['0', '1', '2']
  2. [x for x in range(3) if x%2==0]   [0, 2]
  3. ['pari' if x%2==0 else 'dispari' for x in range(3)]
     ['pari', 'dispari', 'pari'] 
  4. [(x, y) for x in range(1, 4) for y in range(7, 9)]
     [(1, 7), (1, 8), (2, 7), (2, 8), (3, 7), (3, 8)]
  5. [(x, y) for x in range(1, 4) for y in range(7, 9) if y%2==0]
     [(1, 8), (2, 8), (3, 8)]
  6. [(x, y) for x in range(1, 4) if x%2==0 for y in range(7, 9)]
     [(2, 7), (2, 8)]
  7. [(x, y) for x in range(1, 4) if x%2==0 for y in range(7, 9) if y%2==0]
      [(2, 8)]

 

 

[range(i, i + 3) for i in [2, 4, 6]]
 [range(2, 5), range(4, 7), range(6, 9)]
 
[list(range(i, i + 3)) for i in [2, 4, 6]]
 [[2, 3, 4], [4, 5, 6], [6, 7, 8]]

 

Selezionare le parole con 1 ricorrenza

LT lista di 2-tuple, cioe' coppie

LT =[('cespuglio', 1), ('sdrucciolarono', 1), ('rintronare', 1), ('potevano', 3), ('monticello', 1), ('nello', 6), ('strizzoni', 1), ('udirono', 2), ('compagne', 1), ('passandoci', 1), ('appena', 42), ('assistono', 2), ('fieramente', 1), ('scioccherie', 2), ('bella', 40), ('palla', 4), ('diventano', 3), ('fiammiferi', 2), ('pugni', 1), ('piace', 4)]

[e[0] for e in LT  if e[1] == 1]

syntax

[f(x) for x in X if g(x)]

Estrarre una sottosequenza a caso

ref: library/random.html#functions-for-sequences

L e' una lista

  1. random.choice(L)

    equivale a

    L[random.randrange(len(L))

  2. [random.choice(L) for i in range(30)]

 

Links

  1. Operations on lib/Mutable Sequence Types
  2. comprehension
  3. https://stackoverflow.com/questions/4260280/if-else-in-a-list-comprehension
  4. book.pythontips/comprehensions.html