How to read variables' names python [closed]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Can anyone help me with how to read variables' names here:
cactus_6 = "values"
cactus_5 = "are"
cactus_4 = "not"
cactus_3 = "relevant"
cactus_2 = "right"
cactus_1 = "now"
I need this to create a list consisting of those variables
python
closed as unclear what you're asking by Red Cricket, atline, GhostCat, Mark Rotteveel, smci Nov 17 '18 at 11:52
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 1 more comment
Can anyone help me with how to read variables' names here:
cactus_6 = "values"
cactus_5 = "are"
cactus_4 = "not"
cactus_3 = "relevant"
cactus_2 = "right"
cactus_1 = "now"
I need this to create a list consisting of those variables
python
closed as unclear what you're asking by Red Cricket, atline, GhostCat, Mark Rotteveel, smci Nov 17 '18 at 11:52
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1
It is best you use a dictionary here. Rather than return all your global variables.
– BernardL
Nov 17 '18 at 3:13
2
What do you mean "to read variables' names"? If one readscactus_6
its variable name iscactus_6
and its value is"values"
.
– Red Cricket
Nov 17 '18 at 3:16
I see I'll use dictionary then.
– Anonymous
Nov 17 '18 at 3:16
1
There is no need to. Its name iscactus_1
.
– Red Cricket
Nov 17 '18 at 3:18
1
I think the intent here is to have key, value pairs. Where you can register keys through a dictionary without manually declaring variables.
– BernardL
Nov 17 '18 at 3:24
|
show 1 more comment
Can anyone help me with how to read variables' names here:
cactus_6 = "values"
cactus_5 = "are"
cactus_4 = "not"
cactus_3 = "relevant"
cactus_2 = "right"
cactus_1 = "now"
I need this to create a list consisting of those variables
python
Can anyone help me with how to read variables' names here:
cactus_6 = "values"
cactus_5 = "are"
cactus_4 = "not"
cactus_3 = "relevant"
cactus_2 = "right"
cactus_1 = "now"
I need this to create a list consisting of those variables
python
python
asked Nov 17 '18 at 3:12
AnonymousAnonymous
307
307
closed as unclear what you're asking by Red Cricket, atline, GhostCat, Mark Rotteveel, smci Nov 17 '18 at 11:52
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by Red Cricket, atline, GhostCat, Mark Rotteveel, smci Nov 17 '18 at 11:52
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1
It is best you use a dictionary here. Rather than return all your global variables.
– BernardL
Nov 17 '18 at 3:13
2
What do you mean "to read variables' names"? If one readscactus_6
its variable name iscactus_6
and its value is"values"
.
– Red Cricket
Nov 17 '18 at 3:16
I see I'll use dictionary then.
– Anonymous
Nov 17 '18 at 3:16
1
There is no need to. Its name iscactus_1
.
– Red Cricket
Nov 17 '18 at 3:18
1
I think the intent here is to have key, value pairs. Where you can register keys through a dictionary without manually declaring variables.
– BernardL
Nov 17 '18 at 3:24
|
show 1 more comment
1
It is best you use a dictionary here. Rather than return all your global variables.
– BernardL
Nov 17 '18 at 3:13
2
What do you mean "to read variables' names"? If one readscactus_6
its variable name iscactus_6
and its value is"values"
.
– Red Cricket
Nov 17 '18 at 3:16
I see I'll use dictionary then.
– Anonymous
Nov 17 '18 at 3:16
1
There is no need to. Its name iscactus_1
.
– Red Cricket
Nov 17 '18 at 3:18
1
I think the intent here is to have key, value pairs. Where you can register keys through a dictionary without manually declaring variables.
– BernardL
Nov 17 '18 at 3:24
1
1
It is best you use a dictionary here. Rather than return all your global variables.
– BernardL
Nov 17 '18 at 3:13
It is best you use a dictionary here. Rather than return all your global variables.
– BernardL
Nov 17 '18 at 3:13
2
2
What do you mean "to read variables' names"? If one reads
cactus_6
its variable name is cactus_6
and its value is "values"
.– Red Cricket
Nov 17 '18 at 3:16
What do you mean "to read variables' names"? If one reads
cactus_6
its variable name is cactus_6
and its value is "values"
.– Red Cricket
Nov 17 '18 at 3:16
I see I'll use dictionary then.
– Anonymous
Nov 17 '18 at 3:16
I see I'll use dictionary then.
– Anonymous
Nov 17 '18 at 3:16
1
1
There is no need to. Its name is
cactus_1
.– Red Cricket
Nov 17 '18 at 3:18
There is no need to. Its name is
cactus_1
.– Red Cricket
Nov 17 '18 at 3:18
1
1
I think the intent here is to have key, value pairs. Where you can register keys through a dictionary without manually declaring variables.
– BernardL
Nov 17 '18 at 3:24
I think the intent here is to have key, value pairs. Where you can register keys through a dictionary without manually declaring variables.
– BernardL
Nov 17 '18 at 3:24
|
show 1 more comment
5 Answers
5
active
oldest
votes
If you're sure this is what you want:
cacti = [value for name, value in vars().items() if name.startswith('cactus_')]
It's probably better just to use a list
:
cacti = ['values', 'are', 'not', 'relevant', 'right', 'now']
or dict
ionary in the first place, though:
{
1: 'values',
2: 'are',
3: 'not',
4: 'relevant',
5: 'right',
6: 'now',
}
add a comment |
I'm a little confused on what you want but i'll give it a shot! You want to create a list consisting of those variables?
If you only have six you could do something like this.
list =
list.append(cactus_1)
list.append(cactus_2)
list.append(cactus_3)
list.append(cactus_4)
list.append(cactus_5)
list.append(cactus_6)
This will give you a list that looks like this:
["values",'are','not','relevant','right','now']
Hope this was helpful!
What I want is to add all variables which have "cactus" in their name to a list without doing a list.append(x) 6 times
– Anonymous
Nov 17 '18 at 3:25
add a comment |
What you really want is a key-value pair, which is what you get with a dictionary structure.
# python
cactus_6 = "values"
cactus_5 = "are"
cactus_4 = "not"
cactus_3 = "relevant"
cactus_2 = "right"
cactus_1 = "now"
Becomes:
my_cactus_values = {
"cactus_6": "values",
"cactus_5": "are",
"cactus_4": "not",
"cactus_3": "relevant",
"cactus_2": "right",
"cactus_1": "now",
}
Then you can loop over them with the items
method.
for name, value in my_cactus_values.items():
print(name, value)
What if I want to access a specific item from the dictionary using an index? It gives out an error if I try that: print(cacti.items()[0]) TypeError: 'dict_items' object does not support indexing
– Anonymous
Nov 17 '18 at 3:35
Well then you use a list. It's not clear from your question what you are actually trying to do.
– Keith
Nov 17 '18 at 3:56
add a comment |
As mentioned, by using a dictionary you can store your keys without having to manually assign them.
The example below provides that:
#creates an example list of your values
values = "values are not relevant right now".split()
#dict comprehension that adds an incremental value to the key name, starting from 1
cactuses = {'cactus{}'.format(idx):val for idx,val in enumerate(values,1)}
cactuses
{'cactus1': 'values',
'cactus2': 'are',
'cactus3': 'not',
'cactus4': 'relevant',
'cactus5': 'right',
'cactus6': 'now'}
By using enumerate here with a start point from 1, it adds incrementally based on the length of items in your list, appends it to your cactus
key and then stores its value through a dictionary comprehension.
add a comment |
Try using eval()
. Something like this …
cactus_6 = "values"
cactus_5 = "are"
cactus_4 = "not"
cactus_3 = "relevant"
cactus_2 = "right"
cactus_1 = "now"
cmd = '[' + ','.join(["cactus_%s" % x for x in range(1,7)]) + ']'
eval(cmd)
['now', 'right', 'relevant', 'not', 'are', 'values']
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you're sure this is what you want:
cacti = [value for name, value in vars().items() if name.startswith('cactus_')]
It's probably better just to use a list
:
cacti = ['values', 'are', 'not', 'relevant', 'right', 'now']
or dict
ionary in the first place, though:
{
1: 'values',
2: 'are',
3: 'not',
4: 'relevant',
5: 'right',
6: 'now',
}
add a comment |
If you're sure this is what you want:
cacti = [value for name, value in vars().items() if name.startswith('cactus_')]
It's probably better just to use a list
:
cacti = ['values', 'are', 'not', 'relevant', 'right', 'now']
or dict
ionary in the first place, though:
{
1: 'values',
2: 'are',
3: 'not',
4: 'relevant',
5: 'right',
6: 'now',
}
add a comment |
If you're sure this is what you want:
cacti = [value for name, value in vars().items() if name.startswith('cactus_')]
It's probably better just to use a list
:
cacti = ['values', 'are', 'not', 'relevant', 'right', 'now']
or dict
ionary in the first place, though:
{
1: 'values',
2: 'are',
3: 'not',
4: 'relevant',
5: 'right',
6: 'now',
}
If you're sure this is what you want:
cacti = [value for name, value in vars().items() if name.startswith('cactus_')]
It's probably better just to use a list
:
cacti = ['values', 'are', 'not', 'relevant', 'right', 'now']
or dict
ionary in the first place, though:
{
1: 'values',
2: 'are',
3: 'not',
4: 'relevant',
5: 'right',
6: 'now',
}
answered Nov 17 '18 at 3:33
Solomon UckoSolomon Ucko
88121122
88121122
add a comment |
add a comment |
I'm a little confused on what you want but i'll give it a shot! You want to create a list consisting of those variables?
If you only have six you could do something like this.
list =
list.append(cactus_1)
list.append(cactus_2)
list.append(cactus_3)
list.append(cactus_4)
list.append(cactus_5)
list.append(cactus_6)
This will give you a list that looks like this:
["values",'are','not','relevant','right','now']
Hope this was helpful!
What I want is to add all variables which have "cactus" in their name to a list without doing a list.append(x) 6 times
– Anonymous
Nov 17 '18 at 3:25
add a comment |
I'm a little confused on what you want but i'll give it a shot! You want to create a list consisting of those variables?
If you only have six you could do something like this.
list =
list.append(cactus_1)
list.append(cactus_2)
list.append(cactus_3)
list.append(cactus_4)
list.append(cactus_5)
list.append(cactus_6)
This will give you a list that looks like this:
["values",'are','not','relevant','right','now']
Hope this was helpful!
What I want is to add all variables which have "cactus" in their name to a list without doing a list.append(x) 6 times
– Anonymous
Nov 17 '18 at 3:25
add a comment |
I'm a little confused on what you want but i'll give it a shot! You want to create a list consisting of those variables?
If you only have six you could do something like this.
list =
list.append(cactus_1)
list.append(cactus_2)
list.append(cactus_3)
list.append(cactus_4)
list.append(cactus_5)
list.append(cactus_6)
This will give you a list that looks like this:
["values",'are','not','relevant','right','now']
Hope this was helpful!
I'm a little confused on what you want but i'll give it a shot! You want to create a list consisting of those variables?
If you only have six you could do something like this.
list =
list.append(cactus_1)
list.append(cactus_2)
list.append(cactus_3)
list.append(cactus_4)
list.append(cactus_5)
list.append(cactus_6)
This will give you a list that looks like this:
["values",'are','not','relevant','right','now']
Hope this was helpful!
answered Nov 17 '18 at 3:19
Luke KellyLuke Kelly
877
877
What I want is to add all variables which have "cactus" in their name to a list without doing a list.append(x) 6 times
– Anonymous
Nov 17 '18 at 3:25
add a comment |
What I want is to add all variables which have "cactus" in their name to a list without doing a list.append(x) 6 times
– Anonymous
Nov 17 '18 at 3:25
What I want is to add all variables which have "cactus" in their name to a list without doing a list.append(x) 6 times
– Anonymous
Nov 17 '18 at 3:25
What I want is to add all variables which have "cactus" in their name to a list without doing a list.append(x) 6 times
– Anonymous
Nov 17 '18 at 3:25
add a comment |
What you really want is a key-value pair, which is what you get with a dictionary structure.
# python
cactus_6 = "values"
cactus_5 = "are"
cactus_4 = "not"
cactus_3 = "relevant"
cactus_2 = "right"
cactus_1 = "now"
Becomes:
my_cactus_values = {
"cactus_6": "values",
"cactus_5": "are",
"cactus_4": "not",
"cactus_3": "relevant",
"cactus_2": "right",
"cactus_1": "now",
}
Then you can loop over them with the items
method.
for name, value in my_cactus_values.items():
print(name, value)
What if I want to access a specific item from the dictionary using an index? It gives out an error if I try that: print(cacti.items()[0]) TypeError: 'dict_items' object does not support indexing
– Anonymous
Nov 17 '18 at 3:35
Well then you use a list. It's not clear from your question what you are actually trying to do.
– Keith
Nov 17 '18 at 3:56
add a comment |
What you really want is a key-value pair, which is what you get with a dictionary structure.
# python
cactus_6 = "values"
cactus_5 = "are"
cactus_4 = "not"
cactus_3 = "relevant"
cactus_2 = "right"
cactus_1 = "now"
Becomes:
my_cactus_values = {
"cactus_6": "values",
"cactus_5": "are",
"cactus_4": "not",
"cactus_3": "relevant",
"cactus_2": "right",
"cactus_1": "now",
}
Then you can loop over them with the items
method.
for name, value in my_cactus_values.items():
print(name, value)
What if I want to access a specific item from the dictionary using an index? It gives out an error if I try that: print(cacti.items()[0]) TypeError: 'dict_items' object does not support indexing
– Anonymous
Nov 17 '18 at 3:35
Well then you use a list. It's not clear from your question what you are actually trying to do.
– Keith
Nov 17 '18 at 3:56
add a comment |
What you really want is a key-value pair, which is what you get with a dictionary structure.
# python
cactus_6 = "values"
cactus_5 = "are"
cactus_4 = "not"
cactus_3 = "relevant"
cactus_2 = "right"
cactus_1 = "now"
Becomes:
my_cactus_values = {
"cactus_6": "values",
"cactus_5": "are",
"cactus_4": "not",
"cactus_3": "relevant",
"cactus_2": "right",
"cactus_1": "now",
}
Then you can loop over them with the items
method.
for name, value in my_cactus_values.items():
print(name, value)
What you really want is a key-value pair, which is what you get with a dictionary structure.
# python
cactus_6 = "values"
cactus_5 = "are"
cactus_4 = "not"
cactus_3 = "relevant"
cactus_2 = "right"
cactus_1 = "now"
Becomes:
my_cactus_values = {
"cactus_6": "values",
"cactus_5": "are",
"cactus_4": "not",
"cactus_3": "relevant",
"cactus_2": "right",
"cactus_1": "now",
}
Then you can loop over them with the items
method.
for name, value in my_cactus_values.items():
print(name, value)
answered Nov 17 '18 at 3:27
KeithKeith
31.6k84362
31.6k84362
What if I want to access a specific item from the dictionary using an index? It gives out an error if I try that: print(cacti.items()[0]) TypeError: 'dict_items' object does not support indexing
– Anonymous
Nov 17 '18 at 3:35
Well then you use a list. It's not clear from your question what you are actually trying to do.
– Keith
Nov 17 '18 at 3:56
add a comment |
What if I want to access a specific item from the dictionary using an index? It gives out an error if I try that: print(cacti.items()[0]) TypeError: 'dict_items' object does not support indexing
– Anonymous
Nov 17 '18 at 3:35
Well then you use a list. It's not clear from your question what you are actually trying to do.
– Keith
Nov 17 '18 at 3:56
What if I want to access a specific item from the dictionary using an index? It gives out an error if I try that: print(cacti.items()[0]) TypeError: 'dict_items' object does not support indexing
– Anonymous
Nov 17 '18 at 3:35
What if I want to access a specific item from the dictionary using an index? It gives out an error if I try that: print(cacti.items()[0]) TypeError: 'dict_items' object does not support indexing
– Anonymous
Nov 17 '18 at 3:35
Well then you use a list. It's not clear from your question what you are actually trying to do.
– Keith
Nov 17 '18 at 3:56
Well then you use a list. It's not clear from your question what you are actually trying to do.
– Keith
Nov 17 '18 at 3:56
add a comment |
As mentioned, by using a dictionary you can store your keys without having to manually assign them.
The example below provides that:
#creates an example list of your values
values = "values are not relevant right now".split()
#dict comprehension that adds an incremental value to the key name, starting from 1
cactuses = {'cactus{}'.format(idx):val for idx,val in enumerate(values,1)}
cactuses
{'cactus1': 'values',
'cactus2': 'are',
'cactus3': 'not',
'cactus4': 'relevant',
'cactus5': 'right',
'cactus6': 'now'}
By using enumerate here with a start point from 1, it adds incrementally based on the length of items in your list, appends it to your cactus
key and then stores its value through a dictionary comprehension.
add a comment |
As mentioned, by using a dictionary you can store your keys without having to manually assign them.
The example below provides that:
#creates an example list of your values
values = "values are not relevant right now".split()
#dict comprehension that adds an incremental value to the key name, starting from 1
cactuses = {'cactus{}'.format(idx):val for idx,val in enumerate(values,1)}
cactuses
{'cactus1': 'values',
'cactus2': 'are',
'cactus3': 'not',
'cactus4': 'relevant',
'cactus5': 'right',
'cactus6': 'now'}
By using enumerate here with a start point from 1, it adds incrementally based on the length of items in your list, appends it to your cactus
key and then stores its value through a dictionary comprehension.
add a comment |
As mentioned, by using a dictionary you can store your keys without having to manually assign them.
The example below provides that:
#creates an example list of your values
values = "values are not relevant right now".split()
#dict comprehension that adds an incremental value to the key name, starting from 1
cactuses = {'cactus{}'.format(idx):val for idx,val in enumerate(values,1)}
cactuses
{'cactus1': 'values',
'cactus2': 'are',
'cactus3': 'not',
'cactus4': 'relevant',
'cactus5': 'right',
'cactus6': 'now'}
By using enumerate here with a start point from 1, it adds incrementally based on the length of items in your list, appends it to your cactus
key and then stores its value through a dictionary comprehension.
As mentioned, by using a dictionary you can store your keys without having to manually assign them.
The example below provides that:
#creates an example list of your values
values = "values are not relevant right now".split()
#dict comprehension that adds an incremental value to the key name, starting from 1
cactuses = {'cactus{}'.format(idx):val for idx,val in enumerate(values,1)}
cactuses
{'cactus1': 'values',
'cactus2': 'are',
'cactus3': 'not',
'cactus4': 'relevant',
'cactus5': 'right',
'cactus6': 'now'}
By using enumerate here with a start point from 1, it adds incrementally based on the length of items in your list, appends it to your cactus
key and then stores its value through a dictionary comprehension.
answered Nov 17 '18 at 3:31
BernardLBernardL
2,42411232
2,42411232
add a comment |
add a comment |
Try using eval()
. Something like this …
cactus_6 = "values"
cactus_5 = "are"
cactus_4 = "not"
cactus_3 = "relevant"
cactus_2 = "right"
cactus_1 = "now"
cmd = '[' + ','.join(["cactus_%s" % x for x in range(1,7)]) + ']'
eval(cmd)
['now', 'right', 'relevant', 'not', 'are', 'values']
add a comment |
Try using eval()
. Something like this …
cactus_6 = "values"
cactus_5 = "are"
cactus_4 = "not"
cactus_3 = "relevant"
cactus_2 = "right"
cactus_1 = "now"
cmd = '[' + ','.join(["cactus_%s" % x for x in range(1,7)]) + ']'
eval(cmd)
['now', 'right', 'relevant', 'not', 'are', 'values']
add a comment |
Try using eval()
. Something like this …
cactus_6 = "values"
cactus_5 = "are"
cactus_4 = "not"
cactus_3 = "relevant"
cactus_2 = "right"
cactus_1 = "now"
cmd = '[' + ','.join(["cactus_%s" % x for x in range(1,7)]) + ']'
eval(cmd)
['now', 'right', 'relevant', 'not', 'are', 'values']
Try using eval()
. Something like this …
cactus_6 = "values"
cactus_5 = "are"
cactus_4 = "not"
cactus_3 = "relevant"
cactus_2 = "right"
cactus_1 = "now"
cmd = '[' + ','.join(["cactus_%s" % x for x in range(1,7)]) + ']'
eval(cmd)
['now', 'right', 'relevant', 'not', 'are', 'values']
answered Nov 17 '18 at 4:53
Red CricketRed Cricket
4,694103390
4,694103390
add a comment |
add a comment |
1
It is best you use a dictionary here. Rather than return all your global variables.
– BernardL
Nov 17 '18 at 3:13
2
What do you mean "to read variables' names"? If one reads
cactus_6
its variable name iscactus_6
and its value is"values"
.– Red Cricket
Nov 17 '18 at 3:16
I see I'll use dictionary then.
– Anonymous
Nov 17 '18 at 3:16
1
There is no need to. Its name is
cactus_1
.– Red Cricket
Nov 17 '18 at 3:18
1
I think the intent here is to have key, value pairs. Where you can register keys through a dictionary without manually declaring variables.
– BernardL
Nov 17 '18 at 3:24