Python Forum
Create X Number of Variables and Assign Data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create X Number of Variables and Assign Data
#1
Good morning,

I'm trying to create a number of variables that is dynamic.

Lets say I have a string

sentence = "All work and no play makes Jack a dull boy."

and I count the number of words in the string using a counting scheme of some sort and arrive at a value of 10.

I then want to create 10 variables and store each separate word into a different variable.

I think I could use a function like this:

while i < num_vars:
var_name = ??? - I think I could just loop through numbers and concatenate for a name. Ie.. "var_" concatenated with i , then iterate i
var_value = ??? - Should I use some string split function in python, or assign to an array and use array functions?
exec(var_name + " = " + var_value)

Thanks again for the help and I hope this makes sense.
Reply


Messages In This Thread
Create X Number of Variables and Assign Data - by RockBlok - Nov-12-2023, 02:31 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Better python library to create ER Diagram by using pandas data frames as tables klllmmm 0 1,389 Oct-19-2023, 01:01 PM
Last Post: klllmmm
Question How create programmatically variables ? SpongeB0B 6 1,657 Aug-19-2023, 05:10 AM
Last Post: SpongeB0B
  Delete strings from a list to create a new only number list Dvdscot 8 1,822 May-01-2023, 09:06 PM
Last Post: deanhystad
  Create simple live plot of stock data dram 2 3,062 Jan-27-2023, 04:34 AM
Last Post: CucumberNox
  Create a function for writing to SQL data to csv mg24 4 1,340 Oct-01-2022, 04:30 AM
Last Post: mg24
  Best way to accommodate unknown number of variables? Mark17 9 4,048 May-30-2022, 03:57 AM
Last Post: Skaperen
  Create array of values from 2 variables paulo79 1 1,202 Apr-19-2022, 08:28 PM
Last Post: deanhystad
  How to create 2 dimensional variables in Python? plumberpy 5 2,036 Mar-31-2022, 03:15 AM
Last Post: plumberpy
  Strategy on updating edits back to data table and object variables hammer 0 1,255 Dec-11-2021, 02:58 PM
Last Post: hammer
  How can I assign "multiple variables" to a single "value"? Psycpus 2 1,959 Oct-04-2021, 03:29 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020