Note that there are some explanatory texts on larger screens.

plurals
  1. POwhats another way to write python3 zip
    text
    copied!<p>Ive been working on a code that reads lines in a file document and then the code organizes them. However, i got stuck at one point and my friend told me what i could use. the code works but it seems that i dont know what he is doing at line 7 and 8 FROM THE BOTTOM. I used #### so you guys know which lines it is.</p> <p>So, essentially how can you re-write those 2 lines of codes and why do they work? I seem to not understand dictionaries from sys import argv</p> <pre><code>filename = input("Please enter the name of a file: ") file_in=(open(filename, "r")) print("Number of times each animal visited each station:") print("Animal Id Station 1 Station 2") animaldictionary = dict() for line in file_in: if '\n' == line[-1]: line = line[:-1] (a, b, c) = line.split(':') ac = (a,c) if ac not in animaldictionary: animaldictionary[ac] = 0 animaldictionary[ac] += 1 alla = [] for key, value in animaldictionary: if key not in alla: alla.append(key) print ("alla:",alla) allc = [] for key, value in animaldictionary: if value not in allc: allc.append(value) print("allc", allc) for a in sorted(alla): print('%9s'%a,end=' '*13) for c in sorted(allc): ac = (a,c) valc = 0 if ac in animaldictionary: valc = animaldictionary[ac] print('%4d'%valc,end=' '*19) print() print("="*60) print("Animals that visited both stations at least 3 times: ") for a in sorted(alla): x = 'false' for c in sorted(allc): ac = (a,c) count = 0 if ac in animaldictionary: count = animaldictionary[ac] if count &gt;= 3: x = 'true' if x is 'true': print('%6s'%a, end=' ') print("") print("="*60) print("Average of the number visits in each month for each station:") #(alla, allc) = #for s in zip(*animaldictionary.keys()): # (alla,allc).append(s) #print(alla, allc) (alla,allc,) = (set(s) for s in zip(*animaldictionary.keys())) ##### how else can you write this ##### how else can you rewrite the next code print('\n'.join(['\t'.join((c,str(sum(animaldictionary.get(ac,0) for a in alla for ac in ((a,c,),))//12)))for c in sorted(allc)])) print("="*60) print("Month with the maximum number of visits for each station:") print("Station Month Number") print("1") print("2") </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload