简体   繁体   中英

Python: How can I use an enumerate element as a string?

I have a list of dict1.keys() I'm enumerating over and I'd like to use the element as a string.

for i,j in enumerate(dict1.keys()): str(j) = somethingElse
>>> SyntaxError: can't assign to function call

https://dbader.org/blog/python-enumerate describes the enumerate entities as a tuple of: (index, element). The type(j) is <class 'str'> , which I can print, but not use as a variable.

EDIT:

for i,j in enumerate(dict1.keys()): j = somethingElse

EDIT2: I think the problem may be with pandas. The first line works, not the second.

for i,j in enumerate(dict1.keys()): list1.append(j)
for i,k in enumerate(list1): k = pd.DataFrame(dict1[k]['Values'])

EDIT3: That second line does work, but only for only ends up with one df, with name 'k' instead of the key. But heres what Im trying to. Each dict converted to a df using its key name:

for i,j in enumerate(dict1.keys()): j = pd.DataFrame(dict1[j]['Values'])

EDIT4: According to the comments below, I switched to a for loop on the keys (which dont need to be explicitly called), but it still won't use the element 'i' as a variable. However, from the question linked below, elements are able to be used as a key in a dict. After reducing the question to "use list item as name for dataframe" and searching that, it verks. I'll post as an answer also:

dict2={}
for i in dict1: dict2[i] = pd.DataFrame(dict1[i]['Values'])

..thus the names are preserved. Actually, this is similar to Sheri's answer with lists, but the names retain association with the dfs. There may not be a way to set a variable value using something other than a plain string, but I'll start a different question for that.

use elements in a list for dataframe names

Because you are generating your pandas dataframe dynamically inside a for loop so at the end when you print j it will show you the last generated dataframe. You should store your dataframe in list Try using this:

listOfFrame = [] 
for j in dict.keys(): 
    j = pd.DataFrame(dict[j]['Values'])
    listOfFrame.append(j)

Indeed j will be a str (or whatever else type of key you are using in dict ).

The actual problem is with the loop body, as the error message states:

str(j) = somethingElse

is not valid Python. The left hand side is a call to the str function, so you cannot assign a value to it.

Based on the comments you want neither enumerate nor to iterate over the dict keys. Instead, you want to iterate over its values :

dfs = []
for val in dict1.values():
    dfs.append(pd.DataFrame(val['Values']))

However, this would normally written without an explicit loop in Python, for instance by using list comprehension :

dfs = [pd.DataFrame(val['Values']) for val in dict1.values()]

From the question linked below, elements are able to be used as a key in a dict. After reducing the question to "use list item as name for dataframe" and searching that, it verks. I'll post as an answer also:

dict2={}
for i in dict1: dict2[i] = pd.DataFrame(dict1[i]['Values'])

..thus the names are preserved. Actually, this is similar to Sheri's answer with lists, but the names retain association with the dfs. There may not be a way to set a variable value using something other than a plain string, but I'll start a different question for that.

use elements in a list for dataframe names

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM