简体   繁体   English

如何将 Python 中的字典列表转换为元组列表?

[英]How can I convert list of Dictionary in Python to list of Tuples?

I have a big list of dictionaries like this one:我有一大堆像这样的词典:

[{'emailAddress': 'abc@gmail.com', 'firstName': 'JOSHI', 'lastName': 'Messi', 'dropdownMenu2': 'California', 'dropdownMenu': 'Geotech', 'singleLineText': 'Temp LLC', 'mobilePhone': '+9XXXXXXX', 'id': '4514', 'submittedAt': '1641392678'}, {'emailAddress': 'tets@qqq.com', 'firstName': 'Ronaldo', 'lastName': 'Crist7', 'dropdownMenu2': 'Dubai', 'dropdownMenu': 'Investigation', 'singleLineText': 'Company LTD', 'mobilePhone': '+YYYYYYYYY', 'id': '4512', 'submittedAt': '1641389624'}]

It's basically data inserted in this list of dict:它基本上是插入此字典列表中的数据:

[{'emailAddress': 'X', 'firstName': 'X', 'lastName': 'X', 'dropdownMenu2': 'X', 'dropdownMenu': 'X', 'singleLineText': 'X', 'mobilePhone': 'X', 'id': 'X', 'submittedAt': 'X'}]

where X is the value.其中X是值。

I'm trying to get it in this format:我试图以这种格式获取它:

[ ('abc@gmail.com' , 'JOSHI' , 'Messi' ,'California', 'Geotech' , 'Temp LLC','+9XXXXXXX' , '4514' ,'1641392678') , ('tets@qqq.com' , 'Ronaldo' , 'Crist7' , 'Dubai' , 'Investigation' , 'Company LTD' , '+YYYYYYYYY' , '4512' , '1641389624')]

So basically what I'm trying to do is:所以基本上我想做的是:

  1. convert the big data list from dictionary list to tuple list将大数据列表从字典列表转换为元组列表
  2. extract the values from the list so later on I can push it to a MySQL DB从列表中提取值,以便稍后我可以将其推送到 MySQL 数据库

Can someone please help?有人可以帮忙吗?

I have tried converting the dict to JSON and then convert again to tuple but it didn't work.我试过将字典转换为 JSON,然后再次转换为元组,但没有成功。

You can try to first create your list You can create your tuple and take only the values by using:您可以尝试先创建列表您可以创建元组并使用以下方法仅获取值:

yournewtuple = tuple(yourdict.values())

With this you will get only the values of your dictionnary and you have just to do:有了这个,您将只获得字典的值,您只需要做:

finalList.append(yournewtuple) and so on... finalList.append(yournewtuple)等等...

My solution can be more optimized than this but I can't give you the whole solution to your question because I don't know what do you want exactly to do but here is your list of tuples from your dict我的解决方案可以比这更优化,但我无法为您提供问题的完整解决方案,因为我不知道您到底想做什么,但这是您字典中的元组列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM