Python-将整数拆分为单个数字(位数不确定) [英] Python - Split integer into individual digits (undetermined amount of digits)

查看:174
本文介绍了Python-将整数拆分为单个数字(位数不确定)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在从文件中读取包含整数的行.读取的行数不确定.我们必须将每个整数拆分为整数,然后将这些数字相加,然后创建另一个文件,该文件同时写入整数和每个整数的数字总和.

We are reading lines from a file which contain integers. There are an indeterminate amount of lines being read. We must split each integer into the digits of the integer and then add the digits together and create another file which writes in both the integer and the sum of the digits for each integer.

这位教授说使用事件控制循环,但没有指定其他功能.我们只允许在 while 循环中使用,而不在 for 循环中使用.

The professor said to use an event-controlled loop, but didn't specify beyond that. We are only permitted to use while loops, not for loops.

我不知道如何将代码放入此处,以便可以显示到目前为止已测试的内容,以使他们重新认识一定数量的整数.

I can't figure out how to put code in here so that I can show what I have tested so far just to get reacquainted with the splitting of a certain amount of digits in an integer.

修改以添加:

myFile = open("cpFileIOhw_digitSum.txt", "r")

numbers = 1
numberOfLines = 3
digitList = []
while(numbers <= numberOfLines):
    firstNum = int(myFile.readline())
    while (firstNum != 0):
        lastDigit = firstNum % 10
        digitList.append(lastDigit)
        firstNum = firstNum / 10
        firstDigit = firstNum
    digitList.append(firstDigit)
    digitSum = sum(digitList)
    print digitList
    print digitSum
    numbers += 1


myFile.close()

^到目前为止,这里是我所拥有的,但是现在我的问题是我需要将每个整数的数字存储在不同的列表中.而且这是从文件中读取的不确定数量的整数.用于计数和结束循环的数字只是示例.

^Here is what I have so far but now my problem is I need each integer's digits stored in a different list. And this is an undetermined amount of integers being read from the file. The numbers used for the count and to end the loop are simply examples.

代码的最新更新:现在我几乎需要知道的几乎是如何让 while 循环知道txt文件中没有剩余的行了.

LASTEST UPDATE to my code: Pretty much all I need to know now is how to let the while loop know that there are no more lines left in the txt file.

myFile = open("cpFileIOhw_digitSum.txt", "r")
myNewFile = open("cpFileIOhw_output.txt", "w")

total = 0
fullInteger =
while(fullInteger != 0):
    fullInteger = int(myFile.readline())
    firstNum = fullInteger
    while (firstNum != 0):
        lastDigit = firstNum % 10
        total = total + lastDigit
        firstNum = firstNum / 10
        firstDigit = firstNum
    total = total + firstDigit
    myNewFile.write(str(fullInteger) + "-" + str(total))
    print " " + str(fullInteger) + "-" + str(total)
    total = 0


myFile.close()
myNewFile.close()

推荐答案

不需要将整数强制转换为字符串.由于您是从文本文件中读取整数,因此将从字符串开始.

Casting the integers to strings shouldn't be necessary. Since you're reading the integers in from a text file, you'll start with strings.

您将需要一个外部 while 循环来处理从文件中读取行.由于禁止使用进行循环,因此我将使用

You'll need an outer while loop to handle reading the lines from the file. Since you're forbidden to use for loops, I would use my_file.readline(), and you'll know you're done reading the file when it returns an empty string.

嵌套在该循环中,您将需要一个来处理将数字分开的操作.虽然-您的教授是否需要两个循环?我以为您的问题是在编辑之前就说过的,但现在没有.如果不需要,我将使用列表理解.

Nested inside that loop, you'll need one to handle pulling apart the digits. Although-- did your professor require two loops? I thought that your question said that before it was edited, but now it doesn't. If it's not required, I would just use a list comprehension.

这篇关于Python-将整数拆分为单个数字(位数不确定)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆