map python list of months to values - python-3.5

I have 2 lists whereby the sequence of values in the second list map to the months in the first list:
['Apr-16', 'Jul-16', 'Dec-15', 'Sep-16', 'Aug-16', 'Feb-16', 'Mar-16', 'Jan-16', 'May-16', 'Jun-16', 'Oct-15', 'Nov-15']
[15, 15, 6, 81, 60, 36, 6, 18, 36, 27, 24, 29]
I need to retain 2 seperate lists for use in another function. Using python how do I achieve sorting the lists into monthly order whilst retaining the existing mapping of values to months?

The idea is to
associate both lists
sort the resulting list of couples according to the year/month criteria (months must be converted as month indexes first using an auxiliary dictionary)
then separate the list of couples back to 2 lists, but now sorted according to date.
Here's a commented code which does what you want, maybe not the most compact or academic but works and is simple enough.
a = ['Apr-16', 'Jul-16', 'Dec-15', 'Sep-16', 'Aug-16', 'Feb-16', 'Mar-16', 'Jan-16', 'May-16', 'Jun-16', 'Oct-15', 'Nov-15']
b = [15, 15, 6, 81, 60, 36, 6, 18, 36, 27, 24, 29]
# create a dictionary key=month, value=month index
m = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
monthdict = dict(zip(m,range(len(m))))
# the sort function: returns sort key as (year as integer,month index)
def date_sort(d):
month,year = d[0].split("-")
return int(year),monthdict[month]
# zip both lists together and apply sort
t = sorted(zip(a,b),key=date_sort)
# unzip lists
asort = [e[0] for e in t]
bsort = [e[1] for e in t]
print(asort)
print(bsort)
result:
['Oct-15', 'Nov-15', 'Dec-15', 'Jan-16', 'Feb-16', 'Mar-16', 'Apr-16', 'May-16', 'Jun-16', 'Jul-16', 'Aug-16', 'Sep-16']
[24, 29, 6, 18, 36, 6, 15, 36, 27, 15, 60, 81]

Related

Pick 4 elements of a list and make a number that is a square of other

I have a list of some numbers, and I need to pick 4 of them that can make a number which will be like sqrt(sum_of_nums) % 1 = 0, problem is I don't know how to pick them
Way to pick 4 numbers from a list can be:
from itertools import combinations
nums=[1,2,3,4,5,6]
list(combinations(nums,4))
Now you can find the sum of these numbers by:
[sum(x) for x in list(combinations(nums,4))]
#output
[10, 11, 12, 12, 13, 14, 13, 14, 15, 16, 14, 15, 16, 17, 18]
Now you can filter the perfect squares by:
[sum(x) for x in list(combinations(nums,4))if sum(x)**0.5 % 1 ==0]
#output
[16, 16]

new value is the sum of old values

I have 2 list A and B, list A contain values I want list B to be the sum of value from list a
A = [3,5,7,8,9,12,13,20]
#Wanted result
#B = [3, 8, 15, 23,...77]
#so the new value will be the sum of the old value
# [x1, x2+x1, x3+x2+x1,... xn+xn+xn]
what methods I could use to get the answer, thank you.
The easiest way IMO would be to use numpy.cumsum, to get the cumulative sum of your list:
>>> import numpy as np
>>> np.cumsum(A)
array([ 3, 8, 15, 23, 32, 44, 57, 77])
But you also could do it in a list comprehension like this:
>>> [sum(A[0:x]) for x in range(1, len(A)+1)]
[3, 8, 15, 23, 32, 44, 57, 77]
Another fun way is to use itertools.accumulate, which gives accumulated sums by default:
>>> from itertools import accumulate
>>> list(accumulate(A))
[3, 8, 15, 23, 32, 44, 57, 77]

Python arrow get list of years between date range

I have this function:
def function(start_date_arrow=None,end_date_arrow=None, date_concept=None):
list=[getattr(date, date_concept) for date in arrow.Arrow.range(date_concept, start_date_arrow, end_date_arrow)]
This function works well when iterating over date_concept='month' and date_concept='day'. On the other hand, date_concept='year' only returns a list of one item.
For example:
start_date_arrow= arrow.get('2021-11-05')
end_date_arrow= arrow.get('2022-02-05')
year_list=function(start_date_arrow=start_date_arrow,end_date_arrow=end_date_arrow, date_concept='year')
year_list is [2021]
month_list=function(start_date_arrow=start_date_arrow,end_date_arrow=end_date_arrow, date_concept='month')
month_list is [11, 12, 1, 2]
day_list=function(start_date_arrow=start_date_arrow,end_date_arrow=end_date_arrow, date_concept='day')
day_list is [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
Second and third call are okei, but first one should return [2021,2022] instead of [2021].
Any idea of what is happening in the year call?
Found the issue.
If you use:
start_date_arrow= arrow.get('2021-11-05')
end_date_arrow= arrow.get('2022-02-05')
Year difference between both is less than 1, so it only returns the first one, so to return 2022 in the list end_date_arrow should be end_date_arrow= arrow.get('2022-11-05')
So I forced with an if statement the end date to be bigger just by one year, to force the return of both years.

How to find closest pair or triplet, consecutive numbers group from a python list

i have a list of values [4, 5, 18, 19, 20, 30, 31] and i am trying to making pair, triplet or 4,5.
l = [4, 5, 18, 19, 20, 30, 31]
this list need to convert like p_list
p_list = [(4,5),(18,19,20),(30,31)]

Python for every sequence of random sample generated also include an individual ID

I am trying to program a Lotto simulator, where the code generates 6 random unique numbers out of 45 for about 1000 players where each player has a unique ID. I want to place it into an array that looks like this:
lotto[0...n-1][0...5]
Where [0...n-1] contains the players ID, and [0...5] their unique 6 game numbers.
So it should look something like this when printed
lotto[1][32, 34, 24, 13, 20, 8]
lotto[2][1, 27, 4, 41, 33, 17]
...
lotto[1000][6, 12, 39, 16, 45, 3]
What is the best way of doing something like this without actually merging the two arrays together?
As later on I want to use a merge-sort algorithm to then numerically order the game numbers for each player so it would look something like this without the players ID interfering with the game numbers.
lotto[1][8, 13, 20, 24, 32, 34]
lotto[2][1, 4, 17, 27, 33, 41]
So far I've got:
playerID = list(range(1, 1001))
playerNum = random.sample(range(1, 45), 6)
print(playerID + playerNum)
But that just prints and joins:
[1, 2, 3, ..., 1000, 32, 5, 19, 27, 6, 22]
Thanks for the help.
import random
n_players = 1000
lotto = [random.sample(range(1, 45), 6) for _ in range(n_players)]
OR
import random
n_players = 1000
tup = tuple(range(1, 45))
lotto = []
for _ in range(n_players):
lotto.append(random.sample(tup, 6))

Resources