Search code examples
pythonbinary-search-tree

How to resolve the Error: zip argument #2 must support iteration in Python


I am new to python. I have a function that returns an array. I want to print it. This is my code.

import importlib
from datetime import datetime
from BST import BinarySearchTree

bst = BinarySearchTree()


def bst_build():
#bst_list = importlib.import_module("BST").BinarySearchTree()
bst11 = importlib.import_module("BST").BinarySearchTree()
bst12 = importlib.import_module("BST").BinarySearchTree()
bst13 = importlib.import_module("BST").BinarySearchTree()
bst21 = importlib.import_module("BST").BinarySearchTree()
bst22 = importlib.import_module("BST").BinarySearchTree()
bst23 = importlib.import_module("BST").BinarySearchTree()

bst_list = [bst11, bst12, bst13, bst21, bst22, bst23]
return bst_list

def bst_insert():
    text_files = ["insert_set1_data_1.txt",
                  "insert_set1_data_2.txt",
                  "insert_set1_data_3.txt",
                  "insert_set2_data_1.txt",
                  "insert_set2_data_2.txt",
                  "insert_set2_data_3.txt"]
    bst_list = bst_build()
    time = []

    for file, bst in zip(text_files, bst_list):

        data = open(file, 'r')
        Lines = data.readlines()
        start = datetime.now()
        for line in Lines:
            val = int(line.strip())
            bst.insert(val)
        end = datetime.now()
        duration = end - start
        time.append(duration.microseconds)

    return time

To print the output I used the following method.

res= bst_insert()
print("Time is".format(res))

But it gave me an error, 'zip argument #2 must support iteration'. How can I resolve this error?


Solution

  • As far as I can tell you don't want to make bst iterable, you just want to append into the same object.

    For example, something like:

    from time import perf_counter
    from BST import BinarySearchTree
    
    def bst_insert_files(filenames):
        bst = BinarySearchTree()
        time = []
    
        for filename in filenames:
            with open(filename) as fd:
                lines = fd.readlines()
            start = perf_counter()
            for line in lines:
                val = int(line.strip())
                bst.insert(val)
            dt = perf_counter() - start
            time.append(dt * 1e6)
    
        return bst, time
    
    text_files = [
        "insert_set1_data_1.txt",
        "insert_set1_data_2.txt",
        "insert_set1_data_3.txt",
        "insert_set2_data_1.txt",
        "insert_set2_data_2.txt",
        "insert_set2_data_3.txt",
    ]
    
    bst, time = bst_insert_files(text_files)
    print(time)
    

    I've cleaned up a few things: your importing of BST looked weird (you don't need to import and use importlib); using perf_counter for timing is better for benchmarking than using datetimes (if the OS/user adjusts the clock datetime will give incorrect results, but perf_counter should give the correct duration). I'd also suggest using context managers around files so they can clean up appropriately. Finally, passing in the list of files as an argument will allow you to test on a smaller set of files, or extend the list without changing the function.