标签:in place close other dea struct members array bit art
Python lists are
ordered collections of arbitrary objects
mutable sequence
mutable: can be changed in place
sequence operations: indexing, slicing, concatenation, iteration (sequences maintain a left-to-right positional ordering among the items they contain)
'''Basic List Operations'''
# ------- basic ------- #
L1 = [1, 2, 3] + [4, 5, 6] # concatenation
L2 = ['Ni!'] * 4 # repetition
print(L1)
print(len(L1)) # length
# ------- iteration and comprehensions ------- #
print(3 in L1) # membership test
for x in L1: # iteratiom
print(x)
# ------- indexing, slicing, nesting ------- #
L = ['spam', 'Spam', 'SPAM!']
print(
L[2], # positive: offsets start at 0
L[-2], # negative: count from the right, offsets start at -1
L[1:] # slicing returns a new list
)
'''
one of the simplest ways to represent matrixes (multidimensional arrays) in Python is as lists with nested sublists.
For an array of size N × M, the rows and columns are numbered from 0 to N-1 and columns are numbered from 0 to M-1, respectively
Here is a basic 3 × 5 two-dimensional list-based array
'''
matrix = [[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]]
print(matrix[1], # with one index, get an entire row
matrix[1][1]) # with two indexes,get an item within the row
'''
True
6
123456SPAM! Spam ['Spam', 'SPAM!']
[6, 7, 8, 9, 10] 7
'''
list comprehensions are a way to build a new list by applying an expression to each item in a sequence (really, in any iterable), and are close relatives to for loops
the map built-in function does similar work, but applies a function to items in a sequence and collects all the results in a new list:
'''Changing Lists in Place'''
# ------- index and slice assignments ------- #
L = ['spam', 'Spam', 'SPAM!']
L[2] = 'eggs' # index assignment
L[0:2] = ['eat', 'more', 'and', 'more' ] # slice assignment: delete + insert
print(L) # the number of items inserted doesn’t have to match the number of items deleted
# slice assignment is a powerful operation
number = [1, 2, 3]
number[1:2] = [4, 5] # replace by overwriting
number[1:1] = [6, 7] # expand by inserting (replace nothing)
number[1:2] = [] # shrink by deleting (insert nothing)
a = [1]
a[:0] = [2, 3, 4] # insert at the front
a[len(a):] = [5, 6, 7] # insert at the end
# ------- list method calls ------- #
# https://docs.python.org/3/tutorial/datastructures.html#more-on-lists
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
fruits.append('grape') # list.append(x) equivalent to a[len(a):] = [x]
fruits.extend(['lemon', 'watermelon']) # list.extend(iterable) equivalent to a[len(a):] = iterable
lastFruit = fruits.pop() # list.pop([i]) i deault to -1
firstFruit = fruits.pop(0)
# ------- other common list operations ------- #
# del statement
food = ['spam', 'eggs', 'ham', 'toast']
del food[0] # delete one item
del food[1:] # delete an entire section, same as food[1:] = []
print(food)
# L[i:j] = []
mylist = ['Already', 'got', 'one']
mylist[1:] = []
mylist[0] = []
print(mylistt)
标签:in place close other dea struct members array bit art
原文地址:https://www.cnblogs.com/bit-happens/p/11884520.html