-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroups.py
More file actions
50 lines (44 loc) · 1.78 KB
/
groups.py
File metadata and controls
50 lines (44 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import csv
try:
with open("data.csv", "r") as csvfile:
reader = csv.reader(csvfile)
line_count = 0
sets = dict()
all_rows = []
for csvrow in reader:
all_rows.append(csvrow)
for row in all_rows:
if line_count != 0: # Ignore header
set1 = set()
if len(row) > 2:
for e in row[2:]:
set1.add(e)
compare_count = line_count + 1 # we want to compare each row only once and not with itself
for comparing_row in all_rows[compare_count:]:
if compare_count > line_count:
set2 = set()
if len(comparing_row) > 2:
for e in comparing_row[2:]:
set2.add(e)
set1 = frozenset(set1)
set2 = frozenset(set2)
# print(row[2:], comparing_row[2:])
# print(set1, set2)
intersection = set1 & set2
if len(intersection) != 0: # don't add empty intersections
if intersection in sets:
sets[intersection] += 1
else:
sets[intersection] = 1
compare_count += 1
line_count += 1
except IOError:
print("data.csv file not found")
try:
with open("groups.csv", "w") as groupsfile:
writer = csv.writer(groupsfile)
writer.writerow(["Occurrence of Group", "Groupflags"])
for set, occurrence in sets.items():
writer.writerow([occurrence, *set])
except IOError:
print("Could not write file groups.csv")