Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:如何在while循环中跟踪两个或多个相等的变量_Python_Python 3.x - Fatal编程技术网

Python:如何在while循环中跟踪两个或多个相等的变量

Python:如何在while循环中跟踪两个或多个相等的变量,python,python-3.x,Python,Python 3.x,我正在使用python使用以下代码计算.txt文件行中紧跟在名字后面的名字数量,以确定谁的名字最多: lines=0 wordCount=0 mostWordsInLine = 0 follows = open("follows.txt", "r") for word in follows: lines += 1 f1=word.split() wordCount=wordCount+len(f1) if len(f1) > mostWordsInLin

我正在使用python使用以下代码计算.txt文件行中紧跟在名字后面的名字数量,以确定谁的名字最多:

lines=0
wordCount=0
mostWordsInLine = 0
follows = open("follows.txt", "r")


for word in follows:
    lines += 1
    f1=word.split()
    wordCount=wordCount+len(f1)
    if len(f1) > mostWordsInLine:
        mostWordsInLine = len(f1)
        mostWordsInLine = word[: word.find(' ')]

print ("Most social user: " + str(mostWordsInLine))
.txt文件如下所示:

andrew fred
fred
judy andrew fred
george judy andrew
john george
我得到的结果是:

Most social user: andrew

我的问题是,我的代码应该返回judy和george,但由于某种原因返回andrew。如何修复此问题?

字符串库具有.count'name'函数。因此,您可以计算for循环中的每个元素,并对它们进行比较。你需要澄清这个问题,因为andrew和fred发生了三次

wordCount=0
mostWordsInLine = 0
follows = open("follows.txt", "r")
mostFollows = []


for word in follows:
    f1=word.split()
    wordCount += len(f1)
    if len(f1) > mostWordsInLine:
        mostWordsInLine = len(f1)
        mostFollows = [f1[0]]
    elif len(f1) == mostWordsInLine:
        mostFollows.append(f1[0])


print ("Most social user: " + ''.join(mostFollows))

有道理…首先要有一个唯一名称的列表[名称]。然后用字典存储它们。{name:[列出名称]然后是lenstring。编写一个函数来迭代字典并检索说话人最多的字典。

不是直接回答家庭作业,而是一种可以用来解决这个问题和将来可能遇到的问题的技术:在代码中添加print语句以了解发生了什么。例如,如果我们添加2个print stateme新界南总区如下:

for word in follows:
    lines += 1
    f1=word.split()
    wordCount=wordCount+len(f1)
    print("f1 = {}, mostWordsInLine = {}".format(f1, mostWordsInLine))
    if len(f1) > mostWordsInLine:
        mostWordsInLine = len(f1)
        mostWordsInLine = word[: word.find(' ')]
    print("    after comparison: mostWordsInLine = {}".format(mostWordsInLine))
它将打印以下输出:

f1 = ['andrew', 'fred'], mostWordsInLine = 0
    after comparison: mostWordsInLine = andrew
f1 = ['fred'], mostWordsInLine = andrew
    after comparison: mostWordsInLine = andrew
f1 = ['judy', 'andrew', 'fred'], mostWordsInLine = andrew
    after comparison: mostWordsInLine = andrew
f1 = ['george', 'judy', 'andrew'], mostWordsInLine = andrew
    after comparison: mostWordsInLine = andrew
f1 = ['john', 'george'], mostWordsInLine = andrew
    after comparison: mostWordsInLine = andrew
f1 = [], mostWordsInLine = andrew
    after comparison: mostWordsInLine = andrew
Most social user: andrew

第一次其中一个输出行没有意义时,请查看您是否能找出原因。

您正在为mostWordsInLine分配两个不同的值。第二个是字符串,与循环下一次迭代时的数字相比没有意义。您需要两个不同的变量,一个用于计数,一个用于名称。您能证实你的问题?不清楚。提示:使用readline方法在lines@kraymer在file对象上循环也很好。您标记了问题Python 3…它实际上是Python 2吗?这是该代码实际生成您声称的输出的唯一方式。这有点令人困惑,但谁的名字后面最多,所以我认为它的名字[跟随者]…所以你看谁拥有最多的跟随者是有意义的…首先要有一个唯一名字[名字]的列表。然后用字典来存储它们。{name:[列出名字]然后是lenstring。编写一个函数,迭代字典并检索说话人最多的字典。您使用Python 2吗?将int与str进行比较会在Python 3中引发一个异常。是的;这是Python 2。我假设OP也使用Python 2,因为它对他也起作用。虽然注意到Python 3标记…hmmClose,但您e无法正确处理大多数下面的内容。只有当长度等于最大值时,才需要附加到它,如果最大长度增加,则需要清除它。感谢您的帮助!不幸的是,我尝试运行了您的代码,但我得到了george和john,而不是judy和george,您知道为什么这仍然不起作用吗?