[英]get list of NoneType values 'None'
I am new to Python. 我是Python的新手。 I'm trying to get a list of NoneType values to create a subplot (plotly library). 我正在尝试获取NoneType值的列表以创建子图(图库)。 The following setup is required to create subplots with different specs: 需要以下设置才能创建具有不同规格的子图:
fig = tools.make_subplots(rows=2, cols=3, specs=[ [{'colspan':3}, None, None],
map(lambda x: {}, ew) ],
shared_xaxes=False, shared_yaxes=False,
start_cell='top-left', print_grid=False)
So, based on the values in the list "ew", a list of Nonetype values is needed. 因此,基于列表“ ew”中的值,需要一个Nonetype值列表。 The values in the list can vary and so should the list of Nonetypes. 列表中的值可以变化,并且Nonetypes列表也应变化。
Solution: List of Strings, List comprehension: 解决方案:字符串列表,列表理解:
lst =', '.join([str(None) for ticker in ew])
Problem: Strings - convert to Nonetype possible? 问题:字符串-可能转换为Nonetype吗?
Solution: Lambda function that inserts None for each value in ew. 解决方案:Lambda函数为ew中的每个值插入None。
map(lambda x: None, ew)
Problem: Brackets of the list. 问题:列表中的括号。 Can't get rid of them. 无法摆脱它们。
The Solution I am looking for: 我正在寻找的解决方案:
print(lst)
None, None
<type 'NoneType'>
so that: 以便:
fig = tools.make_subplots(rows=2, cols=3, specs=[ [{'colspan':3}, lst],
map(lambda x: {}, ew) ],
shared_xaxes=False, shared_yaxes=False,
start_cell='top-left', print_grid=False)
Is there a way to get such a list? 有没有办法得到这样的清单? or a better solution over a embedded function? 还是比嵌入式功能更好的解决方案?
EDIT since there is still an error by inserting the following 'lst' to fig: 编辑,因为通过向图插入以下“ lst”仍然存在错误:
lst = print(*map(lambda x: None, ew), sep= ', ') #returns None, None
print('{lst}'.format(**locals())) #returns only None
-> is this a possible explanation? ->这可能是解释吗?
You can just create a list and then remove the brackets later. 您可以只创建一个列表,然后在以后删除括号。
a = [None, None, None, None, None] # an example of a list you might want
and you can print it without the brackets like : 您可以在不使用方括号的情况下进行打印:
print str(a)[1:-1]
If you're using Python 3.x you can try this to print a list without the brackets even if there's a NoneType data included: 如果您使用的是Python 3.x,则即使没有NoneType数据,也可以尝试打印不带括号的列表:
print (*lst, sep=', ') #lst = [None, None]
It should output: 它应该输出:
None, None
If you're using Python 2.x you can also do this by using from __future__ import print_function
如果您使用的是Python 2.x,也可以使用from __future__ import print_function
Found an answer for my question: Make one whole list with the dictionary at the first place. 找到了我的问题的答案:首先列出完整的词典列表。
tr = []
for ticker in ew:
if ew.index(ticker) ==0:
tr.append({'colspan': len(ew)})
else:
tr.append(None)
into fig: 成无花果:
fig = tools.make_subplots(rows=2, cols=3, specs=[ tr, map(lambda x: {},ew)],
shared_xaxes=False, shared_yaxes=False,
start_cell='top-left', print_grid=False)
more pythonic solutions are welcome. 欢迎使用更多pythonic解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.