Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I would like to convert an integer to an array name in python. I have no idea whether this is even possible. Help is highly appreciated.

a = int(10)
print a["1"]

Should return >> 10["1"]
share|improve this question

closed as unclear what you're asking by Padraic Cunningham, Tom Fenech, vaultah, Lukas Graf, dawg Jul 27 '14 at 17:59

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.

2  
What are you trying to accomplish here? –  inspectorG4dget Jul 27 '14 at 17:47
2  
It's totally unclear what you're trying to accomplish, please clarify. Also, Python doesn't have arrays as such, it has lists and dictionaries - which one are you trying to use? –  Lukas Graf Jul 27 '14 at 17:53
1  
I agree that it's unclear as to what you're trying to accomplish, OP. Does "Should return >> 10["1"]" mean that "10["1"]" should be printed, literally? Would you like to access the value at key "1" in the dictionary 10? I've read what you've written, and I feel like I half-see what you're getting at, but it's sort of nonsensical. –  David Frye Jul 27 '14 at 17:56
2  
As Lukas mentioned, straight-up "arrays" aren't really a thing in Python. But if you're simply trying to associate a list/dictionary/whatever with a number, that's totally doable in the sense that you can create a dictionary with integer keys and list/dictionary/whatever values. –  David Frye Jul 27 '14 at 17:59
1  
@PhilippBraun: I think you're conflating different languages here. Python doesn't support arrays like this, in the C sense. I mean, there's numpy, but it's used in the "python-way" not the "C-way". Also, python doesn't support using integers as variable names. Why don't you start from the beginning on what problem you're trying to solve? –  Noob Saibot Jul 27 '14 at 18:01

2 Answers 2

up vote 2 down vote accepted

I don't think even eval would do that (though you wouldn't want to do that anyway). Your best bet is to use a dict, which can make an integer into a key.

int_dict = {}
int_dict[10] = ["1"]

Though I admit your use case is a little confusing. You may want different list contents.

share|improve this answer

A variable name can't start with a number. How would you evaluate 10=10?

share|improve this answer
    
is it possible to rename it to like "week"+a –  Philipp Braun Jul 27 '14 at 17:55
1  
This is a comment, not an answer. –  miR Jul 27 '14 at 17:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.