-1

I have many list containing thousands of line of data. I want to merge those list side by side in a .dat file like this 1::2::F::4::990. Consider these single data as different list I want to merge the data exactly as this. Can you help

I have done this so far.

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))

this is a sample output enter image description here

I want to merge data like this and i have list like user_id, movie_id, ratings, etc each column is a list

this is how my list looks like

These are my sample lists

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']

i have tried this and didn't got what i want

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)

this is my output only one line

1::1246::4::978302091::F::1::1::48067::Toy Story::Animation|Children's|Comedy::1995
Hussnain_Root
  • 33
  • 1
  • 6
  • Please, check [ask]. Post [mre], incl. sample data and expected output. Ask question about specific problem you cannot solve. Right now there is no question here. Chek [Why is "Can someone help me?" not an actual question?](https://meta.stackoverflow.com/q/284236/4046632) – buran Dec 12 '21 at 07:02
  • i have different lists and i want to merge those side by side in a file separated by :: .. list_item1::list_item2::...... like this – Hussnain_Root Dec 12 '21 at 07:04
  • So far you didn't show any lists, nor [mre]. Also the desired output is unclear - why do you have `'\n'`? Also what king of elements you have in these lists - e.g. `join()` will expect `str`. – buran Dec 12 '21 at 07:04
  • Please, don't post images of code, error, output, etc. Copy/paste as text. For the last time - show sample input, expected output, [mre] of your code. – buran Dec 12 '21 at 07:07
  • @buran are you clear now? – Hussnain_Root Dec 12 '21 at 07:22
  • No, it's not clear. Your sample output does not match the sample input. Also, note that different lists have different length. Given the sample input what is expected output? As a side note - `zip()` is built-in function, don't use it as name. – buran Dec 12 '21 at 07:25
  • Also check https://stackoverflow.com/questions/1663807/how-to-iterate-through-two-lists-in-parallel Maybe that is what you are looking for. – buran Dec 12 '21 at 07:27
  • i have tried that but didn't got the expected output – Hussnain_Root Dec 12 '21 at 07:37
  • just add `\n` at the last piece of code. – Faraaz Kurawle Dec 12 '21 at 11:56

1 Answers1

1

In the example you posted there are various issues, the names of the lists and variables, the lack of new line (\n), also you don't need to open the file each time. And please notice that the zip iterator only iterates the least number of elements when the lists are of different lengths. Using your example but fixing the mentioned issues, this works:

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')

output:

1::1246::5::9999::F::1::10::67890::titl1::genre1::1999
2::1247::6::0000::G::1::10::45600::title2::genre2::1990
Jayvee
  • 10,670
  • 3
  • 29
  • 40