How to create a function for recursively generating iterating functions

2024/9/8 9:05:14

I currently have a bit of Python code that looks like this:

for set_k in data:for tup_j in set_k:for tup_l in tup_j:

The problem is, I'd like the number of nested for statements to differ based on user input. If I wanted to create a function which generated n number of for statements like those above, how might I go about doing that?

Answer
def nfor(data, n=1):if n == 1:yield from iter(data)else:for element in data:yield from nfor(element, n=n-1)

Demo:

>>> for i in nfor(['ab', 'c'], n=1):
...     print(i)
...     
ab
c
>>> for i in nfor(['ab', 'c'], n=2):
...     print(i)
...     
a
b
c
https://en.xdnf.cn/q/73311.html

Related Q&A

How to get molecular weight of a compound in python?

User inputs a formula, for example: C12H2COOHWe have to calculate its molecular weight given that C = 12.01, H = 1.008 and O = 16. We were told to be careful of elements with double digits after it and…

How to install Numpy and Pandas for AWS Lambdas?

Problem: I wanted to use Numpy and Pandas in my AWS lambda function. I am working on Windows 10 with PyCharm. My function compiles and works fine on local machine, however, as soon as package it up and…

vim highlighting everything in red

I added a print line to a python script while the script was executing, and now all the text is highlighted in red when I open the file. Opening and closing the file doesnt get rid of it. Opening a sec…

Using python modules in node.js

Is it possible to create a glue that makes it possible for python modules (more specifically, library bindings) to be used in node.js? Some of the data structures can be directly mapped to V8 objects …

Using PHP to call Virtualenv’ed Python Script

Last night I spent 5.5 hours trying make PHP execute and receive the output of Virtualenv’ed Python script. Nothing worked; except for scripts that were not Virtualenv’ed.What I am trying to do:I am …

How do I find my program name?

ProblemI am unable to write to a different log than the default one using syslog. I am unsure if maybe my app name is wrong in my configuration. Do "program name" and "process name"…

Is it possible to automate the execution of a Python script using Microsoft Flow?

I want to execute a snippet of python code based on some trigger using Microsoft-Flow. Is there a way to do this? Basically I am exploring on Powerapps and Microsoft-Flow. I have data in powerapp, I c…

Python: Selecting numbers with associated probabilities [duplicate]

This question already has answers here:Closed 13 years ago.Possible Duplicates:Random weighted choiceGenerate random numbers with a given (numerical) distribution I have a list of list which contains …

Weights and Biases: Login and network errors

I recently installed Weights and Biases (wandb) for recording the metrics of my machine learning projects. Everything worked fine when connected to wandb cloud instance or when I used a local docker im…

Python: Opening a file without creating a lock

Im trying to create a script in Python to back up some files. But, these files could be renamed or deleted at any time. I dont want my script to prevent that by locking the file; the file should be abl…