【问题标题】:Loop and/or conditional statements produce different values every time it is ran循环和/或条件语句每次运行时都会产生不同的值
【发布时间】:2021-12-22 15:01:13
【问题描述】:

我有这个循环,它从文件夹中读取文件,然后将其值放入列表中。然后将该列表的内容与另一个列表进行比较,其中将根据另一个列表中内容的存在来完成操作。我的问题现在存在于正在进行的操作中。这些操作有时有效,但大多数时候,结果都是随机的。它执行的一些 if 和 elif 语句比所需的要多,所以我认为它与下面的条件语句有关。然而,到目前为止,我无法弄清楚为什么会发生这种情况。

pair = {}
bag = []
new = []
os.chdir(classPath)              
for file in os.listdir():
    prod = Decimal(1)
    bag2 = []
    if file.endswith(".txt"):
        file_path = f"{classPath}\{file}"
        input = readFile(file_path)
        for i in list(set(input)):
            bag2.append(re.sub(r'[^a-zA-Z0-9]', '', i))
            for j in list(set(bag2)):
                count1 = Decimal(0)
                op = Decimal(0)
                if j in bag:
                    count1 = Decimal(count1 + 1)
                    op = Decimal(count1 + 2) / ((len(bag)) + (2 * (len(list(set(dict()))) + len(new))))
                    prod = Decimal(prod * op)
                elif j not in bag:
                    op = Decimal(count1 + 2) / ((len(bag)) + (2 * (len(list(set(dict()))) + len(new))))
                    print('hello')
                    prod = Decimal(prod * op)
            pair[file] = prod

例如文件包含单词hello hi 列表 2 包含 yes no。由于 hello 和 hi 不存在于其他列表中,因此 elif 语句必须执行两次。

def spamCount():
bagInput = []
os.chdir(spamPath)  
for file in os.listdir():
    if file.endswith(".txt"):
        file_path = f"{spamPath}\{file}"
        inputs = readFile(file_path)
        bagInput.append(inputs)

return list(chain(*bagInput))

包列表取自该函数

a = spamCount()
    for i in (a):
    bag.append(re.sub(r'[^a-zA-Z0-9]', '', i))

【问题讨论】:

  • 这是什么:(len(list(set(dict())))?
  • 啊抱歉,忘记提了。它只是另一个函数的列表的长度。它的值为 7。
  • 等等,len(list(set(dict())))总是 0。
  • 好吧,除非你覆盖了内置的字典,否则 dict() 会给你一个空字典,它会给出一个空集,一个空列表,它的长度将为 0
  • 你有一个条件(如果 j 在袋子里(做某事),否则如果 j 不在袋子里(做其他事情)。可能发生的是当它进入 else 部分时,数据可能有两个条件。数据不在包中,也不在包中。如果数据为空,则可能发生这种情况。您可以尝试(如果 j 不在包中,ELSE 会做一些事情)

标签: python loops conditional-statements


【解决方案1】:
for file in os.listdir():
    prod = Decimal(1)
    bag2 = []
    if file.endswith(".txt"):
        file_path = f"{classPath}\{file}"
        inputs = readFile(file_path)
        for i in list(set(inputs)):
            bag2.append(re.sub(r'[^a-zA-Z0-9]', '', i))
        for j in list(set(bag2)):
            print(j)
            count1 = Decimal(0)
            op = Decimal(0)
            if j not in bag:
                op = Decimal(count1 + k) / ((len(bag)) + (k * (len(list(set(dictS()))) + len(new))))
                prod = Decimal(prod * op)
            else:
                count1 = Decimal(count1 + 1)
                op = Decimal(count1 + k) / ((len(bag)) + (k * (len(list(set(dictS()))) + len(new))))
                prod = Decimal(prod * op)
        pair[file] = prod

事实证明,这个 sn-p 中的第三个 for 循环会遍历列表,即使它还没有完成。在 bag2.append 完成后完成循环是我所做的修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 2012-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    相关资源
    最近更新 更多