简体   繁体   English

如何根据在python“ for循环”迭代期间获得的“元组列表”在csv文件中写入多列

[英]How to write multiple columns in a csv file according to “Lists of tuples” obtained during “for loop” iteration in python

With due respect to the previous responses, I am going to completely change my question. 在适当考虑以前的答复的情况下,我将完全改变我的问题。 I am generating lists of tuples as below. 我正在生成元组列表,如下所示。

for i in range(5):

TotalDistance = 0   # particle i starts moving from 0
TotalTime = 0 # particle i starts moving at time 0
driftpoints =[(0,0)]
while TotalDistance < 5.0:
    time = random.uniform(0,1) # paritcle takes time to move to next position
    distance = random.uniform(0,1) # particle moves by distance.
    TotalTime = TotalTime + time
    TotalDistance = TotalDistance + distance
    position = (TotalTime, TotalDistance)
    driftpoints.append(position)

An example list for first iteration is given below. 下面给出了第一次迭代的示例列表。

[(0, 0), (0.21724544874575513, 0.754467286127031), (0.25007307998158623, 1.118356895500405), (0.7047856454945854, 1.4755146942363875), (1.3710776008226833, 2.16401542582095), (1.9942383846177156, 2.9751487045440026), (2.707031044871571, 3.9578284975759295), (3.3278895170648877, 4.831285527860187), (4.000180863917544, 5.218308572399064)]

If it was a single list, I can save in csv file in the following format. 如果是单个列表,则可以以下格式保存在csv文件中。

Time,                Position,
0,                   0,
0.21724544874575513, 0.754467286127031, 
0.25007307998158623, 1.118356895500405,
0.7047856454945854,  1.4755146942363875,
1.3710776008226833,  2.16401542582095, 
1.9942383846177156,  2.9751487045440026,
2.707031044871571,   3.9578284975759295,
3.3278895170648877,  4.831285527860187, 
4.000180863917544,   5.218308572399064

But I have more iterations to come. 但是我还有更多的迭代。 And the problem I am facing is adding columns for next particles. 我面临的问题是为下一个粒子添加列。 How do I save 5 pairs of columns for each particles in a single csv file? 如何在单个csv文件中为每个粒子保存5对列? remember that the length of the columns can differ significantly because of the random numbers taken as the time and distance. 请记住,由于随机数被视为时间和距离,因此列的长度可能会显着不同。

Please forgive me and redirect me to the solution if this or similar question has been already answered. 如果已经回答了这个问题或类似问题,请原谅我,并将我重定向到解决方案。

Okay, I did not get any response on my question except the first one after which I edited my question and the response was deleted. 好的,除了第一个问题之后,我没有得到任何答复,之后我编辑了问题并删除了答复。

Any way, I got it the following way. 无论如何,我是通过以下方式获得的。

transposed = []
 __________________
[for i in range(5):]
[  all             ]
[      my          ]
[        previous  ]
[____________code__]


    transposed=transposed+zip(*driftpoints)
FinalFile=list(itertools.izip_longest(*transposed))

The first line after my previous code transposed the lists created in each iteration and added to make a long list of tuples that look like 我之前的代码后的第一行将在每次迭代中创建的列表进行转置,并添加到列表中,以形成一长串如下所示的元组

transposed = [(0, t1, t2,...t5),(0, x1, x2,...x5), ... (0, t1, t2,...t8),(0,x1, x2,...x8)]

The last code then re-transposed the list created so that each iteration appear in the next set of columns. 然后,最后的代码重新转换创建的列表,以便每次迭代都出现在下一组列中。 Unlike zip(*transposed), it is not limited to the shortest length of the columns. 与zip(* transposed)不同,它不限于列的最短长度。

Finally, write the FinalFile to csv file as 最后,将FinalFile写入csv文件,如下所示:

with open('file.csv','wb') as out:
    csv_out=csv.writer(out)
    for row in Finalfile:
        csv_out.writerow(row)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM