东北冻耳朵

.csv

Bob	Carol	Leanne	Mark	Paul	Will
Carol	Leanne	Mark			
Farley	Paul				
Leanne	Sarai				
Larry	Carol	Leanne	Mark	Will	
Mark	Philip	Zach			
Paul	Zach				
Will	Leanne	Mark			
Zach	Philip				

Code:

### utf-8 ###
import csv
touch_figure = []
temp_touch = []
f = open(r"./DataSet1.txt", "w")
touch_file = open('touch.csv', 'r')
reader = csv.reader(touch_file)

for i in reader:
    temp_touch = []
    for j in range(len(i)):
        if (i[j] != ""):
            temp_touch.append(i[j])
    touch_figure.append(temp_touch)
    for j in range(len(i)):
        if j==0:
            f.write("{} had contact with".format(i[j]))
        else:
            if j+2<len(i) and i[j]!="":
                if i[j+2]!="":
                    f.write(" {},".format(i[j]))
                elif i[j+1]=="":
                    f.write(" {}.".format(i[j]))
                    break
                else:
                    f.write(" {} and {}.".format(i[j],i[j+1]))
                    break
            if j+2==len(i) and i[j+1]!="":
                f.write(" {} and {}.".format(i[j], i[j + 1]))
                break
    f.write("\n")
touch_file.close()

touch_tree = {}
for k in touch_figure:
    for j in k:
        touch_tree[j] = 0
touch_head = {}
for k in touch_figure:
    touch_head[k[0]] = 0
touch_num = {}
touch_num = touch_tree
changed = True
while (changed):
    changed = False
    for p1 in touch_head.keys():
        for i in range(len(touch_figure)):
            if p1 == touch_figure[i][0]:
                for j in range(len(touch_figure[i]) - 1):
                    if touch_tree[p1] <= touch_tree[touch_figure[i][j + 1]]:
                        touch_tree[p1] = touch_tree[touch_figure[i][j + 1]] + 1
                        changed = True
po_head = {}
touch_tree = sorted(touch_tree.items(), key=lambda d: d[1], reverse=True)
f.write("Patient Zero(s):")
for p1 in touch_head.keys():
    flag = True
    for i in range(len(touch_figure)):
        if p1 in touch_figure[i][1:]:
            flag = False
    if flag:
        f.write(" {}".format(p1))
        po_head[p1] = 0
f.write("\n")

f.write("Potential Zombies:")
for i in touch_tree[:]:
    if i[1] == 0:
        f.write(" {}".format(i[0]))
        po_head[i[0]] = 0
f.write("\n")
f.write("Neither Patient Zero or Potential Zombie:")
for j in range(len(touch_tree)):
    if touch_tree[j][0] not in po_head.keys():
        f.write(" {}".format(touch_tree[j][0]))
f.write("\n")
j = -99999999
jj = -1
temp = 0
for i in range(len(touch_figure)):
    jj = j
    j = max(len(touch_figure[i][1:]), j)
    if jj != j:
        temp = i
f.write("Most viral People: {}\n".format(touch_figure[temp][0]))
Tastiest = {}
for i in range(len(touch_figure)):
    for j in range(len(touch_figure[i][1:])):
        Tastiest[touch_figure[i][j + 1]] = 0
for i in range(len(touch_figure)):
    for j in touch_figure[i][1:]:
        Tastiest[j] += 1
touch_min1 = -99999999
for i in Tastiest:
    touch_min1 = max(Tastiest[i], touch_min1)
f.write("Tastiest:")
for j in Tastiest.items():
    if j[1] == touch_min1:
        f.write(" {}".format(j[0]))
f.write("\n")
f.write("Heights:\n")
for i in range(len(touch_tree)):
    f.write("  {}: {}\n".format(touch_tree[i][0], touch_tree[i][1]))


Result:

Bob had contact with Carol, Leanne, Mark, Paul and Will.
Carol had contact with Leanne and Mark.
Farley had contact with Paul.
Leanne had contact with Sarai.
Larry had contact with Carol, Leanne, Mark and Will.
Mark had contact with Philip and Zach.
Paul had contact with Zach.
Will had contact with Leanne and Mark.
Zach had contact with Philip.
Patient Zero(s): Bob Farley Larry
Potential Zombies: Sarai Philip
Neither Patient Zero or Potential Zombie: Carol Will Mark Paul Leanne Zach
Most viral People: Bob
Tastiest: Leanne Mark
Heights:
  Bob: 4
  Larry: 4
  Carol: 3
  Will: 3
  Farley: 3
  Mark: 2
  Paul: 2
  Leanne: 1
  Zach: 1
  Sarai: 0
  Philip: 0

猜你喜欢

转载自blog.csdn.net/nyist_yangguang/article/details/121752667