在python中正确使用列表和集合

在python中正确使用列表和集合,python,algorithm,Python,Algorithm,我试图用python编写一个先验算法,当算法必须检查k维项目集时,我遇到了一个问题。到目前为止,我已经编写了以下代码: def A_Priori_Algorithm_Next_Passes(file, freqk, k, s): input_file = open(file, 'r') csv_reader = csv.reader(input_file, delimiter=',') baskets = [] for row in csv_reader:

我试图用python编写一个先验算法,当算法必须检查k维项目集时,我遇到了一个问题。到目前为止,我已经编写了以下代码:

def A_Priori_Algorithm_Next_Passes(file, freqk, k, s):

    input_file = open(file, 'r')
    csv_reader = csv.reader(input_file, delimiter=',')

    baskets = []

    for row in csv_reader:
        unique_row_items = set([field.strip().lower() for field in row])
        baskets.append(unique_row_items)

    input_file.close()
    all_items = []
    counts = {}
    freq = {}
    length = len(baskets)
    i = 0

    while(i < length):
        items = GetUniqueItems(baskets[i])
        items_list = list(items)
        length_1 = len(items_list)
        itemset_pairs = GetPairs(freqk)
        u = 0
        while(u < len(itemset_pairs)):
            all_items.append(tuple(itemset_pairs[u]))
            u = u + 1
        candidates = []
        q = 0
        while(q < len(itemset_pairs)):
            a1 = itemset_pairs[q][0]
            a2 = itemset_pairs[q][1]
            #print(a1)
            #print(a2)
            #candidate_sum = a1 + ',' + a2
            candidate_set = set(a1).union(set(a2))
            candidate = []
            candidate.append(candidate_set)
            if(tuple(candidate) not in candidates):
                candidates.append(tuple(candidate))
                if((len(candidate) == (k + 1)) and ((candidate < items) == True)):
                    #print(candidate)
                    if(tuple(candidate) not in counts):
                        counts[tuple(candidate)] = 1
                    else:
                        counts[tuple(candidate)] = counts[tuple(candidate)] + 1
            q = q + 1
        i = i + 1      
    i = 0
    while(i < len(all_items)):
        if(all_items[i] in counts):
            if(counts[tuple(all_items[i])] >= s):
                freq[all_items[i]] = counts[all_items[i]]
        i = i + 1

    return freq

提前谢谢

集合在测试成员资格方面优于集合(如果集合中有x),集合必须包含可散列数据,并且不能/不会包含重复数据(请尝试
集合([1,1,3,4])
)。集合提供了许多集合论函数,例如交集。它们添加成员的速度较慢,而且没有顺序,如果没有充分的理由使用set(),通常最好使用list()。我鼓励你阅读。

(候选者候选者
你需要将其简化为可管理的内容。但是
candidate
是列表、集合还是数字
candidate
表示一个数字。candidate应具有候选项集。它可能是列表或设置谢谢您的帮助!我看了文件。我添加了伪代码,我必须制作一个python程序。
Algorithm: A-Priori algorithm (k + 1) pass.

Input: F, a file containing baskets

Input: freqk, a table containg the frequencies of itemsets of size k in          baskets above the threshold s

Input: k, the size of the itemsets in freqk

Input: s, the support

Output: freq, a table containg the frequencies of itemsets of size k + 1 with threshold s

1 counts ← ∅

2 freq ← ∅

3 foreach basket in F do

4 items ← GetUniqueItems(basket)

5 itemset_pairs = GetPairs(freqk)

6 candidates ← ∅

7 foreach pair in itemset_pairs do

8 (fp,sp) ← pair

9 candidate ← fp ∪ sp

10 if not candidate in candidates then

11 Add(candidates, candidate)

12 if |candidate| = k + 1 and candidate ⊆ items then

13 counts[candidate] ← counts[candidate] + 1

14 foreach itemset, count in counts do

15 if count ≥ s then

16 freq[itemset] = count

17 return freq