Python 3 |将复杂列表数据转换为列表中的分组?

时间:2016-11-12 00:12:56

标签: list python-3.x tuples

我正在从我自己的脸谱资料中抓取数据,并在尝试将数据插入sqlite3数据库时陷入困境。

我已经抓住了我的好友列表,并将结果组织成了一个python列表但是python列表至少可以说是凌乱的。

我会告诉你我的意思:

['Jody Ann Elizabeth Lill\n98 mutual friends\nFriends\nGeorgia Gee Smith\n60 mutual friends\nFriends\nShannon Kirk\n52 mutual friends\nFriends\nNatasha Byars-Marsden\n4 mutual friends\nFriends\nLorrae Moakes\n36 mutual friends\nFriends\nAdam AP Kirk\n98 mutual friends\n7 new posts\nFriends\nLittle Mark Jr.\n5 mutual friends\nFriends\nMark Duffy\nFriends with Nat Hodgson\nFriends\nEdward McConville\n40 mutual friends\n3 new posts\nFriends', 'Tanisha Barlow\n2 mutual friends\n10 new posts\nFriends\nCatherine Firth\n4 mutual friends\nFriends\nJayde Woods\n56 mutual friends\n4 new posts\nFriends\nScott Goodall\n43 mutual friends\n4 new posts\nFriends\nNikki Harris\n43 mutual friends\n4 new posts\nFriends\nDebbie Goodall\n85 mutual friends\n6 new posts\nFriends\nConrick Junior\n48 mutual friends\n1 new post\nFriends\nBoris Bighead\n5 mutual friends\nFriends']

上面列表中只有两个项目!但我需要将它们组织成正确的组,所以我需要:

['Jody Ann Elizabeth Lill\n98 mutual friends\nFriends\n', 'Georgia Gee Smith\n60 mutual friends\nFriends\n', 'Shannon Kirk\n52 mutual friends\nFriends\n', 'Natasha Byars-Marsden\n4 mutual friends\nFriends\n', 'Lorrae Moakes\n36 mutual friends\nFriends\n', 'Adam AP Kirk\n98 mutual friends\n7 new posts\nFriends\n', 'Little Mark Jr.\n5 mutual friends\nFriends\n', 'Mark Duffy\nFriends with Nat Hodgson\nFriends\n', 'Edward McConville\n40 mutual friends\n3 new posts\nFriends', 'Tanisha Barlow\n2 mutual friends\n10 new posts\nFriends\n', 'Catherine Firth\n4 mutual friends\nFriends\n', 'Jayde Woods\n56 mutual friends\n4 new posts\nFriends\n', 'Scott Goodall\n43 mutual friends\n4 new posts\nFriends\n', 'Nikki Harris\n43 mutual friends\n4 new posts\nFriends\n', 'Debbie Goodall\n85 mutual friends\n6 new posts\nFriends\n', 'Conrick Junior\n48 mutual friends\n1 new post\nFriends\n', 'Boris Bighead\n5 mutual friends\nFriends']

正如您所看到的,每个分组都没有相同数量的项目:

(其中有3个项目)

'Jody Ann Elizabeth Lill\n98 mutual friends\nFriends\n',

VS

(虽然这个组中有4个项目)

'Conrick Junior\n48 mutual friends\n1 new post\nFriends\n', 

我确实想过创建一个元组列表,但由于组内的项目数量不均匀,我认为肯定会失败。

让你了解我真正想要的最终结果;

我需要按照这样的方式组织我的列表:

[ ('Jody Ann Elizabeth Lill', '98 mutual friends', 'Friends'),
'Georgia Gee Smith', '60 mutual friends', 'Friends',
'Conrick Junior', '48 mutual friends', '1 new post,' 'Friends', 
'Shannon Kirk', '52 mutual friends', 'Friends' ]

然后我希望迭代它们并将它们插入到数据库表中,如下所示:

+---------------------------------+-----------------+--------------------------+------------------------+---------+
|        name                          | new_posts | mutual_friends    | already_friends |id         |
+---------------------------------+-----------------+--------------------------+------------------------+---------+
|Jody Ann Elizabeth Lill |                       |98 mutual friends| Friends                 |1          |
+---------------------------------+-----------------+--------------------------+------------------------+---------+
|Georgia Gee Smith        |                       |60 mutual friends | Friends                 |2          |
+---------------------------------+-----------------+--------------------------+------------------------+---------+
|Adam AP Kirk                  |7 new posts|98 mutual friends | Friends                |3          |
+---------------------------------+-----------------+--------------------------+------------------------+---------+

如何转换我的列表,以便我可以继续将有组织的列表用于数据库?

1 个答案:

答案 0 :(得分:0)

当你从朋友列表中搜集时,你会在每种情况下共同拥有`\ nFriends'字符串。我能想到的最简单方法是应用以下内容:

friends_str = 'Jody Ann Elizabeth Lill\n98 mutual friends\nFriends\nGeorgia Gee Smith\n60 mutual friends\nFriends\nShannon Kirk\n52 mutual friends\nFriends\nNatasha Byars-Marsden\n4 mutual friends\nFriends\nLorrae Moakes\n36 mutual friends\nFriends'
friends_array = friends_str.split('\nFriends')
for friend in friends_array:
    columns = (friend+'\nFriends').split('\n')
    add_to_db(columns)

希望对你有用。