如何比较2个列表并将它们合并到Python / MySQL中? [英] How to compare 2 lists and merge them in Python/MySQL?

查看:154
本文介绍了如何比较2个列表并将它们合并到Python / MySQL中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是示例数据。



例如:

  abcdef key dupe 
--------------------
1 dcfkl 1 x
2 ghj 1
3 ihuu 2
4 urt 2 x

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

  abcdef key dupe 
--------------------
2 gchkj 1
3 irhuu 2

我到目前为止:

  import string,os,sys 
import来自EncryptedFile import的MySQLdb
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不为空)
result = cursor.fetchall()
result2 = cursor2.fetchall()
$ b每个记录
每个字段

执行比较并执行必要的更新

###如何将记录与相同的键值进行比较,并使用副本中的非空值更新原始行空字段值?请填写此空白...


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

谢谢你们!

解决方案

让我们有一些乐趣...

  mysql> create table so(a int,b char,c char,d char,e char,f char,`key` int,dupe char); 
查询OK,0行受影响(0.05秒)

mysql>插入到这样的值(1,'d','c','f','k','1',1,'x'),(2,'g',null,'h',null' j',1,null),(3,'i',null,'h','u','u',2,null),(4,'u','r',null, t',2,'x');
查询OK,4行受影响(0.00秒)
记录:4重复:0警告:0

mysql>选择*从这样的顺序由a;
+ ------ + ------ + ------ + ------ + ------ + ------ + --- --- + ------ +
| a | b | c | d | e | f |关键| dupe |
+ ------ + ------ + ------ + ------ + ------ + ------ + --- --- + ------ +
| 1 | d | c | f | k | l | 1 | x |
| 2 | g | NULL | h | NULL | j | 1 | NULL |
| 3 |我| NULL | h |你|你| 2 | NULL |
| 4 |你| r | NULL | NULL | t | 2 | x |
+ ------ + ------ + ------ + ------ + ------ + ------ + --- --- + ------ +
4行集(0.00秒)



< hr>

  Python 2.6.5(r265:79063,2010年3月26日,22:43:05)
[GCC 4.2.1 (Apple Inc. build 5646)(dot 1)] on darwin
输入help,copyright,credits或license了解更多信息。
>>>导入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()
>>>行
((1L,'d','c','f','k','1',1L,'x'),(4L,'u','r' (3L,'i',无''''','',' u','u',2L,None))
>>> data = dict()
>>>对于行行:
...键,isDupe =行[-2],行[-1]
...如果键不在数据中:
... data [key ] =列表(行[: - 1])$ ​​b $ b ... else:
...对于我在范围(len(行)-1):
...如果data [键] [i]为无或(不是数组,行[i]不是无):
...数据[key] [i] =行[i]
...
>>>数据
{1L:[2L,'g','c','h','k','j',1L],2L:[3L,'i','r' ,'u','u',2L]}


I want to merge data. Following are my MySQL tables. I want to use Python to traverse though a list of both Lists (one with dupe = 'x' and other with null dupes).

This is sample data. Actual data is humongous.

For instance :

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

From the above sample table, the desired output is :

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

What I have so far :

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()

Thanks guys!

解决方案

OK, let's have some fun...

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]}

这篇关于如何比较2个列表并将它们合并到Python / MySQL中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆