python 如何将列表中的数字转换为数字[关闭]

3okqufwl  于 2024-01-05  发布在  Python
关注(0)|答案(2)|浏览(125)

已关闭。此问题需要更多focused。目前不接受回答。
**要改进此问题吗?**更新此问题,使其仅针对editing this post的一个问题。

2天前关闭。
社区正在审查是否重新开放这个问题,截至2天前。
Improve this question
有人能帮助我解决这个问题吗?我试图寻找解决方案,但找不到
所以我想把python上列表中的数字转换成数字

  1. [2, 1, 3, 0] -> 2130

字符串
我怎么能这么做?
我试图以某种方式改变它,但我就是不能解决这个问题,并在这里搜索找到它,但找不到答案

wmtdaxz3

wmtdaxz31#

  1. def list_to_int(numbers):
  2. """
  3. Converts a list of numbers into a single integer.
  4. Args:
  5. numbers: A list of integers.
  6. Returns:
  7. The integer formed by concatenating the digits of the numbers in the list.
  8. """
  9. # Convert each number to a string and concatenate them.
  10. digits_string = "".join(str(n) for n in numbers)
  11. # Convert the string to an integer.
  12. return int(digits_string)

个字符
此函数首先遍历数字列表并将每个数字转换为字符串。然后,它将所有字符串连接在一起以形成单个字符串。最后,它将字符串转换为整数。

展开查看全部
slhcrj9b

slhcrj9b2#

将每一项乘以10的幂,然后求和

  1. x = [2, 1, 3, 0]
  2. y = sum(num * 10 ** i for i, num in enumerate(reversed(x)))
  3. print(x, "==>", y)

个字符

相关问题