简体   繁体   English

Python:如何将枚举元素用作字符串?

[英]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.我有一个正在枚举的 dict1.keys() 列表,我想将该元素用作字符串。

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). https://dbader.org/blog/python-enumerate将枚举实体描述为以下元组:(index, element)。 The type(j) is <class 'str'> , which I can print, but not use as a variable. type(j)<class 'str'> ,我可以打印,但不能用作变量。

EDIT:编辑:

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

EDIT2: I think the problem may be with pandas. EDIT2:我认为问题可能出在熊猫上。 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. EDIT3:第二行确实有效,但仅以一个 df 结束,名称为“k”而不是键。 But heres what Im trying to.但继承人什么林试图。 Each dict converted to a df using its key name:每个 dict 使用其键名转换为 df:

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. EDIT4:根据下面的评论,我切换到键上的 for 循环(不需要显式调用),但它仍然不会使用元素 'i' 作为变量。 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.实际上,这类似于 Sheri 对列表的回答,但名称保留与 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.因为您是在 for 循环内动态生成 Pandas 数据帧,所以在最后打印j它会显示最后生成的数据帧。 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 ).实际上j将是str (或您在dict中使用的任何其他类型的键)。

The actual problem is with the loop body, as the error message states:实际问题出在循环体上,如错误消息所述:

str(j) = somethingElse

is not valid Python.不是有效的 Python。 The left hand side is a call to the str function, so you cannot assign a value to it.左侧是对str函数的调用,因此您不能为其赋值。

Based on the comments you want neither enumerate nor to iterate over the dict keys.根据您既不想enumerate也不想遍历 dict 键的注释。 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 :但是,这通常会在 Python 中没有显式循环的情况下编写,例如使用列表理解

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.实际上,这类似于 Sheri 对列表的回答,但名称保留与 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 使用列表中的元素作为数据框名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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