Pythonistas,如何在 MySQL 中从上到下到从左到右移动数据(为每个 ID 考虑多个值)?
当前的任务是将表 1 中所示的数据移动到表 2 中的数据。
表 (1)
ID Val
-- ---
1 a
1 b
1 c
2 k
3 l
3 m
3 n
Val 列取决于每个 ID 的唯一值的数量。在本例中它是 3,但在现实世界中它可以是 20!
表 (2)
ID Val1 Val2 Val3
-- -- -- --
1 a b c
2 k
3 l m n
我如何处理较小的 Val 列值(本例中为 3):
我创建一个临时表。
create table test(ID int not null, b int auto_increment not null,primary key(ID,b), Val varchar(255));
然后我插入数据进行测试。
我得到以下信息(我必须手动创建 Val 列):
ID Val b
-- --- --
1 a 1
1 b 2
1 c 3
2 k 1
3 l 1
3 m 2
3 n 3
我知道这是一个繁琐的过程,需要大量的手动工作。那是在我爱上 Python 之前!非常感谢 Python 中针对此问题的有效解决方案!
这就是我到目前为止所拥有的
import MySQLdb
import itertools
import dbstring
cursor = db.cursor()
cursor.execute("select ID, val from mytable")
mydata = cursor.fetchall()
IDlist = []
vallist = []
finallist = []
for record in mydata:
IDlist.append(record[1])
vallist.append(record[2])
zipped = zip(IDlist,vallist)
zipped.sort(key=lambda x:x[0])
for i, j in itertools.groupby(zipped, key=lambda x:x[0]):
finallist = [k[1] for k in j]
finallist.insert(0, i)
finallist += [None] * (4 - len(finallist)) ### Making it a uniform size list
myvalues.append(finallist)
cursor.executemany("INSERT INTO temptable VALUES (%s, %s, %s, %s)", myvalues)
db.close()
Task at hand is to move data as shown in table 1 to that of table 2.
Table (1)
ID Val
-- ---
1 a
1 b
1 c
2 k
3 l
3 m
3 n
Val columns depend on the number of unique values for each ID. in this case it is 3 but it can be 20 in real world!
Table (2)
ID Val1 Val2 Val3
-- -- -- --
1 a b c
2 k
3 l m n
How am I tackling it for smaller values of Val columns (3 in this case) :
I create a temp table.
create table test(ID int not null, b int auto_increment not null,primary key(ID,b), Val varchar(255));
I then insert data in to test.
I get the following (I have to create the Val columns manually):
ID Val b
-- --- --
1 a 1
1 b 2
1 c 3
2 k 1
3 l 1
3 m 2
3 n 3
I know that this is a tedious process with lot of manual work. This was before I fell in love with Python! An efficient solution in Python for this problem is really appreciated!
This is what I have so far
import MySQLdb
import itertools
import dbstring
cursor = db.cursor()
cursor.execute("select ID, val from mytable")
mydata = cursor.fetchall()
IDlist = []
vallist = []
finallist = []
for record in mydata:
IDlist.append(record[1])
vallist.append(record[2])
zipped = zip(IDlist,vallist)
zipped.sort(key=lambda x:x[0])
for i, j in itertools.groupby(zipped, key=lambda x:x[0]):
finallist = [k[1] for k in j]
finallist.insert(0, i)
finallist += [None] * (4 - len(finallist)) ### Making it a uniform size list
myvalues.append(finallist)
cursor.executemany("INSERT INTO temptable VALUES (%s, %s, %s, %s)", myvalues)
db.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
执行此操作的 pytonic 方法是使用 itertools.groupby
return
the pytonic way to do this is to use itertools.groupby
return