首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中列表变为非python

在python中列表变为非python
EN

Stack Overflow用户
提问于 2021-04-18 08:00:08
回答 1查看 59关注 0票数 0
代码语言:javascript
复制
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")

这是我的代码,用于在sage中从非常长的数据列表中创建直方图。数据都是像"3600 79 280058“这样的值,然后是频率。除了直方图生成本身之外,一切都运行得很好。我已经尝试过输出列表,它打印得非常好。这是我运行它时的输出:

代码语言:javascript
复制
<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)返回list,那么我在这里遗漏了什么?是什么让它的类型变成了none?

EN

回答 1

Stack Overflow用户

发布于 2021-04-18 08:30:54

使用for使用while的实例,如下所示

代码语言:javascript
复制
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")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67144033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档