遇到TypeType错误:只能将元组(不是“列表”)连接到元组

时间:2019-04-21 22:02:36

标签: python jupyter networkx data-science

我正在按照某个程序的教程来可视化Facebook连接,但我不断收到“ TypeError:只能将元组(而不是“列表”)连接到元组”,我不知道如何解决此问题

colors = cl.scales['12']['qual']['Paired']
def scatter_nodes(pos, labels=None, color='rgb(152, 0, 0)', size=8, opacity=1):

    trace = Scatter(x=[], 
                    y=[],  
                    mode='markers', 
                    marker=Marker(
        showscale=False,
        colorscale='Greens',
        reversescale=True,
        color=[], 
        size=10,
    line=dict(width=0)))
    for nd in nodeID:
        trace['x'].append(pos[nd][0])
        trace['y'].append(pos[nd][1])
        color = colors[part[nd] % len(colors)]
        trace['marker']['color'].append(color)
    attrib=dict(name='', text=labels , hoverinfo='text', opacity=opacity) # a dict of Plotly node attributes
    trace=dict(trace, **attrib)# concatenate the dict trace and attrib
    trace['marker']['size']=size

    return trace 

def scatter_edges(G, pos, line_color='#a3a3c2', line_width=1, opacity=.2):
    trace = Scatter(x=[], 
                    y=[], 
                    mode='lines'
                   )
    for edge in G.edges():
        trace['x'] += [pos[edge[0]][0],pos[edge[1]][0], None]
        trace['y'] += [pos[edge[0]][1],pos[edge[1]][1], None]  
        trace['hoverinfo']='none'
        trace['line']['width']=line_width
        if line_color is not None: # when it is None a default Plotly color is used
            trace['line']['color']=line_color
    return trace          

labels = []

for nd in nodeID:
      labels.append('{} ({})'.format(nd, part[nd],))

trace1 = scatter_edges(G, pos, line_width=0.25)
trace2 = scatter_nodes(pos, labels=labels)

我尝试将其更改为括号,但这也不起作用。

错误声明

 TypeError                                 Traceback (most recent call last)
<ipython-input-26-7db6d16fa847> in <module>
----> 1 trace1 = scatter_edges(G, pos, line_width=0.25)
      2 trace2 = scatter_nodes(pos, labels=labels)

<ipython-input-23-0e8ff4c52f3c> in scatter_edges(G, pos, line_color, line_width, opacity)
      5                    )
      6     for edge in G.edges():
----> 7         trace['x'] += [pos[edge[0]][0],pos[edge[1]][0], None]
      8         trace['y'] += [pos[edge[0]][1],pos[edge[1]][1], None]
      9         trace['hoverinfo']='none'

TypeError: can only concatenate tuple (not "list") to tuple

1 个答案:

答案 0 :(得分:1)

trace['x']是元组

[pos[edge[0]][0],pos[edge[1]][0], None]是一个列表,因此错误

您不能将列表直接串联成元组。但是您可以将数据转换/调整为相同类型。在一个方向或另一个方向上。例如, 您可以1)从元组构建列表2)然后连接另一个列表3)将结果转换为元组