首页 > 编程语言 >【Python/Numpy】list/tuple/dictionary/numpy 的操作

【Python/Numpy】list/tuple/dictionary/numpy 的操作

时间:2024-03-17 21:35:33浏览次数:16  
标签:random dictionary tuple Python nums list np shape print

Common Data Structures

Lists

Lists are mutable arrays.

普通操作

# Two ways to create an empty list
empty_list = []
empty_list = list()

# Create a list that contains different data types,this is allowed in Python
mylist = ["aa", "bb", 1, 2, ["Jack", 12]]

# Index into list by index
print(mylist[0]) # "aa"

# Append to end of list
mylist.append("append")

# Get length of list
len(mylist)

# Concatenate two lists
mylist += ["concatenate", "two"]

切片

List slicing is a useful way to access a slice of elements in a list.

nums = [0, 1, 2, 3, 4, 5, 6]

# Slices from start index (inclusive) to end index (exclusive)
print(nums[0:3]) # [0, 1, 2]

# When start index is not specified, it is start of list
# When end index is not specified, it is end of list
print(nums[:3]) # [0, 1, 2]
print(nums[5:]) # [5, 6]

# : takes the slice of all elements along a dimension, is very useful when working with numpy arrays
print(nums[:]) # [0, 1, 2, 3, 4, 5, 6]

# Negative index wraps around, start counting from the end of list
print(nums[-1]) # 6
print(nums[-3:]) # [4, 5, 6]
print(nums[3:-2]) # [3, 4]

注意:如果使用赋值操作将 nums 赋值给另一个变量,那么修改新变量的值会影响原始列表 nums 的值。例如,如果执行新列表变量 new_nums = nums,那么在对 new_nums 进行修改后,nums 的值也会被修改。但如果使用 nums[:] 进行切片操作赋值给新变量,则对新变量进行的任何修改都不会影响原始列表 nums 值。

Tuples

Tuples are immutable arrays. Unlike lists, tuples do not support item re-assignment

# Two ways to create an empty tuple
empty_tuple = ()
empty_tuple = tuple()

# Use parentheses for tuples, square brackets for lists
names = ("Zach", "Jay")

# Index
print(names[0])

# Get length
len(names)

# Create a tuple with a single item, the comma is important
single = (10,)
print(single) # (10,)

Dictionary

Dictionaries are hash maps.

# Two ways to create an empty dictionary
phonebook = {}
phonebook = dict()

# Create dictionary with one item
phonebook = {"Zach": "12-37"}
# Add anther item
phonebook["Jay"] = "34-23"

# Check if a key is in the dictionary
print("Zach" in phonebook) # True
print("Kevin" in phonebook) # False

# Get corresponding value for a key
print(phonebook["Jay"]) # 34-23

# Delete an item
del phonebook["Zach"]
print(phonebook) # {'Jay': '34-23'}

Loops

# Basic for loop
for i in range(5):
    print(i)
    
# To iterate over a list
names = ["Zach", "Jay", "Richard"]
for name in names:
    print(name)

# To iterate over indices and values in a list
# Way 1
for i in range(len(names)):
    print(i, names[i])
# Way 2
for i, name in enumerate(names):
    print(i, name)
    
#########################################

# To iterate over a dictionary
phonebook = {"Zach": "12-37", "Jay": "34-23"}

# Iterate over keys
for name in phonebook:
    print(name)

# Iterate over values
for number in phonebook.values():
    print(number)
    
# Iterate over keys and values
for name, number in phonebook.items():
    print(name, number)

Numpy

Optimized library for matrix and vector computation.

Numpy is a Python library, which adds support for large, multi-dimensional arrays and matrices, along with a large collection of optimized, high-level mathematical of functions to operate on these arrays.

Vectors can be represented as 1-D arrays of shape (N,) or 2-D arrays of shape (N,1) or (1,N). But it's important to note that the shapes (N,), (N,1), and (1,N) are not the same and may result in different behavior (we'll see some examples below involving matrix multiplication and broadcasting).

Matrices are generally represented as 2-D arrays of shape (M,N).

# Import numpy
import numpy as np

# Create numpy arrays from lists
x = np.array([1, 2, 3])
y = np.array([[3, 4, 5]])
z = np.array([[6, 7], [8, 9]])

# Get shapes
print(y.shape) # (1, 3)

# reshape
a = np.arange(10) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = a.reshape((5, 2))
'''
[[0 1]
 [2 3]
 [4 5]
 [6 7]
 [8 9]]
'''

Array Operations

There are many Numpy operations that can be used to reduce a numpy array along an axis.

x = np.array([[1,2], [3,4], [5,6]])

# np.max operation
print(np.max(x, axis = 1)) # [2 4 6]
print(np.max(x, axis = 1).shape) # (3,)

print(np.max(x, axis = 1, keepdims = True))
'''
[[2]
 [4]
 [6]]
'''
print(np.max(x, axis = 1, keepdims = True).shape) # (3, 1)

#######################################################
# some matrix operations

# take an element-wise product(Hadamard product)
# A.shape must equal B.shape
A = np.array([[1, 2], [3, 4]])
B = np.array([[3, 3], [3, 3]])
print(A * B) 
'''
[[3 6]
 [9 12]]
'''

# do matrix multiplication with np.matmul or @
# the last dimension of A must equal the first dimension of B
print(np.matmul(A, B))
print(A @ B)
'''
[[9 9]
 [21 21]]
'''

# dot product or a matrix vector product with np.dot
u = np.array([1, 2, 3])
v = np.array([1, 10, 100])

print(np.dot(u, v)) # 321
print(u.dot(v)) # 321

# Taking the dot product of a vector and a multidimensional matrix is actually doing matrix multiplication
W = np.array([1, 2], [3, 4], [5, 6])
print(np.dot(v, W)) # [531 642]
print(np.dot(W, v)) # ValueError: shapes (3,2) and (3,) not aligned:2 (dim 1) != 3 (dim 0)
# We can fix the above issue by transposing W.
print(np.dot(W.T, v))

Slicing

Slicing / indexing numpy arrays is a extension of Python concept of slicing(lists) to N dimensions.

x = np.random.random((3, 4))

# Selects all of x
print(x[:])
'''
[[0.51640626 0.3041091  0.27188644 0.87484083]
 [0.79114758 0.99308623 0.98326875 0.04455941]
 [0.39529208 0.54231156 0.15966311 0.63360179]]
'''

# Selects the 0th and 2nd rows
print(x[np,array([0, 2]), :])
'''
[[0.51640626 0.3041091  0.27188644 0.87484083]
 [0.39529208 0.54231156 0.15966311 0.63360179]]
'''

# Selects 1st row as 1-D vector and and 1st through 2nd elements
print(x[1, 1:3])
# [0.99308623 0.98326875]

# Boolean indexing
print(x[x > 0.5])
# [0.51640626 0.87484083 0.79114758 0.99308623 0.98326875 0.54231156 0.63360179]

# 3-D vector of shape (3, 4, 1)
print(x[:, :, np.newaxis])
'''
[[[0.51640626] 
  [0.3041091]
  [0.27188644]
  [0.87484083]]
  
 [[0.79114758]
  [0.99308623]
  [0.98326875]
  [0.04455941]]
  
 [[0.39529208]
  [0.54231156]
  [0.15966311]
  [0.63360179]]]
'''

Broadcasting

The term broadcasting describes how Numpy treats arrays with different shapes during arithmentic operations.

General Broadcasting Rules

When operating on two arrays, Numpy compares their shapes element-wise(逐元素的).It starts with the trailing (i.e. rightmost) dimensions and works its way left. Two dimensions are compatible when:

  • they are equal, or
  • one of them is 1 (in which case, elements on the axis are repeated along the dimension)
image-20240317204043591
x = np.random.random((3, 4))
y = np.random.random((3, 1))
z = np.random.random((1, 4))

# In this example, y and z are broadcasted to match the shape of x.
# y is broadcasted along dim 1.
s = x + y
# z is broadcasted along dim 0.
p = x * z

# more example
a = np.zeros((3, 3))
b = np.array([[1, 2, 3]])
print(a+b)
'''
[[1. 2. 3.]
 [1. 2. 3.]
 [1. 2. 3.]]
'''

# more complex example
a = np.random.random((3, 4))
b = np.random.random((3, 1))
c = np.random.random((3, ))

result1 = b + b.T
print(b.shape) # (3, 1)
print(b.T.shape) # (1, 3)
print(result1.shape) # (3, 3)

result2 = a + c # ValueError: operands could not be broacast together whih shapes (3, 4) (3,)

result3 = b + c
print(b)
print(c)
print(result3)
'''
[[0.14781386]
 [0.89302824]
 [0.28916391]]

[0.96525397 0.86351595 0.29259715]

[[1.11306782 1.01132981 0.44041101]
 [1.8582822  1.75654419 1.18562539]
 [1.25441788 1.15267986 0.58176106]]
'''

Efficient Numpy Code

When working with numpy, avoid explicit for-loops over indices/axes at costs. For-loops will dramatically slow down your code.

We can time code uising the %%timeit magic. Let's compare using explicit for-loop vs. using numpy operations.

%%timeit
x = np.random.ran(1000, 1000)
for i in range(100, 1000):
    for j in range(x.shape[1]):
        x[i, j] += 5

\(459 ms\underline+10.5ms \text{ per loop (mean} \underline+\text{ std. dev. of 7 runs, 1 loops each)}\)

%%timeit
x = np.random.rand(1000, 1000)
x[np.arange(100, 1000), :] += 5

\(12.2 ms\underline+143\mu s \text{ per loop (mean} \underline+\text{ std. dev. of 7 runs, 100 loops each)}\)

标签:random,dictionary,tuple,Python,nums,list,np,shape,print
From: https://www.cnblogs.com/hzyuan/p/18079223

相关文章

  • python动态加载指定的模块
    importimportlibimportsysimportosimportpkgutilimportreimportinspectclassTestInstance:#初始化方法,当创建TestInstance对象时调用def__init__(self,projectName):#初始化实例变量projectName,存储项目名称self.projectName=......
  • 学了 Python 但又感觉没学 Python 不如重学 Python - day2(基础内置函数与变量引用的详
    目录1、int函数2、bin、oct、hex 函数3、type函数4、complex函数5、布尔运算6、chr与ord函数7、max与min函数8、eval函数9、变量对象引用10、对象的垃圾回收11、变量命名规则12、序列赋值13、增强赋值1、int函数按n进制将整数字符串转换为......
  • 【10】Python3之使用openpyxl,操纵表格
    使用openpyxl,读取Excel文件fromopenpyxlimportload_workbook#加载工作簿,后面写Excel的路径wb=load_workbook(r"C:\Users\以权天下\Desktop\月光.xlsx")#选择活动工作表或特定工作表wb.activesheet=wb['2024']#2024是表名Excel_data=sheet['A2'].value#A2是单元格......
  • [Python初阶]2255.统计是给定字符串前缀的字符串数目
    目录     2255.统计是给定字符串前缀的字符串数目①.题目②.问题分析③.startswith()方法理解与说明Ⅰ.定义和用法 Ⅱ.语法 ④.问题解决⑤总结     2255.统计是给定字符串前缀的字符串数目①.题目②.问题分析需求:统计列表words中,是字......
  • 网络安全快速入门(四) python基础
    4.1初识python我们在前面已经了解了批处理和控制台命令,但这类语言输入显得过于复杂,并且需要注意的事项有很多。那么问题来了,有没有什么操作简单,门槛较低,容易学习操作的计算机语言呢?今天他来了,python!百度是这么说的:简单,易学,速度快等等一系列优点,今天我们就来了解一下python语......
  • 20240317python学习
    20240317python学习      先听课,之后不会的百度。 ......
  • 机器人路径规划:基于迪杰斯特拉算法(Dijkstra)的机器人路径规划(提供Python代码)
    迪杰斯特拉算法(Dijkstra)是由荷兰计算机科学家狄克斯特拉于1959年提出的,因此又叫狄克斯特拉算法。是从一个顶点到其余各顶点的最短路径算法,解决的是有权图中最短路径问题。迪杰斯特拉算法主要特点是从起始点开始,采用贪心算法的策略,每次遍历到始点距离最近且未访问过的顶点的邻......
  • python中类的__new__方法和__init__方法
    python文章目录python一、python中类的__new__方法和__init__方法二、第三行解释说明instance=super().__new__(cls)三、__init__,__new__返回的是什么?四、debug代码运行中cls,instance,self都是什么东西怎么理解cls是<class'__main__.MyClass'>,instance是<__main__.......
  • Python进行金融特征的估计和分类,及如何构建深度RNN
    8.5金融特征RNN的应用不仅限于原始价格或收益率数据,还可以包括附加特征以改进它的预测性能。以下Python代码向数据集中添加了典型的金融特征。In[77]:data=generate_data()In[78]:data['r']=np.log(data/data.shift(1))In[79]:window=20data['mom']=......
  • 自学Python day 5
    今天是周日,时间比较多,所以今天的内容很多首先了解了什么是比较运算符以及如何使用result=10>5print(f"10>5的结果是:{result},类型是:{type(result)}")result="it"=="itt"print(f"字符串it和itt是否相等,结果是:{result},类型是:{type(result)}")#比较运算符的......