533 次浏览
要将周期转换为所需的频率,请使用 period.asfreq() 方法。假设我们将使用“H”说明符将其设置为所需的每小时频率。首先,导入所需的库 - import pandas as pd pandas.Period 表示一段时间。创建两个 Period 对象 period1 = pd.Period("2020-09-23 03:15:40") period2 = pd.Period(freq="D", year = 2021, month = 4, day = 16, hour = 2, minute = 35) 显示 Period 对象 print("Period1...", period1) print("Period2...", period2) 将 Period 转换为所需的频率。我们将频率设置为 H,即每小时频率 res1 = period1.asfreq('H') res2 = period2.asfreq('H') 示例 以下代码 import pandas as pd # pandas.Period 表示… 阅读更多
458 次浏览
要从 Period 对象中获取年份,请使用 period.year 属性。首先,导入所需的库 - import pandas as pd pandas.Period 表示一段时间。创建两个 Period 对象 period1 = pd.Period("2020-09-23") period2 = pd.Period(freq="D", year = 2021, month = 4, day = 16, hour = 2, minute = 35) 显示 Period 对象 print("Period1...", period1) print("Period2...", period2) 从两个 Period 对象中获取年份 res1 = period1.year res2 = period2.year 示例 以下代码 import pandas as pd # pandas.Period 表示一段时间 # 创建两个 Period 对象 period1 = pd.Period("2020-09-23") period2 = pd.Period(freq="D", year = 2021, month = ... 阅读更多
184 次浏览
要获取给定 Period 的一年中的周数,请使用 period.weekofyear 属性。首先,导入所需的库 - import pandas as pd pandas.Period 表示一段时间。创建两个 Period 对象 period1 = pd.Period("2020-09-23") period2 = pd.Period(freq="D", year = 2021, month = 4, day = 16, hour = 2, minute = 35) 显示 Period 对象 print("Period1...", period1) print("Period2...", period2) 从两个 Period 对象中获取一年中的周数 res1 = period1.weekofyear res2 = period2.weekofyear 示例 以下代码 import pandas as pd # pandas.Period 表示一段时间 # 创建两个 Period 对象 period1 = pd.Period("2020-09-23") period2 = ... 阅读更多
293 次浏览
要查找给定 Period 对象的起始时间,请使用 period.start_time 属性。首先,导入所需的库 - import pandas as pd pandas.Period 表示一段时间。创建两个 Period 对象 period1 = pd.Period("2020-09-22") period2 = pd.Period(freq="D", year = 2021, month = 2, day = 14, hour = 2, minute = 35) 显示 Period 对象 print("Period1...", period1) print("Period2...", period2) 从两个 Period 对象中获取起始时间 res1 = period1.start_time res2 = period2.start_time 示例 以下代码 import pandas as pd # pandas.Period 表示一段时间 # 创建两个 Period 对象 period1 = pd.Period("2020-09-22") period2 = pd.Period(freq="D", year = ... 阅读更多
172 次浏览
假设我们得到一个二叉树。我们必须找出其子树中是否存在二叉搜索树 (BST),并找出最大 BST 的总和。为了求和,我们将该 BST 中每个节点的值相加。我们将总和值作为输出返回。因此,如果输入如下所示,则输出将为 12。给定二叉树中的 BST 为 - 节点的总和 = 12。为了解决这个问题,我们将遵循以下步骤 - c := 0 m := null value := 0 定义一个函数 recurse()。这将采用节点 if ... 阅读更多
124 次浏览
假设我们得到一个二叉树。我们必须找出树中作为二叉搜索树 (BST) 的最大子树。我们返回 BST 的根节点。因此,如果输入如下所示,则输出将为 - 为了解决这个问题,我们将遵循以下步骤 - c := 0 m := null 定义一个函数 recurse()。这将采用节点 if 节点不为空,则 left_val := recurse(节点的左子节点) right_val := recurse(节点的右子节点) count := 负无穷 if (node.left 与 null 相同或 node.left.val
315 次浏览
假设我们得到一个包含多个整数的列表。我们必须找出数组中每对值的差值,并找出第 k 小的差值。索引从 0 开始,值 k 是作为输入给我们的。因此,如果输入类似于 numbers = {2, 6, 4, 8},k = 2,则输出将为 2。对之间的差值为 - (2, 6) = 4 (2, 4) = 2 (2, 8) = 6 (6, 4) = 2 (6, 8) = 2 (4, 8) = 4 如果我们对值进行排序,它将变为 2, 2, ... 阅读更多
191 次浏览
假设我们得到两个列表 p 和 q,它们包含一些整数。我们必须将这些列表的所有值相乘,并必须从乘法结果中找出第 k 大的值。因此,如果输入类似于 p = [2, 5],q = [6, 8],k = 2,则输出将为 16。乘法结果为:2 * 6 = 12,2 * 8 = 16,5 * 6 = 30,5 * 8 = 40。第 2 大元素位于(索引从 0 开始)是 16。为了解决这个问题,我们将遵循以下步骤… 阅读更多
433 次浏览
假设我们得到一个列表,其中包含成对的值 (m, c)。这些值表示一条线,其中 y = mx + c。我们还得到两个值 l 和 r。我们必须找出在 x = l 到 x = h 范围之间彼此相交的线的数量。因此,如果输入类似于 input_list = [[4, 6], [-6, 10], [8, 12]],l = 0,h = 2,则输出将为 2。如果我们查看给定的照片,则线 4x + 6 = 0 和 -6x + ... 阅读更多
250 次浏览
假设我们得到一个正整数。我们必须用文字拼写这个数字;例如,如果输入数字“56”,则输出将为“Fifty-Six”。转换范围最多为十亿。因此,如果输入类似于 input = 5678,则输出将为 Five Thousand Six Hundred Seventy-Eight。为了解决这个问题,我们将遵循以下步骤 - 定义一个数组“numbers”,其中包含以下对 - {{"Billion", 1000000000}, {"Million", 1000000}, {"Thousand", 1000}, {"Hundred", 100}, {"Ninety", 90}, {"Eighty", 80}, {"Seventy", 70}, {"Sixty", 60}, {"Fifty", 50}, {"Forty", 40}, {"Thirty", 30}, {"Twenty", 20}, {"Nineteen", 19}, ... 阅读更多