I am trying to find dates 27 to 33 days apart from the starting date provided in the dictionary; however, I cannot append the new dates to the key-value combination pair. I keep getting an error that cannot assign to a function call. Any help would be appreciated.
dictionary_items = {"one": [20210101], "two": [20210202], "three": [20210303], "four": [20210404]}
dictionary_items = dictionary_items.items()
for key, value in dictionary_items:
temp_var = []
value = value[-1]
temp_var.append(value)
for date in range(20210101, 20210531, 1):
x = (date - value) + 1
if 27 <= x <= 33:
temp_var.append(date)
dictionary_items[key] = temp_var
print(dictionary_items)
Desired output :
{"one": [20210101, Date2, Date3], "two": [20210202, Date2, Date3], "three": [20210303, Date2, Date3],
"four": [20210404, Date2, Date3]}
That's because you redefined dictionary_items
label to it's .items()
and you are trying to add item to items
view. It's a just a view object of the original dictionary, you should add items to the dictionary itself:
dictionary = {"one": [20210101], "two": [20210202], "three": [20210303], "four": [20210404]}
for key, value in dictionary.items():
temp_var = []
value = value[-1]
temp_var.append(value)
for date in range(20210101, 20210531, 1):
x = (date - value) + 1
if 27 <= x <= 33:
temp_var.append(date)
dictionary[key] = temp_var
print(dictionary)
output :
{'one': [20210101, 20210127, 20210128, 20210129, 20210130, 20210131, 20210132, 20210133], 'two': [20210202, 20210228, 20210229, 20210230, 20210231, 20210232, 20210233, 20210234], 'three': [20210303, 20210329, 20210330, 20210331, 20210332, 20210333, 20210334, 20210335], 'four': [20210404, 20210430, 20210431, 20210432, 20210433, 20210434, 20210435, 20210436]}
Those temporary variables can be removed:
dictionary = {"one": [20210101], "two": [20210202], "three": [20210303], "four": [20210404]}
for key, value in dictionary.items():
for date in range(20210101, 20210531):
x = (date - value[0]) + 1
if 27 <= x <= 33:
dictionary[key].append(date)
print(dictionary)
output is the same.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments