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

时间:2015-05-18 19:08:38

标签: 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:
        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

我的问题是我无法识别何时使用列表以及何时使用集合。在这个if语句中  &#34; if((len(candidate)==(k + 1))和((candidate&lt; items)== True)):&#34; 该计划永远不会进入。你知道我还没有理解的东西吗? 算法的伪代码是:

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

提前致谢!

1 个答案:

答案 0 :(得分:0)

集合对于测试成员资格是优越的(如果是集合中的x),并且集合必须包含可散列数据,并且不能/不会包含重复项(try set([1, 1, 3, 4]))。集合提供了许多集合论函数,例如交集。它们添加成员的速度较慢,没有订购,如果您没有充分的理由使用set(),那么使用list()通常是个好主意。我鼓励您阅读the official python documentation on set()