Pythonistas,如何在 MySQL 中从上到下到从左到右移动数据(为每个 ID 考虑多个值)?

发布于 2024-09-30 03:30:02 字数 1472 浏览 4 评论 0原文

当前的任务是将表 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷月断魂刀 2024-10-07 03:30:02

执行此操作的 pytonic 方法是使用 itertools.groupby

import itertools

a = [(1, 'a'), (1, 'b'), (2, 'c')]

# groupby need sorted value so sorted in case
a.sort(key=lambda x:x[0])

for i, j in itertools.groupby(a, key=lambda x:x[0]):
    print i, [k[1] for k in j]

return

1 ['a', 'b']
2 ['c']

the pytonic way to do this is to use itertools.groupby

import itertools

a = [(1, 'a'), (1, 'b'), (2, 'c')]

# groupby need sorted value so sorted in case
a.sort(key=lambda x:x[0])

for i, j in itertools.groupby(a, key=lambda x:x[0]):
    print i, [k[1] for k in j]

return

1 ['a', 'b']
2 ['c']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文