gpt4 book ai didi

python - 从字符串python中提取小时和分钟

转载 作者:行者123 更新时间:2023-11-28 19:54:18 25 4
gpt4 key购买 nike

我有一个格式为 '00:00' 的字符串显示时间。它可以是任何时间。我想将小时和分钟提取到单个变量中。

最佳答案

您可能希望小时分钟是整数:

hours, minutes = map(int, "00:00".split(':'))

这是如何运作的

  1. str.split(delim) 使用 delim 作为分隔符拆分 str。返回一个列表:"00:00".split(':') == ["00", "00"]
  2. map(function, data)function 应用于可迭代data 的每个成员。 map(int, ["00","00"]) 返回一个可迭代对象,其成员是整数。
  3. a, b, c = iterable 提取 iterable 的前 3 个值,并将它们分配给名为 ab< 的变量c

关于python - 从字符串python中提取小时和分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38555327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com