【问题标题】:How to merge lists in a .dat file in python如何在python中合并.dat文件中的列表
【发布时间】:2022-01-16 03:42:02
【问题描述】:

我有很多包含数千行数据的列表。我想将这些列表并排合并到一个像1::2::F::4::990 这样的.dat 文件中。将这些单个数据视为不同的列表,我想完全按照此合并数据。你能帮忙吗

到目前为止,我已经这样做了。

with open("mergefile.dat", "w") as outfile:
    outfile.write("\n".join(users_id)+'::'+'\n'.join(movie_id)+'::'+'\n'.join(ratings)+'::'+'\n'.join(timestamps)+'::'+'\n'.join(genders)+'::'+'\n'.join(age)+'::'+'\n'.join(occupation)+'::'+'\n'.join(zipp)+'::'+'\n'.join(moviename)+'::'+'\n'.join(yearOfMovie))

这是一个示例输出

我想合并这样的数据,我有 user_id、movie_id、评级等列表,每一列都是一个列表

这就是我的列表的样子

这些是我的示例列表

user_id=['1','2', '3','4', '5','6','7']
movie_id=['1246','1247', '2081', '1240', '714']
ratings=['5','6','2','7']
timestamp=['9999','0000','5555','2222','1111']
gender=['F','G','F','F','G']
age=['1','1','2']
occupation=['10','10','10','10','10','10']
zip=['67890','45600']
title=['titl1','title2']
genres=['genre1','genre2','genre3']
year=['1999','1990']

我试过了,但没有得到我想要的

for user,movie,rating,timestamp,gender,age,occupations,zipps,title,genre,year in zip(users_id,movies_id,ratings,timestamps,genders,age,occupation,zipp,moviename,genres,yearOfMovie):
    with open("mergefile1.dat", "w") as outfile:
        outfile.write(user+'::'+movie+'::'+rating+'::'+timestamp+'::'+gender+'::'+age+'::'+occupations+'::'+zipps+'::'+title+'::'+genre+'::'+year)

这是我的输出只有一行

1::1246::4::978302091::F::1::1::48067::Toy Story::Animation|Children's|Comedy::1995

【问题讨论】:

  • 请检查How to Ask。发布minimal reproducible example,包括。样本数据和预期输出。询问您无法解决的具体问题。现在这里没有问题。赤Why is "Can someone help me?" not an actual question?
  • 我有不同的列表,我想将它们并排合并到一个由 :: .. list_item1::list_item2::...... 分隔的文件中
  • 到目前为止,您没有显示任何列表,也没有显示minimal reproducible example。所需的输出也不清楚 - 为什么你有'\n'?还有你在这些列表中的元素之王 - 例如join() 将期待 str
  • 请不要发布代码、错误、输出等的图像。复制/粘贴为文本。最后一次 - 显示示例输入、预期输出、minimal reproducible example 您的代码。
  • @buran 你现在清楚了吗?

标签: python list file


【解决方案1】:

在您发布的示例中存在各种问题,列表和变量的名称,缺少换行符(\n),您也不需要每次都打开文件。请注意,当列表长度不同时,zip 迭代器只迭代最少数量的元素。 使用您的示例但解决上述问题,这可行:

user_ids=['1','2', '3','4', '5','6','7']
movie_ids=['1246','1247', '2081', '1240', '714']
ratings=['5','6','2','7']
timestamps=['9999','0000','5555','2222','1111']
genders=['F','G','F','F','G']
ages=['1','1','2']
occupations=['10','10','10','10','10','10']
zipps=['67890','45600']
titles=['titl1','title2']
genres=['genre1','genre2','genre3']
yearOfMovie=['1999','1990']

with open("mergefile1.dat", "w") as outfile:
    for user,movie,rating,timestamp,gender,age,occupation,zipp,title,genre,yearof in zip(user_ids,movie_ids,ratings,timestamps,genders,ages,occupations,zipps,titles,genres,yearOfMovie):
        outfile.write(user+'::'+movie+'::'+rating+'::'+timestamp+'::'+gender+'::'+age+'::'+occupation+'::'+zipp+'::'+title+'::'+genre+'::'+yearof+'\n')

输出:

1::1246::5::9999::F::1::10::67890::titl1::genre1::1999
2::1247::6::0000::G::1::10::45600::title2::genre2::1990

【讨论】:

  • 是的,这只是一个示例列表,适用于我的数据。无论如何感谢您的评论
猜你喜欢
  • 2018-08-26
  • 2021-04-08
  • 1970-01-01
  • 1970-01-01
  • 2016-12-09
  • 1970-01-01
  • 2020-08-04
  • 2021-12-09
  • 2020-05-04
相关资源
最近更新 更多