Equal Less and Greater List python - python

Hi guys I'm trying to figure out how to compare the previous number with the current one until the last digit.
this is the list:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 1, 2, 3, 4, 5, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7]
I need on each sequence of iteration the highest number (e.g. in the first one it's 10).
After the sequence is finalized it again begins counting from the beginning (1,2,3,4..etc) until a condition is reached.
Now the problem is that I get the result correctly all until the very last iteration, the max number should be in the 7 (as you can see: 1,2,3,4,5,6,7)
but the algorithm skips it. I tried with zip function even with iter loop the same issue.
example codes that yield the same results are the following:
def printElements(arr, n):
# Traverse array from index 1 to n-2
# and check for the given condition
for i in range(1, n - 1, 1):
if (arr[i] > arr[i - 1] and
arr[i] > arr[i + 1]):
print(arr[i], end = " ")
# Driver Code
if __name__ == '__main__':
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 1, 2, 3, 4, 5, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7]
n = len(arr)
printElements(arr, n)
print(count_shelf)
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 1, 2, 3, 4, 5, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7]
for prev, current in zip(arr, arr[1:]):
print(prev,current)
if prev > current:
x = prev
print(prev,'prev greater')
print(current,'current')
results of the last alg:
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 1
10 prev greater
1 current
1 2
2 3
3 4
4 5
5 6
6 1
6 prev greater
1 current
1 2
2 3
3 4
4 5
5 6
6 7
7 1
7 prev greater
1 current
1 2
2 3
3 1
3 prev greater
1 current
1 2
2 3
3 4
4 5
5 1
5 prev greater
1 current
1 2
2 3
3 4
4 1
4 prev greater
1 current
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 1
8 prev greater
1 current
1 2
2 3
3 4
4 5
5 6
6 7 ``

arr.append(float('-inf'))
def printElements(arr, n):
# Traverse array from index 1 to n-2
# and check for the given condition
for i in range(1, n - 1, 1):
if (arr[i] > arr[i - 1] and
arr[i] > arr[i + 1]):
print(arr[i], end = " ")
# Driver Code
if __name__ == '__main__':
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 1, 2, 3, 4, 5, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7]
arr.append(float('-inf'))
n = len(arr)
printElements(arr, n)

You can use list comprehension to get the maximum value for each sequence.
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 1, 2, 3, 4, 5, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7]
maxvals = [lst[x] for x in range(len(lst)) if x == len(lst)-1 or lst[x] > lst[x+1]]
print(maxvals)
Output
[10, 6, 7, 3, 5, 4, 8, 7]
I don't see any way to use zip to find the solution.

Related

How to generate array like 1 1 2 1 2 3 1 2 3 4 .... in python using numpy?

Of course,the array above is simple.We can use nested loop in python to generate it.
for i in range(1,10):
for j in range(1,j+1):
print(j)
But how can I use numpy in python to crate it quickly?
One quick way to generate the array is to use np.tril_indices. The column indices of the lower triangle of 2D square array is what you need:
np.tril_indices(10)[1] + 1
array([ 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2,
3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6,
7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10])

Getting the sum of rows until a certain point

I would like to have some code that would add one from the row above until a new 'SCU_KEY' comes up. For example here is code and what I would like:
df = pd.DataFrame({'SCU_KEY' : [3, 3, 3, 5, 5, 5, 7, 8, 8, 8, 8], 'count':[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]})
Expected output:
df = pd.DataFrame({'SCU_KEY' : [3, 3, 3, 5, 5, 5, 7, 8, 8, 8, 8], 'count':[1, 2, 3, 1, 2, 3, 1, 1, 2, 3, 4]})
You can try this:
import pandas as pd
df = pd.DataFrame({
'SCU_KEY': [3, 3, 3, 5, 5, 5, 7, 8, 8, 8, 8],
'count': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
})
s = df['SCU_KEY']
df['count'] = s.groupby(s).cumcount() + 1
print(df)
It gives:
SCU_KEY count
0 3 1
1 3 2
2 3 3
3 5 1
4 5 2
5 5 3
6 7 1
7 8 1
8 8 2
9 8 3
10 8 4
This assumes that values of the SCU_KEY column cannot reappear once they change, or that they can reappear but then you want to continue counting them where you left off.
If, instead, each contiguous sequence of repeating values should be counted starting from 1, then you can use this instead:
s = df['SCU_KEY']
df['count'] = s.groupby((s.shift() != s).cumsum()).cumcount() + 1
For the above dataframe the result will be the same as before, but you can add, say, 3 at the end of the SCU_KEY column to see the difference.
This will do the job-
import pandas as pd
df = pd.DataFrame({'SCU_KEY' : [3, 3, 3, 5, 5, 5, 7, 8, 8, 8, 8], 'count':[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]})
for item in set(df['SCU_KEY']):
inc = 0
for i in range(len(df.index)):
if df['SCU_KEY'][i] == item:
df['count'][i] += inc
inc += 1
P.S.- As others have mentioned, it's a good practice to show your work before asking others for solution. It shows your effort which everyone appreciates and encourages to help you.

A Function take 'N' as argument, performs an Increase N times of Elements then decrease (N-1) times and then return as a list in the Python

The problem is, suppose I pass 'N' = 9 to the function,
So the list will be
list = [1,2,3,4,5,6,7,8,9,8,7,6,5,4,3,2,1].
At first, the list elements increased to (1-9) then it decreased to reversed order (8-1)
Please suggest the easiest way to achieve this.
Thank You in advance.
list(range(1, 1+N)) + list(range(N-1, 0, -1))
You can use list comprehension to achieve in one line. First we have the list comprehension which has a simple loop that appends the number from 1 to n. The second is a list comprehension where the numbers in the for loop are passed through the equation (n+1)-i. This is to calculate the difference between the current value of i and n+1. This gives us the pattern of descending numbers. Finally, both lists are added and stored in the variable r.
r = [x for x in range(1,n+1)] + [n+1-i for i in range(2, n+1)]
When r is printed out it produces the following output.
[1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1]
A simple way would be to use a range from -8 to 8 and output the difference from 9 (ignoring the sign):
N = 9
print([N-abs(i) for i in range(1-N,N)])
[1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1]
the range(1-N,N) will generate:
-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8
in absolute value this will be:
8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8
difference from 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8 -
-------------------------------------------------
1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1

Sort by frequency using the key argument not working as expected [duplicate]

This question already has answers here:
Sort list by frequency
(8 answers)
Closed 3 years ago.
A given array is to be sorted on the basis of the frequency of occurrence of its elements.
I tried using key=arr.count (arr is the name of the list I want to sort). It works for some inputs. I also tried using the collections.Counter() class object, it behaved similarly to how arr.count did.
>>> arr = [6, 4, 6, 4, 4, 6, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 1, 7, 7, 7, 2, 2, 2, 7, 1, 7, 1, 2, 1, 2, 7, 1, 1, 7, 2, 1, 2]
>>> sorted(arr, key=arr.count)
[6, 4, 6, 4, 4, 6, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 1, 7, 7, 7, 2, 2, 2, 7, 1, 7, 1, 2, 1, 2, 7, 1, 1, 7, 2, 1, 2]
>>> sorted(arr, key=counts.get)
[6, 4, 6, 4, 4, 6, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 1, 7, 7, 7, 2, 2, 2, 7, 1, 7, 1, 2, 1, 2, 7, 1, 1, 7, 2, 1, 2]
Expected output is:
1 1 1 1 1 1 1 2 2 2 2 2 2 2 7 7 7 7 7 7 7 3 3 3 3 3 3 5 5 5 5 4 4 4 6 6 6
Not sure what I am doing wrong here.
Use a tuple to sort first by frequency and then by value, for inverting the ordering you can use - (so smallest numbers comes first), and then since you want the biggest count first use reverse:
sorted(arr, key=lambda x: (arr.count(x), -x), reverse=True)
Output:
[1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 7, 7, 7, 7, 7, 7, 7, 3, 3, 3, 3, 3, 3, 5, 5, 5, 5, 4, 4, 4, 6, 6, 6]
I think the problem is that some entries have the same frequency, e.g.:
arr.count(1) == arr.count(2) == arr.count(7)
To make sure that these entries remain grouped, you have to sort not only by counts, but also by value:
counts = collections.Counter(arr)
sorted(arr, key=lambda x: (counts[x], x), reverse=True)
Output:
[7, 7, 7, 7, 7, 7, 7, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 5, 5, 5, 5, 6, 6, 6, 4, 4, 4]

Python append values to empty list by skipping values in between a list

Just a minimal example of what I want to achieve.
I have an array:
array = [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,....,1,2,3,4,5,6,7,8,9,10]
I would like to loop through this array and create a new array which looks like this:
new_array = [1,1,2,1,2,3,1,2,3,4,1,2,3,4,5,....,1,2,3,4,5,6,7,8,9,10]
i.e. loop through array a, get the first value (i.e. 1), then skip the remaining 9 values, then get the first and the second value (i.e. 1,2), then skip the remaining 8 values, and so on.
The idea I came up with was to create indices and use it in the following way:
In [1]: indices = np.arange(1,10,1)
Out[1]: array([1, 2, 3, 4, 5, 6, 7, 8, 9])
new_array = []
for i in array:
for a,b in zip(indices,range(10)):
new_array.append(i[0:a]) # here I am including i[0:1], i[0:2] and so on
So it loops through array and gets the first value, then skips the remaining 9 values, then gets the first two values and skips the remaining 8 values and so on.
But this doesn't seem to work. How can I achieve this ?
If you don't need all values (only pass your scheme)
list = [1,2,3,4,5,6,7,8,9,10] * 10
output = []
skip = 9
i = 0
while skip > 0:
output.append(list[i])
i += skip + 1
skip -= 1
print(list)
print(output)
But your "new_array" doesn't pass your algorithm. Why not:
[1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10]
If I get first 1 (it has index 0) after skip 9 values I will get 1, after then skipping 8 values I won't get 2
Edit: Ok, I understand now. This should work:
list = [1,2,3,4,5,6,7,8,9,10] * 10
output = []
skip = 9
i = 0
j = 0
add = 1
while skip >= 0:
newList = list[i:j+1]
for x in newList:
output.append(x)
i += skip + add
j += skip + add + 1
add += 1
skip -= 1
print(output)
For a signle list you can also use list extension for this:
list = [1,2,3,4,5,6,7,8,9,10]
output = []
i = 1
while i <= 10:
output.extend(list[0:i])
i +=1
print output
For your list you can extend this to:
list = [1,2,3,4,5,6,7,8,9,10]*10
output = []
i = 1
j = 0
k = 1
while k <= 10:
output.extend(list[j:i])
j +=10
k +=1
i = j+k
print output
can you please try this code. I have tested this code on python 3 and it is working fine.
inp = [1,2,3,4,5,6,7,8,9,10] * 10
inp_len = len(inp);
output = [inp[0]]
skip = 9
limit= skip +2;
pointer = 1;
while skip > 0:
pointer = pointer+skip;
if(pointer >inp_len):
pointer = pointer %inp_len;
for x in inp[pointer : pointer+limit-skip ]:
output.append(x);
pointer= pointer+ limit-skip ;
skip=skip-1;
print(inp)
print(output)
Explaination - Adding default first element and then adding elements in below order.
skip 9 elements = [1, 2]
skip 8 elements = [1, 2, 3]
skip 7 elements = [1, 2, 3, 4]
skip 6 elements = [1, 2, 3, 4, 5]
skip 5 elements = [1, 2, 3, 4, 5, 6]
skip 4 elements = [1, 2, 3, 4, 5, 6, 7]
skip 3 elements = [1, 2, 3, 4, 5, 6, 7, 8]
skip 2 elements = [1, 2, 3, 4, 5, 6, 7, 8, 9]
skip 1 elements = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Please test it with your input . Here i am using a defined list.
Input list -[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output list - [1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
How about:
list = [1,2,3,4,5,6,7,8,9,10] * 10;
output = [];
take = 1;
index = 0;
while index < len(list):
# take the "take" next elements, notice the "index" is not changing.
for i in range(take):
output.append(list[index + i]);
# Skip the remaining values and increase the "take"
index += 10;
take += 1;
print(list)
print(output)
You can also go like this with two indices:
list = [1,2,3,4,5,6,7,8,9,10] * 10;
output = [];
index = 0;
for i in range(10):
for j in range(10):
if j <= i:
output.append(list[index + i]);
index++;
print(list)
print(output)
array = [1,2,3,4,5,6,7,8,9,10]*10
new_array = []
c=j=0
while c < len(array):
for i in range(0,j):
new_array.append(array[i])
i+=1
j+=1
c+=10
print(new_array)
Output
[1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9]
it's not too different from printing a triangle with numbers from 1 to 10.
array = [x for x in range(1,11)]*10 #here 10 means adding same list 10 times
new_array = []
for i in range(10):
new_array += array[10*i:10*i+i+1]
print(new_array)
hope this helps!
It takes 0.0005 sec

Categories

Resources