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

北城以北 提交于 2020-04-18 05:35:15

问题


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


回答1:


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)



回答2:


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.




回答3:


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()]



回答4:


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



来源:https://stackoverflow.com/questions/60997190/python-how-can-i-use-an-enumerate-element-as-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!