Home > Mobile >  Pythonic way to get the difference in timestamps in the same dict from one value to another
Pythonic way to get the difference in timestamps in the same dict from one value to another

Time:07-10

Is there any pythonic way of getting the values minus from the same dictionary.

I have a dictionary

{'ZLER3B8I': {1: datetime.datetime(2022, 6, 30, 7, 32, 34, 586000, tzinfo=datetime.timezone.utc),
     3: datetime.datetime(2022, 7, 1, 6, 4, 10, 446000, tzinfo=datetime.timezone.utc),
     5: datetime.datetime(2022, 7, 1, 6, 4, 18, 596000, tzinfo=datetime.timezone.utc),
     6: datetime.datetime(2022, 7, 1, 11, 8, 7, 151000, tzinfo=datetime.timezone.utc),
     7: datetime.datetime(2022, 7, 1, 13, 16, 44, 104000, tzinfo=datetime.timezone.utc),
    11: datetime.datetime(2022, 7, 1, 6, 41, 45, 749000, tzinfo=datetime.timezone.utc),
    12: datetime.datetime(2022, 7, 1, 10, 19, 55, 470000, tzinfo=datetime.timezone.utc)}}

Is an efficient pythonic way of calculating the timestamp from the 1st key, value to the all the other key values ? Without many nested for loops.

like from 1 key to 3 key time taken is 28 hrs, 30 mins

{(1-3): 28 Hrs 30 mins, (1-5): 30 hrs 2 mins,.. so on  }

CodePudding user response:

data = {'ZLER3B8I': {1: datetime.datetime(2022, 6, 30, 7, 32, 34, 586000, tzinfo=datetime.timezone.utc),
     3: datetime.datetime(2022, 7, 1, 6, 4, 10, 446000, tzinfo=datetime.timezone.utc),
     5: datetime.datetime(2022, 7, 1, 6, 4, 18, 596000, tzinfo=datetime.timezone.utc),
     6: datetime.datetime(2022, 7, 1, 11, 8, 7, 151000, tzinfo=datetime.timezone.utc),
     7: datetime.datetime(2022, 7, 1, 13, 16, 44, 104000, tzinfo=datetime.timezone.utc),
    11: datetime.datetime(2022, 7, 1, 6, 41, 45, 749000, tzinfo=datetime.timezone.utc),
    12: datetime.datetime(2022, 7, 1, 10, 19, 55, 470000, tzinfo=datetime.timezone.utc)}}

out = {i:{f'1-{j}':y-x[1] for j, y in x.items() if j != 1} for i, x in data.items()}
print(out)

Output:

{'ZLER3B8I': {'1-3': datetime.timedelta(seconds=81095, microseconds=860000),
              '1-5': datetime.timedelta(seconds=81104, microseconds=10000),
              '1-6': datetime.timedelta(days=1, seconds=12932, microseconds=565000),
              '1-7': datetime.timedelta(days=1, seconds=20649, microseconds=518000),
              '1-11': datetime.timedelta(seconds=83351, microseconds=163000),
              '1-12': datetime.timedelta(days=1, seconds=10040, microseconds=884000)}}

{i:{f'1-{j}':f'{y-x[1]}' for j, y in x.items() if j != 1} for i, x in data.items()}
{'ZLER3B8I': {'1-3': '22:31:35.860000',
              '1-5': '22:31:44.010000',
              '1-6': '1 day, 3:35:32.565000',
              '1-7': '1 day, 5:44:09.518000',
              '1-11': '23:09:11.163000',
              '1-12': '1 day, 2:47:20.884000'}}

CodePudding user response:

Sounds like you're looking for itertools.combinations:

d = {
    1: datetime.datetime(2022, 6, 30, 7, 32, 34, 586000, tzinfo=datetime.timezone.utc),
    3: datetime.datetime(2022, 7, 1, 6, 4, 10, 446000, tzinfo=datetime.timezone.utc),
    5: datetime.datetime(2022, 7, 1, 6, 4, 18, 596000, tzinfo=datetime.timezone.utc),
    6: datetime.datetime(2022, 7, 1, 11, 8, 7, 151000, tzinfo=datetime.timezone.utc),
    7: datetime.datetime(2022, 7, 1, 13, 16, 44, 104000, tzinfo=datetime.timezone.utc),
    11: datetime.datetime(2022, 7, 1, 6, 41, 45, 749000, tzinfo=datetime.timezone.utc),
    12: datetime.datetime(2022, 7, 1, 10, 19, 55, 470000, tzinfo=datetime.timezone.utc),
}

for i, j in itertools.combinations(d.keys(), 2):
    print(f'{i=} - {j=} = {d[i] - d[j]}')

Sample execution:

i=1 - j=3 = -1 day, 1:28:24.140000
i=1 - j=5 = -1 day, 1:28:15.990000
i=1 - j=6 = -2 days, 20:24:27.435000
i=1 - j=7 = -2 days, 18:15:50.482000
i=1 - j=11 = -1 day, 0:50:48.837000
i=1 - j=12 = -2 days, 21:12:39.116000
i=3 - j=5 = -1 day, 23:59:51.850000
i=3 - j=6 = -1 day, 18:56:03.295000
i=3 - j=7 = -1 day, 16:47:26.342000
i=3 - j=11 = -1 day, 23:22:24.697000
i=3 - j=12 = -1 day, 19:44:14.976000
i=5 - j=6 = -1 day, 18:56:11.445000
i=5 - j=7 = -1 day, 16:47:34.492000
i=5 - j=11 = -1 day, 23:22:32.847000
i=5 - j=12 = -1 day, 19:44:23.126000
i=6 - j=7 = -1 day, 21:51:23.047000
i=6 - j=11 = 4:26:21.402000
i=6 - j=12 = 0:48:11.681000
i=7 - j=11 = 6:34:58.355000
i=7 - j=12 = 2:56:48.634000
i=11 - j=12 = -1 day, 20:21:50.279000

There's also permutations if you want to compare all possible pairings (i.e., 1 - 3 and 3 - 1 are distinct, not just 1 - 3).

  • Related