我想为绘图的X,Y值解压缩元组,如果新元组中的项由3个组件组成('AXIN',37,'reported'),而不是2(AXIN,37),这会给我一个错误。在
错误表示要解压缩的多个值
new = (('AXIN', 37, 'reported'),
('LGR', 30, 'reported'),
('NK', 24, 'reported'),
('TN', 23, 'reported'),
('CC', 19, 'reported'),
('APC', 18, 'reported'),
('TRD@', 16, 'reported'),
('TOX', 15, 'UNREPORTED'),
('LEF', 15, 'reported'),
('MME', 13, 'reported'),
('NOTUM', 13, 'reported'),
('PLCB', 13, 'UNREPORTED'),
('GN', 11, 'UNREPORTED'),
('LOX', 10, 'UNREPORTED'),
('LOX', 10, 'reported'),
('CRND', 10, 'reported'),
('LRP', 9, 'reported'),
('BMP', 9, 'reported'),
('VSNL', 8, 'UNREPORTED'),
('LOC', 8, 'reported'),
('ZNRF', 8, 'reported'),
('KRT', 8, 'UNREPORTED'),
('CTNN', 8, 'reported'))
X, Y = zip(*new)
import seaborn as sns
sns.set()
import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize = (20, 10))
mytitle = "Most common genes coexpressed with {gene1}, {gene2}, {gene3}, {gene4}".format(gene1="axin2", gene2="lef", gene3="nkd1", gene4="lgr5")
plt.title(mytitle, fontsize=40)
plt.ylabel('Number of same gene encounters across studies', fontsize=20)
ax = plt.bar(range(len(X)), Y, 0.6, align='center', tick_label = X, color="green")
ax = plt.xticks(rotation=90)
new = tuple(new)
import networkx as nx
children = sorted(new, key=lambda x: x[1])
parent = children.pop()[0]
G = nx.Graph()
for child, weight in children: G.add_edge(parent, child, weight=weight)
width = list(nx.get_edge_attributes(G, 'weight').values())
colors = []
for i in new:
if i[2] == 'UNREPORTED':
colors.append('green')
elif i[2] == 'REPORTED':
colors.append('yellow')
nx.draw_networkx(G, font_size=10, node_size=2000, alpha=0.6, node_color=colors)
plt.savefig("plt.gene-expression.pdf")
plt.figure(figsize = (20, 10))
mytitle = "Most common genes coexpressed with {gene1}, {gene2}, {gene3}, {gene4}".format(gene1="axin2", gene2="lef", gene3="nkd1", gene4="lgr5")
plt.title(mytitle, fontsize=40)
nx.draw_networkx(G, font_size=10, node_size=2000, alpha=0.6) #width=width is very fat lines
plt.savefig("gene-expression-graph.pdf")
ValueError: too many values to unpack (expected 2)
确实,有太多的值需要解包。您只给了两个变量来解压,但是每个元组有三个值。试着给它另一个变量:
现在它已经完全打开了。如果不想使用第三个组件,可以给它一个从未使用过的变量名。或者使用
^{pr2}$_
,这在Python中是“nevermind”或“don't care”:我对这个约定的唯一保留意见是,在交互使用中(例如IPython、Jupyter Notebook或stock Python interactive REPL),
_
也意味着“最后产生的值”。这有时与“不关心”的解释相冲突。在顺便说一句,你也需要在你的代码中使用同样的技巧,这同样存在“要解包的项目数量错误”的问题:
这些改变,在排行榜上赫然出现:
相关问题 更多 >
编程相关推荐