no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
| — | development:python:examples [2020/01/09 07:49] (current) – created - external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Python examples 9 ====== | ||
| + | <code python> | ||
| + | def zapis(): | ||
| + | f=open(" | ||
| + | while True: | ||
| + | a=input(" | ||
| + | if a==" | ||
| + | break | ||
| + | else: | ||
| + | if len(a)< | ||
| + | f.write(a + " | ||
| + | f.close() | ||
| + | f=open(" | ||
| + | lines=f.readlines() | ||
| + | print("" | ||
| + | print(" | ||
| + | for line in lines: | ||
| + | print(line, end = '' | ||
| + | f.close() | ||
| + | </ | ||
| + | |||
| + | <code python> | ||
| + | def ispisNS(ime): | ||
| + | import csv | ||
| + | a=0 | ||
| + | with open(ime) as f: | ||
| + | reader=csv.reader(f, | ||
| + | for line in reader: | ||
| + | for col in line: | ||
| + | if len(col)> | ||
| + | a=len(col) | ||
| + | b=col | ||
| + | print(" | ||
| + | </ | ||
| + | |||
| + | <code python> | ||
| + | def zapisL(f, | ||
| + | ret=False | ||
| + | if len(f)< | ||
| + | f1=f.split(" | ||
| + | if f1[0][0: | ||
| + | ret=True | ||
| + | f=open(f," | ||
| + | for a in range(1, | ||
| + | b=0 | ||
| + | c="" | ||
| + | while b<a: | ||
| + | if len(c)==0: | ||
| + | c=str(a) | ||
| + | else: | ||
| + | c=c + " " + str(a) | ||
| + | b+=1 | ||
| + | f.write(c + " | ||
| + | f.close() | ||
| + | return ret | ||
| + | </ | ||
| + | |||
| + | <code python> | ||
| + | def pr(qty): | ||
| + | num=0 | ||
| + | n=0 | ||
| + | c="" | ||
| + | while n<qty: | ||
| + | if num > 1: | ||
| + | for i in range(2, | ||
| + | if (num % i) == 0: | ||
| + | break | ||
| + | else: | ||
| + | n+=1 | ||
| + | c=c+str(num)+";" | ||
| + | num+=1 | ||
| + | return(c) | ||
| + | def zapisP(f, | ||
| + | ret=False | ||
| + | if len(f)< | ||
| + | if " | ||
| + | if f[0: | ||
| + | ret=True | ||
| + | f=open(f+" | ||
| + | f.write(pr(n+1) + " | ||
| + | f.close() | ||
| + | return ret | ||
| + | </ | ||
| + | |||
| + | <code python> | ||
| + | def izr(f): | ||
| + | sa=0 | ||
| + | su=0 | ||
| + | os=0 | ||
| + | uk=0 | ||
| + | f=open(f," | ||
| + | text=f.read() | ||
| + | f.close() | ||
| + | for a in text: | ||
| + | uk+=1 | ||
| + | if a.isalpha(): | ||
| + | if a.lower() in [' | ||
| + | sa+=1 | ||
| + | else: | ||
| + | su+=1 | ||
| + | else: | ||
| + | os+=1 | ||
| + | f=open(" | ||
| + | f.write(" | ||
| + | f.write(" | ||
| + | f.write(" | ||
| + | f.close() | ||
| + | </ | ||
| + | |||
| + | <code python> | ||
| + | def frek(f): | ||
| + | f=open(f," | ||
| + | lines=f.readlines() | ||
| + | f.close() | ||
| + | newline="" | ||
| + | for line in lines: | ||
| + | for c in line.lower(): | ||
| + | if c.isalpha() or c.isspace(): | ||
| + | newline=newline+c | ||
| + | words=newline.split() | ||
| + | qty=dict() | ||
| + | for word in words: | ||
| + | if word in qty: | ||
| + | qty[word]+=1 | ||
| + | else: | ||
| + | qty[word]=1 | ||
| + | import collections | ||
| + | od=collections.OrderedDict(sorted(qty.items())) | ||
| + | f=open(" | ||
| + | for k,v in od.items(): f.write(k+": | ||
| + | f.close() | ||
| + | </ | ||
| + | |||
| + | <code python> | ||
| + | def ar(f): | ||
| + | f=open(f," | ||
| + | lines=f.readlines() | ||
| + | f.close() | ||
| + | a=0 | ||
| + | b=0 | ||
| + | for line in lines: | ||
| + | a=a+int(line) | ||
| + | b+=1 | ||
| + | f=open(" | ||
| + | f.write(str(a/ | ||
| + | f.close() | ||
| + | </ | ||
| + | |||
| + | <code python> | ||
| + | def zbr(f): | ||
| + | f=open(f," | ||
| + | lines=f.readlines() | ||
| + | f.close() | ||
| + | a=0 | ||
| + | for line in lines: | ||
| + | a=a+int(line) | ||
| + | f=open(" | ||
| + | f.write(str(a)) | ||
| + | f.close() | ||
| + | </ | ||