简体   繁体   English

如何使用 python 将两位数分隔为两位数? 例如 15 -> 1, 5

[英]How can I separate a double digit to two single digits with python? e.g. 15 -> 1, 5

def plus_cycles():
  n = int(input("Write here the number that you want. (1~99)\n"))
  while True:
    if n < 10:
      a = "n" + "0"

In this code, I want a to be separated with two single digits.在这段代码中,我希望 a 用两个个位数分隔。 eg 15 -> 1, 5 or [1, 5]例如 15 -> 1, 5 或 [1, 5]

The divmod() method takes two numbers and returns a pair of numbers (a tuple) consisting of their quotient and remainder. divmod()方法接受两个数字并返回一对数字(一个元组),由它们的商和余数组成。 For integers, the result is the same as (a // b, a % b) .对于整数,结果与(a // b, a % b)相同。

>>>> divmod(15, 10)
(1, 5)
>>> 15 // 10
1
>>> 15 % 10
5

You Could do something like this:你可以这样做:

>>> intToList = lambda number : [int(i) for i in list(str(number))]
>>> intToList(15)
[1, 5]

list(str(15)) Will return a List of Chars like: list(str(15))将返回一个字符列表,例如:

>>> list(str(15))
['1', '5']

So we iterate through them with a for loop to turn them back ino integers.所以我们用一个 for 循环遍历它们,把它们变成整数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (Python)如何将具有日期和时间的列(例如UTC 2019-07-02 00:12:32)拆分为两个单独的列? - (Python) How can I split a column with both date and time (e.g. 2019-07-02 00:12:32 UTC) into two separate columns? 如何重新排列一个三位数的整数(例如 978 到这个 987)并得到最大的整数? - How to rearrange the digits of a three-digit integer (e.g. 978 into this 987) and get the highest integer? 在python中,如何将一位数转换为两位数字符串? - in python how do I convert a single digit number into a double digits string? Python:如何将时间记录(例如2015-02-01 12:34:22)分成两个不同的数组 - Python: how to separate time records(e.g. 2015-02-01 12:34:22) in to two different arrays 如何将一个分支的提交(例如 rebase)添加到另外两个主要分支上? - How can I add the commits of a branch (e.g. rebase) onto two other primary branches? 如何判断我是否已在 VS 代码中成功导入了 Python 包(例如 Pandas)? - How can I tell if I have successfully imported a package for python (e.g. Pandas) in VS code? 我如何在python 2.7中将一位数字输出为两位数 - How do i output a single digit as a double digit in python 2.7 如何从单独的进程(例如,编辑器、vim)在 Jupyter Notebook 服务器中创建和执行单元? - How can I create and execute a cell in Jupyter Notebook server from a separate process (e.g., an editor, vim)? 如何将数据结构(例如 dict)从单独的文件导入或包含到 Python 文件中 - How to import or include data structures (e.g. a dict) into a Python file from a separate file 如何将字符串(例如'A')引用到更大列表的索引(例如['A','B','C','D',...])? - How can I reference a string (e.g. 'A') to the index of a larger list (e.g. ['A', 'B', 'C', 'D', ...])?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM