简体   繁体   English

将列表名称转换为字符串

[英]Turn name of a list into string

I have several lists that I want to write a function and use loop to make multiple heatmaps out of those lists.我有几个列表,我想编写一个 function 并使用循环从这些列表中制作多个热图。 Then I want to name each heatmap by the name of the list.然后我想用列表的名称来命名每个热图。 How can I turn the list name into string?如何将列表名称转换为字符串?

list1 = [[1,2,3], [4,5,6], [4,5,6], [4,5,6]]
list2 = [[2,2,3], [1,5,1], [4,2,3], [3,3,6]]
data = (list1, list2)
def hm(df):
   sns.set_style("whitegrid")
   ax = sns.heatmap(df, cmap=cmap)
   plt.title('df')
   return fig
for l in data:
   hm(1)
     

In a normal case, you cannot retroactively retrieve the name of a variable from it's value.在正常情况下,您不能从变量的值中追溯检索变量的名称。 There might be some hacky ways around this, but I would recommend to just use a dictionary to retain the lists name:可能有一些解决这个问题的方法,但我建议只使用字典来保留列表名称:

list1 = [[1,2,3], [4,5,6], [4,5,6], [4,5,6]]
list2 = [[2,2,3], [1,5,1], [4,2,3], [3,3,6]]
data = {
    "list1": list1,
    "list2": list2,
}

def hm(name, df):
   sns.set_style("whitegrid")
   ax = sns.heatmap(df, cmap=cmap)
   plt.title('df')
   # Do something with the name variable
   return fig
   
for name, l in data.items():
    hm(name, l)

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

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