Jump to content

For Loop With A New Variable Assigned To Each Element

Hey Guys, I've got some search results returned from a re.search function and I want to get all the results. I'd like to use a for loop, where I assign a variable to each item in the list but I don't know how. Can anybody help me with that? Cheers

Link to comment
Share on other sites

Link to post
Share on other sites

Using variable variable names doesn't really make sense. How would you keep track of how many variables there are later on? What you want is an array, which you already seem to have since you're using a for loop. Alternatively you could use a dictionary, to give a unique name to each result.

 

Maybe give a bit more details about what your actual goal is? I'm sure someone can come up with a proper solution.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Dictionary, nodes, arrays are much better options.

 

There's no efficient or resourceful way to do what you want

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

re.search only finds the first match or none. It does not represent a collection of matches.

 

We could help you better if you could tell us exactly what you are trying to do.

 

Until that time, however, suffice it to say:

If the language is reflexive, then it's technically possible to iterate over some collection of objects and turn them into uniquely named variables. However, this is the least efficient, most difficult, and hardest to get right way of handling collections of items.

 

There are a bunch of different types of data structures which can let you efficiently access collections of objects using all sorts of patterns. The selection of which data structure to use is dependent on what you actually know about the objects when the collection is created and how you want to use the objects later. Therefore, if you could tell us more specifically what you are trying to do, then we could help you much better.

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×