Python,将两个具有公共列的列表输出到文件(TXT或CSV)中

考虑一下我有两个清单


第1个人:


    2012-08      person 1             23

    2012-09      person 1             63 

    2012-10      person 1             99  

    2012-11      person 1             62 


第2人:


    2012-08      person 2             45

    2012-09      person 2             69 

    2012-10      person 2             12  

    2012-11      person 2             53 

如果我想使用以下格式的表格数据,您有何建议:


Date        Person 1       Person 2

-----       ---------      ---------

2012-08      23             45

2012-09      63             69 

2012-10      99             12  

2012-11      62             53 

更新:


这是清单:


List1 = [(u'201206', u'Customer_1', 0.19048299999999993), (u'201207', u'Customer_1', 15.409000999998593), (u'201208', u'Customer_1', 71.1695730000299), (u'201209', u'Customer_1', 135.73918600011424), (u'201210', u'Customer_1', 235.26299999991522), (u'201211', u'Customer_1', 271.768984999485), (u'201212', u'Customer_1', 355.90968299883934), (u'201301', u'Customer_1', 508.39194049821526), (u'201302', u'Customer_1', 631.136656500077), (u'201303', u'Customer_1', 901.9127695088399), (u'201304', u'Customer_1', 951.9143960094264)]


List 2 = [(None, None, None), (None, None, None), (None, None, None), (None, None, None), (None, None, None), (None, None, None), (None, None, None), (u'201301', u'Customer_2', 3.7276289999999657), (u'201302', u'Customer_2', 25.39122749999623), (u'201303', u'Customer_2', 186.77777299985306), (u'201304', u'Customer_2', 387.97834699805617)]


天涯尽头无女友
浏览 322回答 3
3回答

Smart猫小萌

用于在处理时itertools.izip()合并两个输入序列:import itertoolsreader1 = csv.reader(file1)reader2 = csv.reader(file2)for row1, row2 in itertools.izip(reader1, reader2):    # process row1 and row2 together.这也适用于列表。izip()使长列表合并变得高效;它是的迭代版本zip()功能,其中,在Python 2,物化在存储器中的整个组合列表。如果可以重新构建将输入列表创建到生成器中的函数,请使用:def function_for_list1(inputfilename):    with open(inputfilename, 'rb') as f:        reader = csv.reader(f)        for row in reader:            # process row            yield rowdef function_for_list2(inputfilename):    with open(inputfilename, 'rb') as f:        reader = csv.reader(f)        for row in reader:            # process row            yield rowfor row1, row2 in itertools.izip(function_for_list1(somename), function_for_list2(someothername)):    # process row1 and row2 together这种安排使您可以处理千兆字节的信息,而仅将处理少量行所需的信息保存在内存中。

繁花如伊

如果不需要Python,并且两个CSV文件的生成是在普通的旧bash脚本中进行的,则可以将join和awk(甚至cut)组合在一起。例子:假设此文件称为one:2012-08 person1 232012-09 person1 63 2012-10 person1 99  2012-11 person1 62 该文件称为two:2012-08 person2 452012-09 person2 69 2012-10 person2 12  2012-11 person2 53 然后命令join one two | awk '{print $1 " " $3 " " $5}'将输出:2012-08 23 452012-09 63 692012-10 99 122012-11 62 53在输出中放置CSV标头或选择其他定界符并不难。请注意,必须注意的是,必须在连接列上对这两个文件进行排序才能起作用。但是您可能已经知道这一点,因为您说这两个CSV文件很大。因此,您可能不想一次将它们全部读入内存。普通的Unix工具真的很适合这类事情,恕我直言。

至尊宝的传说

l1=[ ['2012-08','person 1',23], ['2012-09','person 1',63],&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ['2012-10','person 1',99], ['2012-11','person 1',62]]l2=[ ['2012-08','person 2',45], ['2012-09','person 2',69],['2012-10','person 2',12], ['2012-11','person 2',53]]h1 = { x:z for x,y,z in l1}h2 = { x:z for x,y,z in l2}print "{:<10}{:<10}{:<10}".format("Date", "Person 1", "Person 2")print "{:<10}{:<10}{:<10}".format('-'*5, '-'*8, '-'*8)for d in sorted(h1): print "{:<10} {:<10}{:<10}".format(d,h1[d],h2[d])输出Date&nbsp; &nbsp; &nbsp; Person 1&nbsp; Person 2&nbsp;&nbsp;-----&nbsp; &nbsp; &nbsp;--------&nbsp; --------&nbsp;&nbsp;2012-08&nbsp; &nbsp; 23&nbsp; &nbsp; &nbsp; &nbsp; 45&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;2012-09&nbsp; &nbsp; 63&nbsp; &nbsp; &nbsp; &nbsp; 69&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;2012-10&nbsp; &nbsp; 99&nbsp; &nbsp; &nbsp; &nbsp; 12&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;2012-11&nbsp; &nbsp; 62&nbsp; &nbsp; &nbsp; &nbsp; 53&nbsp; &nbsp; &nbsp;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python