Expand the following dictionary into following list

2024/9/22 8:24:44

how to generate the following list from the following dictionary

d = {2: 4, 3: 1, 5: 3}f = [2**1,2**2, 2**3, 2**4, 3**1, 5**1, 5**2, 5**3, 2**1 * 3, 2**2 * 3, 2**3 * 3, 2**4 * 3, 5**1 * 3, 5**2 * 3, 5**3 * 3, 2**1 * 5, 2**2 * 5, 2**3 * 5, 2**4 * 5, 2**1 * 5**2, 2**2 * 5**2, 2**3 * 5**2, 2**4 * 5**2, 3**1 * 5**2, 2**1 * 5**3, 2**2 * 5**3, 2**3 * 5**3, 2**4 * 5**3, 3**1 * 5**3, 2**1 * 3**1 * 5**1, 2**1 * 3**1 * 5**2, 2**1 * 3**1 * 5**3, 2**2 * 3**1 * 5**1, 2**2 * 3**1 * 5**2, 2**2 * 3**1 * 5**3, 2**3 * 3**1 * 5**1, 2**3 * 3**1 * 5**2, 2**3 * 3**1 * 5**3, 2**4 * 3**1 * 5**1, 2**4 * 3**1 * 5**2, 2**4 * 3**1 * 5**3, ]

Moreover I want to expand for more general case ie dictionary of k key-value pairs

Edit: Explanation of the pattern: Each key is raised to power of 0 to it's value and after that for each of those terms we do combination with similar terms from other keys.

I can think of doing so in recursive ways. Any non recusive approach is welcome.

PS: Order is not important

Answer

One way, expanding the list with each possible factor and its powers:

f = [1]
for k, v in d.items():f += [x * k**ifor x in f for i in range(1, v+1)]

Perhaps faster, repeatedly multiplying with the factor instead of computing powers:

f = [1]
for k, v in d.items():g = ffor _ in range(v):g = [x * k for x in g]f += g

Somewhat tricky version of that which is short and I believe fast (confirmed by OP in the comments):

from itertools import islice, repeat
from operator import mulf = [1]
for k, v in d.items():f += map(mul, repeat(k, len(f) * v), f)

A completely different way, first computing the powers for each factor and then using itertools.product and math.prod to combine them:

from itertools import product, accumulate, repeat
from math import prodf = list(map(prod, product(*[[k**i for i in range(v+1)]for k, v in d.items()])))

All include the number 1 in the list. If that's a problem, just remove it.

https://en.xdnf.cn/q/119152.html

Related Q&A

how can I improve the accuracy rate of the below trained model using CNN

I have trained a model using python detect the colors of the gemstone and have built a CNN.Herewith Iam attaching the code of mine.(Referred https://www.kaggle.com) import os import matplotlib.pyplot a…

Classes and methods, with lists in Python

I have two classes, called "Pussa" and "Cat". The Pussa has an int atribute idPussa, and the Cat class has two atributes, a list of "Pussa" and an int catNum. Every class …

polars dataframe TypeError: must be real number, not str

so bascially i changed panda.frame to polars.frame for better speed in yolov5 but when i run the code, it works fine till some point (i dont exactly know when error occurs) and it gives me TypeError: m…

Get a string in Shell/Python using sys.argv

Im beginning with bash and Im executing a script :$ ./readtext.sh ./InputFiles/applications.txt Here is my readtext.sh code :#!/bin/bash filename="$1" counter=1 while IFS=: true; doline=read …

Need to combine two functions into one (Python)

Here is my code-def Max(lst):if len(lst) == 1:return lst[0]else:m = Max(lst[1:])if m > lst[0]: return melse:return lst[0] def Min(lst):if len(lst) == 1:return lst[0]else:m = Min(lst[1:])if m < ls…

Error: descriptor blit requires a pygame.Surface object but received a NoneType [duplicate]

This question already has answers here:How can I draw images and sprites in Pygame?(4 answers)Closed 2 years ago.I am creating a game. I wrote this code to create a sprite and its hitbox:hg = pygame.i…

How can I deal with overlapping rectangles? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 5 years ago.Improve…

panda df not showing all rows after loading from MS SQL

Im using Pandas with latest sqlalchemy (1.4.36) to query a MS SQL DB, using the following Python 3.10.3 [Win] snippet: import pandas as pd # from sqlalchemy…

How to extract multiple grandchildren/children from XML where one child is a specific value?

Im working with an XML file that stores all "versions" of the chatbot we create. Currently we have 18 versions and I only care about the most recent one. Im trying to find a way to extract a…

Is there are a way to replace python import with actual sources?

I am having python files with the import statements which I would like to replace into the actual code placed in the foo.py.For instance, with the in file:from foo import Barbar = Bar() print barI woul…