【问题标题】:Matching array columns in python在python中匹配数组列
【发布时间】:2012-06-01 19:53:49
【问题描述】:

我有两个文件,内容如下。我的问题是,在下面显示的代码中 如果 id 在 file1 和 file2 中匹配,那么如何匹配 file1 中的第二列和 file2 中对应的第二列直到 n 列..

   def process(file):
     pt = []
     f=open(file)
     for l in f: 
       parts=l.strip().split('\t')
        if len(parts) < 3:
          print 'error with either ur input file or field check parts'
          break
        else:
          pt.append(parts)
     return pt
   arr1 = process(file1)
   arr2 = process(file2)                  
   for arr in arr1:
     if arr[0] in arr2:
        //then match arr1[1] with arr2[1] and so on and get the results

文件1:

ID674097256 voice tech department
ID674097257 NA NA
ID674097399 chat  order processing department

文件2:

ID674097212 voice tech department
ID674097257 NA NA
ID674097399 chat  new processing department

【问题讨论】:

  • 你的数据文件大小是多少?有顺序的数据线?
  • 预期的输出对我来说不是很明显。您能否更清楚地解释程序应该输出什么(给定 file1 和 file2 作为输入)?

标签: python file-comparison


【解决方案1】:

使用zip

for (a1, a2) in zip(arr1, arr2):
  if a1[0] == a2[0]:
      ##  do something.

【讨论】:

  • 这也是我的第一个想法,但是在查看 OP 的代码后,我得出结论,OP 想要搜索整个文件中的每个 ID,而不仅仅是在具有相同编号的行上。不过,如果我错了,使用zip 是个好主意。
【解决方案2】:

这个问题对我来说并不完全清楚,但我认为你正在尝试做

for arr in arr1:
    for a in arr2:
        if a[0] == arr[0]:
             print a
             print arr
             # compare the rest of the fields

但是,就性能而言,这可能不是最佳选择。考虑对文件进行排序,看看Compare two different files line by line and write the difference in third file - Python等问题。

【讨论】:

    【解决方案3】:

    如果我理解正确,您需要匹配文件中的相同行。 此代码可能对您的任务有所帮助:

    >>> s = range(10)
    >>> s
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> s2 = range(20)
    >>> s2
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
    >>> matching = {}
    >>> for i, k in zip(s,s2):
    ...     matching[i] = k
    ...
    >>> matching
    {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}
    >>>
    

    【讨论】:

      【解决方案4】:

      此代码将第一个数组的每一行与第二个数组的每一行进行比较。如果行相同(如果列表相同),则将该行放入列表“行”中,并删除行的重复实例。

          rows = [row1 for row1 in arr1 for row2 in arr2 if row1 == row2]
          rows = list(set(rows))
      

      【讨论】:

        猜你喜欢
        • 2020-11-03
        • 2023-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-02
        • 1970-01-01
        相关资源
        最近更新 更多