【问题标题】:List becoming nonetype in python列表在python中变成非类型
【发布时间】:2021-07-12 14:09:36
【问题描述】:
x = (long list of data)
mymap = map(int, x.split())
box = []
mylist = list(mymap)
while len(mylist)>0:
    box.append([str(mylist[1])]*mylist[0])
    mylist = mylist[2:]
box.sort()
print(type(box))
type(box)
p=sns.displot(data = box)
p.set(xlabel = "Waiting time", ylabel = "Eruptions")

这是我从极长的数据列表中创建鼠尾草直方图的代码。数据都像“3600 79 2800 58”等,有值,然后是频率。除了直方图生成本身之外,一切都运行良好。我已经尝试过输出列表,它打印得非常好。 这是我运行它时的输出:

<class 'list'>
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-26-87f016146a7f> in <module>
      9 print(type(box))
     10 type(box)
---> 11 p=sns.displot(data = box)
     12 p.set(xlabel = "Waiting time", ylabel = "Eruptions")
/usr/local/lib/python3.8/dist-packages/seaborn/distributions.py in displot(data, x, y, hue, row, col, weights, kind, rug, rug_kws, log_scale, legend, palette, hue_order, hue_norm, color, col_wrap, row_order, col_order, height, aspect, facet_kws, **kwargs)
   2225 
   2226             _assign_default_kwargs(hist_kws, p.plot_univariate_histogram, histplot)
-> 2227             p.plot_univariate_histogram(**hist_kws)
   2228 
   2229         else:
/usr/local/lib/python3.8/dist-packages/seaborn/distributions.py in plot_univariate_histogram(self, multiple, element, fill, common_norm, common_bins, shrink, kde, kde_kws, color, legend, line_kws, estimate_kws, **plot_kws)
    422 
    423         # First pass through the data to compute the histograms
--> 424         for sub_vars, sub_data in self.iter_data("hue", from_comp_data=True):
    425 
    426             # Prepare the relevant data
/usr/local/lib/python3.8/dist-packages/seaborn/_core.py in iter_data(self, grouping_vars, reverse, from_comp_data)
    994                 grouping_keys.append(self.var_levels.get(var, []))
    995 
--> 996             iter_keys = itertools.product(*grouping_keys)
    997             if reverse:
    998                 iter_keys = reversed(list(iter_keys))
TypeError: 'NoneType' object is not iterable

显然 box 是一个列表,因为 type(box) 返回列表,所以我在这里缺少什么?是什么让它的类型变成了 none?

【问题讨论】:

    标签: python list typeerror nonetype sage


    【解决方案1】:

    建议使用while 使用for,类似这样的

    x = (long list of data)
    mymap = map(int, x.split())
    box = []
    mylist = list(mymap)
    for i in range (len(mylist)):
        box.insert(i, str(mylist[1] *mylist[0]) # or append
        mylist = mylist[2:]
    box.sort()
    print(type(box))
    type(box)
    p=sns.displot(data = box)
    p.set(xlabel = "Waiting time", ylabel = "Eruptions")
    

    【讨论】:

    • 现在它给了我 mylist = mylist[2:] ^ SyntaxError: invalid syntax that no sense
    猜你喜欢
    • 1970-01-01
    • 2021-06-14
    • 2020-09-14
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 1970-01-01
    相关资源
    最近更新 更多