【问题标题】:How to compare 2 lists and merge them in Python/MySQL?如何比较 2 个列表并在 Python/MySQL 中合并它们?
【发布时间】:2011-02-06 07:34:27
【问题描述】:

我想合并数据。以下是我的 MySQL 表。我想使用 Python 遍历两个列表的列表(一个带有 dupe = 'x' 和另一个带有 null dupes)。

这是示例数据。实际数据非常庞大。

例如:

a b c d e f key dupe
--------------------
1 d c f k l 1   x
2 g   h   j 1    
3 i   h u u 2
4 u r     t 2   x

从上面的示例表中,所需的输出是:

a b c d e f key dupe
--------------------
2 g c h k j 1
3 i r h u u 2

到目前为止我所拥有的:

import string, os, sys
import MySQLdb
from EncryptedFile import EncryptedFile

enc = EncryptedFile( os.getenv("HOME") + '/.py-encrypted-file')
user = enc.getValue("user")
pw = enc.getValue("pw")

db = MySQLdb.connect(host="127.0.0.1", user=user, passwd=pw,db=user)

cursor = db.cursor()
cursor2 = db.cursor()

cursor.execute("select * from delThisTable where dupe is null")
cursor2.execute("select * from delThisTable where dupe is not null")
result = cursor.fetchall()
result2 = cursor2.fetchall()

for each record
    for each field
        perform the comparison and perform the necessary updates

             ### How do I compare the record with same key value and update the original row null field value with the non-null value from the duplicate? Please fill this void...


cursor.close()
cursor2.close()
db.close()

谢谢大家!

【问题讨论】:

  • 无法找出问题所在。你想得到算法,还是在具体框架方面的实现?事实上,您只需要遍历光标和“合并”项目的字段。在这种情况下你能执行普通的 SQL 吗?因为如果可以,查询很简单。
  • 这是简单的测试数据。实际上,有几千行和几百列,因此采用了这种方法。谢谢。
  • update delthistable t set ta = coalesce(dup.a, ta), tb = coalesce(dup.b, tb)... from (select * from delthistable where dupe = 'x') dup其中 t.dupe 'x' 和 t.key = dup.key --------------------------------- ----------------------------- 从 delthistable 中删除,其中 dupe 'x'

标签: python mysql merge duplicates duplicate-data


【解决方案1】:

好吧,让我们玩得开心……

mysql> create table so (a int, b char, c char, d char, e char, f char, `key` int, dupe char);
Query OK, 0 rows affected (0.05 sec)

mysql> insert into so values (1, 'd', 'c', 'f', 'k', 'l', 1, 'x'), (2, 'g', null, 'h', null, 'j', 1, null), (3, 'i', null, 'h', 'u', 'u', 2, null), (4, 'u', 'r', null, null, 't', 2, 'x');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from so order by a;
+------+------+------+------+------+------+------+------+
| a    | b    | c    | d    | e    | f    | key  | dupe |
+------+------+------+------+------+------+------+------+
|    1 | d    | c    | f    | k    | l    |    1 | x    |
|    2 | g    | NULL | h    | NULL | j    |    1 | NULL |
|    3 | i    | NULL | h    | u    | u    |    2 | NULL |
|    4 | u    | r    | NULL | NULL | t    |    2 | x    |
+------+------+------+------+------+------+------+------+
4 rows in set (0.00 sec)

Python 2.6.5 (r265:79063, Mar 26 2010, 22:43:05) 
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> db = MySQLdb.connect(host="127.0.0.1", db="test")
>>> c = db.cursor()
>>> c.execute("SELECT a, b, c, d, e, f, `key`, dupe FROM so")
4L
>>> rows = c.fetchall()
>>> rows
((1L, 'd', 'c', 'f', 'k', 'l', 1L, 'x'), (4L, 'u', 'r', None, None, 't', 2L, 'x'), (2L, 'g', None, 'h', None, 'j', 1L, None), (3L, 'i', None, 'h', 'u', 'u', 2L, None))
>>> data = dict()
>>> for row in rows:
...  key, isDupe = row[-2], row[-1]
...  if key not in data:
...   data[key] = list(row[:-1])
...  else:
...   for i in range(len(row)-1):
...    if data[key][i] is None or (not isDupe and row[i] is not None):
...     data[key][i] = row[i]
... 
>>> data
{1L: [2L, 'g', 'c', 'h', 'k', 'j', 1L], 2L: [3L, 'i', 'r', 'h', 'u', 'u', 2L]}

【讨论】:

  • 感谢您的解决方案。我在实际表中有几百行。如何使您的代码适应我的实际表格?再次感谢!
  • 您的表中的数据是否适合您的 RAM?如果是这样,我认为不需要适应。
  • 有效!非常感谢。我正在找出将最终数据转储到 MySQL 表中的最佳方法。某些字段为无,日期的格式为 date.datetime。转储到 MySQL 的简单方法?
猜你喜欢
  • 2014-08-01
  • 2017-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多