Python arrow get list of years between date range - python-3.x

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.

Related

Inserting str into index[0] for each list in a list of lists. Python

I would like to iterate through a list of lists inserting a date into each list's index [0] at the beginning of the list.
Basically, 'Here' is where I want the date located:
for x in numlist:
x.insert(0,"Here")
print(x)
['Here', 16, 22, 25, 37, 40]
['Here', 18, 19, 21, 25, 30]
['Here', 20, 22, 33, 34, 39]
One list consists of Strings and the other is a list of lists.
for x in daylist:
print(x)
2022-11-14
2022-11-15
<class 'str'>
for x in numlist:
print(x)
[10, 17, 21, 30, 31]
[12, 14, 15, 34, 41]
<class 'list'>
I've been able to zip the lists together, but I would like to have that date as actually part of the numbers so when I iterate through the lists they're as one.
Eventually, I may want to add other fields to it as well so each element in the list consists of the numbers, the date, and an id number too maybe.
masterlist = [j for i in zip(daylist,numlist) for j in i]
print(masterlist)
OUTPUT: ['2022-08-07', [16, 22, 25, 37, 40], '2022-08-08', [18, 19, 21, 25, 30], '2022-08-09', [20, 22, 33, 34, 39],
I feel like I am fairly close, but nested for loops don't seem to work too well and I am just tired of guessing anymore :). It's been a while since I used python.
Thanks.
If I understand you correctly, you can use zip to zip daylist and numlist together and the use .insert:
daylist = ["2022-11-14", "2022-11-15"]
numlist = [[10, 17, 21, 30, 31], [12, 14, 15, 34, 41]]
for d, l in zip(daylist, numlist):
l.insert(0, d)
print(numlist)
Prints:
[
["2022-11-14", 10, 17, 21, 30, 31],
["2022-11-15", 12, 14, 15, 34, 41]
]
Using iterators:
daylist = iter(["2022-11-14", "2022-11-15"])
numlist = [[10, 17, 21, 30, 31], [12, 14, 15, 34, 41]]
print([[next(daylist), *nums] for nums in numlist])
Output:
[
['2022-11-14', 10, 17, 21, 30, 31],
['2022-11-15', 12, 14, 15, 34, 41]
]

Minimizing the code using the enumerate function in Python

Down below I have a function that checks and outputs any common number within the list_1, list_2, list_3 is there a way that I could use the enumerate or any other function function that would minimize the middle part of the code.
Bit that need minimization:
for elem in l1:#loop to access l1elements
if elem in l2:#checking for element in l2
if elem in l3:#checking for element in l3
Full Code:
def intersect(l1, l2, l3) :#function
for elem in l1:#loop to access l1elements
if elem in l2:#checking for element in l2
if elem in l3:#checking for element in l3
print (element) #display element
list_1 =[27, 20, 22, 21, 17, 12, 24, 23, 19, 14, 11, 26, 25, 13, 15, 21, 18, 28, 29, 10]
list_2 = [14, 25, 26, 21, 22, 17, 11, 23, 27, 18, 24, 28, 12, 29, 16, 19, 13, 10, 20, 15]
list_3 = [19, 21, 11, 24, 16, 17, 18, 22, 26, 10, 23, 29, 27, 13, 25, 14, 15, 20, 28, 12]
intersect(list_1, list_2, list_3) #calling function
You can use numpy intersect1d method to find the common values in the lists or array
def intersect(l1, l2, l3):
print(reduce(np.intersect1d, (l1, l2, l3)))
Result:
[10 11 12 13 14 15 17 18 19 20 21 22 23 24 25 26 27 28 29]
Code:
import numpy as np
from functools import reduce
def intersect(l1, l2, l3):
print(reduce(np.intersect1d, (l1, l2, l3)))
list_1 = [27, 20, 22, 21, 17, 12, 24, 23, 19, 14, 11, 26, 25, 13, 15, 21, 18, 28, 29, 10]
list_2 = [14, 25, 26, 21, 22, 17, 11, 23, 27, 18, 24, 28, 12, 29, 16, 19, 13, 10, 20, 15]
list_3 = [19, 21, 11, 24, 16, 17, 18, 22, 26, 10, 23, 29, 27, 13, 25, 14, 15, 20, 28, 12]
intersect(list_1, list_2, list_3) #calling functio
You should use set object instead.
set_1 = set([27, 20, 22, 21, 17, 12, 24, 23, 19, 14, 11, 26, 25, 13, 15, 21, 18, 28, 29, 10])
set_2 = set([14, 25, 26, 21, 22, 17, 11, 23, 27, 18, 24, 28, 12, 29, 16, 19, 13, 10, 20, 15])
set_3 = set([19, 21, 11, 24, 16, 17, 18, 22, 26, 10, 23, 29, 27, 13, 25, 14, 15, 20, 28, 12])
set_1.intersection(set_2, set_3)
#tony selcuk - It seems that you've tried to loop 3 lists to find corresponding matching numbers? In that case, You could try this code snippet to see if it works as you want. It used the enumerate() to loop all 3 lists together and get their (index, num) as tuple to compare if there is a match. Just run it. Once it proves to work as expected, you can turn it into a function easily. This approach will find all matching numbers that appear in all three list and at the SAME position (index).
for i, j, k in zip(enumerate(list_1), enumerate(list_2), enumerate(list_3)):
#print(i, j, k)
if i == j == k:
print("number:{} order:{}".format(i[1], j[0]))

formatting the orders within a list that are greater than a digit

I use the function list_ .index(list_ ) to get the order of digits within list_ like how list_[0] = 14. I want a function to format the list_ and print the orders that are greater than 20. So the answer would be numbers = 1,2,3,4,5,7,8,10,11,13,18 within list_[] that are greater than 20.
list_ = [14, 25, 26, 21, 22, 17, 11, 23, 27, 18, 24, 28, 12, 29, 16, 19, 13, 10, 20, 15]
list_ = [14, 25, 26, 21, 22, 17, 11, 23, 27, 18, 24, 28, 12, 29, 16, 19, 13, 10, 20, 15]
for index,i in enumerate(list_):
if i >= 20:
print(index)
If you want it as a list
x = [index for index,i in enumerate(list_) if i >= 20]
print(x)
>>> [1, 2, 3, 4, 7, 8, 10, 11, 13, 18]

Arranging a list to ouput equidistant points

I want to write a code that prints the first and last number within a list as well as 5 other points. The points have to be equidistant from each other as well. So the numbers outputted would be 25, 19, 29, 16, 20.
list_ = [25, 23, 14, 22, 19, 13, 12, 10, 28, 29, 11, 15, 18, 27, 16, 21, 20, 17, 24, 26]
Is this something you are looking for?
>>> size = len(lst)
>>> size // 5 # five elements starting with first one.
4
>>>
>>> for i in range(0, size, size//5):
print(lst[i])
25
19
28
18
20

In a list of list objects, how do i compare each list object with all other list objects?

I have a list with multiple list objects in it, I wish to compare each inner list to all other inner lists in the outer list object and if a match is found, print it out.
I have already tried looping through each object in the list and compare it to all other objects but I always match the one I start with.
My example list is this:
list_of_lists = [
[1, 11, 17, 21, 33, 34],
[4, 6, 10, 18, 22, 25],
[1, 15, 20, 22, 23, 31],
[3, 5, 7, 18, 23, 27],
[3, 22, 24, 25, 28, 37],
[7, 11, 12, 25, 28, 31],
[1, 11, 17, 21, 33, 34],
...
]
Note that list_of_lists[0] matches list_of_lists[6], which I wish to match in this example.
The expected result is a loop that goes through each list object and compares it to all other objects, if there's a match - print it out.
You could do something like this:
list_of_lists = [
[1, 11, 17, 21, 33, 34],
[4, 6, 10, 18, 22, 25],
[1, 15, 20, 22, 23, 31],
[3, 5, 7, 18, 23, 27],
[3, 22, 24, 25, 28, 37],
[7, 11, 12, 25, 28, 31],
[1, 11, 17, 21, 33, 34],
]
for i in range(len(list_of_lists)):
for j in range(len(list_of_lists)):
# If you're checking the row against itself, skip it.
if i == j:
break
# Otherwise, if two different lists are matching, print it out.
if list_of_lists[i] == list_of_lists[j]:
print(list_of_lists[i])
This outputs:
[1, 11, 17, 21, 33, 34]

Resources