简体   繁体   English

获取NoneType值“ None”的列表

[英]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列表也应变化。

  1. Solution: List of Strings, List comprehension: 解决方案:字符串列表,列表理解:

    lst =', '.join([str(None) for ticker in ew])

    Problem: Strings - convert to Nonetype possible? 问题:字符串-可能转换为Nonetype吗?

  2. 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.

相关问题 从列中的字典获取值。 AttributeError: 'NoneType' object 在值为“None”时没有属性“get” - Get values from a dict in an column. AttributeError: 'NoneType' object has no attribute 'get' when a value is "None" 如何在推文中不包含任何值的情况下获取列表中的所有迭代? - How to get all the iterations in a list with none values included from a tweet? 如何摆脱列表中的 None 值? Python - How do I get rid of None values in list? Python Python: 获取`TypeError: "NoneType" object is not iterable` with no None elements of list - Python: Getting `TypeError: "NoneType" object is not iterable` with no None elements of list 列表理解不是无 AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;group&#39; - List Comprehension is not None AttributeError: 'NoneType' object has no attribute 'group' 使用 None 值对 python 中的列表进行排名 - Ranking a list in python with None values 我可以得到1或3个“无”值,但不能得到2个 - I can get 1 or 3 'None' values, but not 2 获取值不为 None 的范围 - Get ranges where values are not None 将列表列表转换为字符串会给出“无”值 - converting list of list to a string gives “none” values 将列表中的 None 值更改为列表中的先前元素 - Changing None values in a list to previous elements in the list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM