Python - Sorting - python-3.x

So I know how to import a texfile and sort numbers such as:
1
6
4
6
9
3
5
But I don't know how to sort a data that looks like:
Merchant_9976 20122
Merchant_9977 91840
Merchant_9978 92739
Merchant_9979 97252
Merchant_9980 76885
Merchant_9981 67835
Merchant_9982 42201
Merchant_9983 47463
Here's my code so far
import time
def sort_slow(seq):
"""
:param seq:
:return:
"""
for i in range(1, len(seq)):
j = i
while j > 0 and seq[j - 1] > seq[j]:
seq[j - 1], seq[j] = seq[j], seq[j - 1]
j -= 1
return seq
def main():
fileName = str(input('Please enter a filename: '))
file = open(fileName)
sort1 = []
for lines in file:
sort1.append(int(lines.strip()))
#a = [3, 5, 2, 1, 10]
starting = time.clock()
print(starting)
sort_slow(sort1)
print(sort1)
#print(sort_slow([a]))
#print(sort_slow(a))
elapsed = time.clock() - starting
print(elapsed)
main()

lines = file.readlines()
ar = map( lambda x: x.split(), lines )
ar = ar.sort(key=lambda x: x[1])
ar contains all lines in file sorted on second column.

Related

Why is the function returning value of stack as None

I'm not able to figure out why the path variable on last line of the code is being printed out as None. As you can see the second last line of the code is calling the DFS function to find the path between two nodes in a tree (I'm giving a tree as input). I've printed out the value of the stack also before returning it to make sure that it is not None and while being printed inside DFS function it is not None. But I'm not able to understand why it is None when it is returned and stored in path variable. I gave this as input
1
6 1
4 2 1 3 5 2
1 2
2 3
2 4
1 5
5 6
And the out put came as
{0: [1, 4], 1: [0, 2, 3], 2: [1], 3: [1], 4: [0, 5], 5: [4]}
[0, 1, 3]
None
Here is the code for reference
def DFS(adj,x, y,stack,vis):
stack.append(x)
if (x == y):
print(stack)
return stack
vis[x] = 1
if (len(adj[x])>0):
for j in adj[x]:
if (vis[j]==0):
DFS(adj,j,y,stack,vis)
del stack[-1]
T = int(input())
for a in range(T):
N,Q = input().split()
N = int(N)
Q = int(Q)
wt = [int(num) for num in input().split(" ")]
adj = {}
for i in range(N):
adj[i] = []
for b in range(N-1):
u,v = input().split()
u = int(u) - 1
v = int(v) - 1
adj[u].append(v)
adj[v].append(u)
print(adj)
vis = [0]*N
stack = []
path = DFS(adj,0,3,stack,vis)
print(path)
Simple equivalent of your code:
def recursive_func(x):
if x > 0:
return x
else:
x += 1
recursive_func(x)
x = 5
x = recursive_func(x)
print(x)
x = 0
x = recursive_func(x)
print(x)
Output:
5
None
What's happening here?
x, with a value of 5 is sent to recursive_func.
x is greater than 0, so 5 is returned. This is seen in the output.
x, with a value of -5 is sent to recursive_func.
x is not greater than 0, so 1 is added to x.
x, with a value of 1, is then sent to a different recursive_func.
This recursive_func returns 1 because 1 > 0.
This response gets passed to the first recursive_func where the line recursive_func(x) becomes 1, but we don't do anything with it.
recursive_function hits the end of its code, without returning a value. By default None is returned to our main body.
x = recursive_func(x) has become x = None
None is output.
Given this information, why does the following code perform differently?
Simple modification of your code:
def recursive_func_v2(x):
if x > 0:
return x
else:
x += 1
return recursive_func_v2(x)
x = 5
x = recursive_func_v2(x)
print(x)
x = 0
x = recursive_func_v2(x)
print(x)
Output:
5
1

Error: TypeError: cannot perform reduce with flexible type

i am using python version 3.7.Below is the code in which I am performing operation along the rows. i want the mean of the data which are along the rows but I get an error. i am new to numpy and python. i am reading the data from text file.
My code is:
import numpy as np
def getIndexFromDatetime(date_from, date_to):
'''date_from = [2, 10] : 10oclock of day2
'''
if date_from[1] > 24 or date_to[1] > 24: print('error')
start = (date_from[0] - 1) * 48 + date_from[1] * 2
end = (date_to[0] - 1) * 48 + date_to[1] * 2
return [start, end]
def is_num(s):
return s.replace(',', '').replace('.', '').replace('-', '').isnumeric()
def get_dataset(fpath):
with open(fpath, 'r') as f:
cnt = 0
DataWeather = {}
header = []
dtime = []
temp1 = []
temp2 = []
for line in f:
terms = line.split('\t')
#print(terms)
if cnt == 0: header1 = terms
if cnt == 1: header2 = terms
#header.append(terms[3])
cnt += 1
if cnt == 2:
for i in range(len(header1)):
header.append(header1[i]+header2[i])
#print(header)
for i in range(len(terms)):
DataWeather[header[i]] = []
#break
if cnt > 2:
for i in range(len(terms)):
if is_num(terms[i]):
DataWeather[header[i]].append(float(terms[i]))
else:
DataWeather[header[i]].append(terms[i])
for i in range(len(DataWeather[header[0]])):
dtime.append(DataWeather[header[0]][i]+' '+DataWeather[header[1]][i])
return DataWeather, header
def get_data(dataset, header, idx):
y = dataset[header][idx[0]:idx[1]]
return y
data_dir = 'weather_data'
month = 3
day = list(range(1,10))
header_idx = [2,3,4,5,7,16]
for d in day:
print(d)
dtime_from = [d, 9]
dtime_to = [d, 18]
dtime_idx = getIndexFromDatetime(dtime_from, dtime_to)
fpath = '{0}/2019-{1:02}.txt'.format(data_dir, month)
dataset, header = get_dataset(fpath)
for h in header_idx:
print(fpath)
print(header[h], dtime_from, dtime_to, dtime_idx)
data = get_data(dataset, header[h], dtime_idx)
#data= list(map(float,np.array(data)))
#data = map(np.array(data, dtype=np.float))
print(data)
print(np.mean(data))
i am getting the following error:
ret = umr_sum(arr, axis, dtype, out, keepdims)
TypeError: cannot perform reduce with flexible type
i also tried some functions like "map" and "list" as commented in the code still it gives error of converting string to float.

Find multiples of a number in list

I have a list and I want to find all the multiples of that number within a certain tolerance as well as get their indices:
def GetPPMError(t, m):
"""
calculate theoretical (t) and measured (m) ppm
"""
return (((t - m) / t) * 1e6)
multiple = n*1.0033
a = 100
b = [100, 101, 101.0033,102, 102.0066,102.123,103.0099]
results = [p for p in b if abs(GetPPMError(a,b)) < 10]
So I want to find all the multiples like 102.0066 and 103.0099 etc.
where a = 100 + 1*1.0033, a = 100 + 2*1.0033, a = 100 + 3*1.0033 etc
So the result would be the indexes.
Results for the indexes:
[2, 4, 6]
and:
[101.0033, 102.0066, 103.0099]
for the values.
This works for your data:
multiple = 1.0033
a = 100
digits = 6
res = []
res_index = []
for n, x in enumerate(b):
diff = round((x - a) / multiple, digits)
if diff != 0 and diff == int(diff):
res.append(x)
res_index.append(n)
print(res)
print(res_index)
Output:
[101.0033, 102.0066, 103.0099]
[2, 4, 6]

how to find the element in a list having most occurrence using loop

assume
[1,2,4,4,3,3,3]
and upper_limit is 8 (all numbers are not bigger than upper_limit)
how to produce 3 under the help of upper_limit
must run in O(n+upper_limit) overall time.
a = [1,1,2,2,2,3,3,4]
cnt = {}
most = a[0]
for x in a:
if x not in cnt:
cnt[x] = 0
cnt[x] += 1
if cnt[x] > cnt[most]:
most = x
print(most)
You can use collections.Counter
from collections import Counter
lst = [1,2,4,4,3,3,3]
counter = Counter(lst)
most_freq, freq = counter.most_common()[0]
Alternatives to Counter using dictionary.
from collections import defaultdict
d = defaultdict(int)
lst = [1,2,4,4,3,3,3]
for val in lst:
d[val] = d[val] + 1
most_freq, freq = max(d.items(), key = lambda t: t[1])
This example keeps track of the most frequent as you iterate through circumventing the need for max function.
from collections import defaultdict
d = defaultdict(int)
lst = [1,2,4,4,3,3,3]
most_freq, freq = None, 0
for val in lst:
d[val] = d[val] + 1
if d[val] > freq:
most_freq = val
freq = d[val]
print(most_freq, freq)
If you don't want to use a defaultdict then you can just use a normal dictionary and use dict.setdefault instead.
d = {} # set d to dict
d[val] = d.setdefault(val, 0) + 1

Merge sort with help Iterators

I develop merge sort, which can use only iterator (it is a statement of the problem). Output of my function is generator.
I write merge sort function:
def merge_sort(data):
data1, data2 = itertools.tee(data)
counter = 0
for i in data1:
counter += 1
if counter < 2:
return data2
middle = int(counter / 2)
y = itertools.islice(data2, 0, middle)
z = itertools.islice(data2, middle, counter)
sorted_y = merge_sort(y)
sorted_z = merge_sort(z)
return heapq.merge(sorted_y, sorted_z)
I test my function:
def main():
unsorted_list = [10, 3, 5, 0, 1, -5, 6, 2]
result = merge_sort(iter(unsorted_list))
for i in result:
print(i)
But It does not work. I only get the number 10. Where I did the mistake?
This is a right function:
def merge_sort(data):
data1, data2, data3 = itertools.tee(data, 3)
counter = 0
for i in data1:
counter += 1
if counter < 2:
return data3
middle = int(counter / 2)
y = itertools.islice(data2, 0, middle)
z = itertools.islice(data3, middle, counter)
sorted_y = merge_sort(y)
sorted_z = merge_sort(z)
return merge(sorted_y, sorted_z)

Resources