将多个列合并到矩阵python中

2024-04-25 16:52:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python中有几个列最终要合并。现在我使用的是zip函数,但是在合并了列之后,我将它们输出为CSV格式,格式很糟糕(不是保持列分开,而是在添加新列时合并列)。下面是一个例子:

col1 = [text1, text2, text3, etc]
col2 = [str1, str2, str3, etc.]
col3 = [num1, num2, num3, etc.]

我想把它们组合成这样:

^{pr2}$

从那里,我将能够输出的矩阵作为一个csv,其中的列都是分开的。现在看起来像这样:

我的代码

file_weights = similarity[0]
file_ranking = zip(filtered_and_cleaned, case_id)[1:]
file_ranking_2 = zip(file_ranking, file_weights)[1:]

输出

[[text1 str1, num1],
 [text2 str2, num2],
 [text3 str3, num3]]

我明白为什么我所做的不起作用,我只是不知道该怎么解决它。在


Tags: 格式etczipfileweightsrankingtext1str1
1条回答
网友
1楼 · 发布于 2024-04-25 16:52:48

一个指向列列表的指针的^{}应该可以做到这一点!在

col1 = [text1, text2, text3]
col2 = [str1, str2, str3]
col3 = [num1, num2, num3]
print zip(*[col1,col2,col3])

在Python中,3^{}返回迭代器。因此,结果的list将给出所需的输出!。在

也就是说

^{pr2}$

输出格式如下所示!公司名称:

[[text1, str1, num1],
 [text2, str2, num2],
 [text3, str3, num3]]

希望有帮助。在

编码快乐!在

相关问题 更多 >