如何比较两个列表列表,然后在python

时间:2017-12-21 13:19:45

标签: python excel list csv export-to-csv

我在list of lists中有两个python,如下所示:

list1的:

[['10001', 'Jason', 'University of Utah School of Medicine'],
 ['10007', 'Zhang', 'Duke'],
 ['10016', 'Som', 'Dana-Farber']]

列表2:

[['Som', 'mrcc', 'Wed Dec 20 18:36:28 +0000 2017', '10016'],
 ['David', 'mrcc', 'Wed Dec 20 18:36:38 +0000 2017', '10013'],
 ['Kristen', 'mrcc gro', 'Wed Dec 20 18:37:24 +0000 2017', '10045'],
 ['Zhang', 'wan mrcc', 'Wed Dec 20 18:38:44 +0000 2017', '10007']]

现在,我想根据比较这两个excel file之间的数据,将数据写入list of lists。我的excel文件的结构如下:enter image description here

为了进行比较,我想要做的是查看list2中每个列表的最后一个值是否出现在list1的列表中。因此,10016中的list2第一个列表中的上述示例列表list1存在Yes。所以,如果它存在,那么我想在excel行中写下以下值:

[value2 from 3rd list of list1value 3 in 3rd list oflist1value 2 in 1st list oflist2value3 in 1st list of list2value 1 in 3rd list of list1list2]

如果list1中的每个列表的最后一个值都不在No中,则将以下内容写入Excel文件:

[N/AN/Avalue 2 in current list of list2value3 in current list of list2value 4 in current list of list2xlwt]

我知道python中的excel files等用于写入book = xlwt.Workbook(encoding="utf-8") sheet1 = book.add_sheet("Sheet 1") sheet1.write(0, 0, "User ID Present?") sheet1.write(0, 1, "User Name") sheet1.write(0, 2, "Center Name") sheet1.write(0, 3, "Text") sheet1.write(0, 4, "Date") sheet1.write(0, 5, "User ID") i=1 for items2 in list2: for items1 in list1: if items2[3] in items1[0]: sheet1.write(i, 0, "Yes") sheet1.write(i, 1, items1[1]) sheet1.write(i, 2, items1[2]) sheet1.write(i, 3, items2[1]) sheet1.write(i, 4, items2[2]) sheet1.write(i, 5, items1[0]) else: sheet1.write(i, 0, "No") sheet1.write(i, 1, 'N/A') sheet1.write(i, 2, 'N/A') sheet1.write(i, 3, items2[1]) sheet1.write(i, 4, items2[2]) sheet1.write(i, 5, items2[3]) i=i+1 book.save("Sample_Report_V1.xlsx") 的软件包但我在上面进行这样的比较时无法理解如何编写?< / p>

更新:以下是我目前的情况:

file cannot be opened due to wrong file format or file extension

当我运行此代码时,它会毫无错误地执行但是当我尝试打开文件时,我会收到错误mydf <- read.table(text=" vessel1 1 6 vessel1 2 7 vessel1 3 10 vessel1 4 16 vessel1 5 18 vessel1 6 20 vessel1 7 22 vessel1 8 24 vessel1 9 36 vessel1 10 37 vessel1 11 38 vessel1 12 38 vessel1 13 39 vessel1 14 40 vessel1 15 41 vessel1 16 41 vessel1 17 41 vessel2 1 7 vessel2 2 9 vessel2 3 12 vessel2 4 15 vessel2 5 20 vessel2 6 22 vessel2 7 23 vessel2 8 24 vessel2 9 25 vessel2 10 26 vessel2 11 27 vessel3 12 28 vessel4 13 29 vessel5 14 30" ) colnames(mydf) = c("Vessel","Time","Speed") middle_Speed_Time = by(mydf,mydf[,"Vessel"],function(x){ min(x[x[,3] >= max(x[,3])/2,2]) } )

list1和list2是我上面描述的两个列表列表。

1 个答案:

答案 0 :(得分:2)

您想要做的是直截了当,它只是语法以及您如何组织代码。以下是我解决问题的方法:

import csv

def merge(list1, list2):
    lookup = {row[0]: row for row in list1}
    for row2 in list2:
        value = row2[-1]
        if value in lookup:
            row1 = lookup[value]
            yield 'Yes', row1[1], row1[2], row2[1], row2[2], row1[0]
        else:
            yield 'No', 'N/A', 'N/A', row2[1], row2[2], row2[3]

if __name__ == '__main__':
    list1 = [
        ['10001', 'Jason', 'University of Utah School of Medicine'],
        ['10007', 'Zhang', 'Duke'],
        ['10016', 'Som', 'Dana-Farber']]
    list2 = [
        ['Som', 'mrcc', 'Wed Dec 20 18:36:28 +0000 2017', '10016'],
        ['David', 'mrcc', 'Wed Dec 20 18:36:38 +0000 2017', '10013'],
        ['Kristen', 'mrcc gro', 'Wed Dec 20 18:37:24 +0000 2017', '10045'],
        ['Zhang', 'wan mrcc', 'Wed Dec 20 18:38:44 +0000 2017', '10007']]

    rows = merge(list1, list2)
    with open('merge_lists.csv', 'wb') as outfile:
        writer = csv.writer(outfile)
        writer.writerow(['User ID Present?', 'User Name', 'Center Name',
            'Text', 'Date', 'User ID'])
        writer.writerows(rows)

注释

  • 您描述的程序位于功能merge
  • 为了使查找更容易,我创建了一个名为lookup的字典,其中用户ID是键,行是值。
  • 通过写入excel文件,我想你的意思是CSV文件格式

更新

我的原始答案输出到CSV文件,我认为最容易处理。您可以毫无困难地在Excel中打开该文件。但是,我觉得你坚持写Excel格式,所以我花了一些时间来学习xlwt足以使用它。这是我更新的解决方案。请注意,此模块处理较旧的.xls文件格式,而不是较新的.xlsx格式。如果保存为.xlsx,Excel将无法打开它。

import xlwt

def merge(list1, list2):
    lookup = {row[0]: row for row in list1}
    for row2 in list2:
        value = row2[-1]
        if value in lookup:
            row1 = lookup[value]
            yield 'Yes', row1[1], row1[2], row2[1], row2[2], row1[0]
        else:
            yield 'No', 'N/A', 'N/A', row2[1], row2[2], row2[3]

def write_row(sheet, row, row_number, starting_column_number=0):
    for column_number, cell in enumerate(row, starting_column_number):
        sheet.write(row_number, column_number, cell)

if __name__ == '__main__':
    list1 = [
        ['10001', 'Jason', 'University of Utah School of Medicine'],
        ['10007', 'Zhang', 'Duke'],
        ['10016', 'Som', 'Dana-Farber']]
    list2 = [
        ['Som', 'mrcc', 'Wed Dec 20 18:36:28 +0000 2017', '10016'],
        ['David', 'mrcc', 'Wed Dec 20 18:36:38 +0000 2017', '10013'],
        ['Kristen', 'mrcc gro', 'Wed Dec 20 18:37:24 +0000 2017', '10045'],
        ['Zhang', 'wan mrcc', 'Wed Dec 20 18:38:44 +0000 2017', '10007']]

    rows = merge(list1, list2)
    workbook = xlwt.Workbook(encoding='utf-8')
    worksheet = workbook.add_sheet('Sheet 1')

    write_row(
        worksheet,
        'User ID Present?,User Name,Center Name,Text,Date,User ID'.split(','),
        row_number=0)

    for row_number, row in enumerate(rows, 1):
        write_row(worksheet, row, row_number)

    workbook.save('Sample_Report_V1.xls')