将元组列表和列表转换为字典



我有一个元组列表

data = [(2015-10-08,2016-07-17,2015-09-09,2014-01-29),(Alex, Jerry, Tony, Tom), (5,6,7,8)]

我有一个列表,这个列表包含列标题。title =[日期,姓名,年龄]有了这个列表和元组列表,我想要一个字典。这是预期的输出

output = {1:{'Date': 2015-10-08,2016-07-17,2015-09-09,2014-01-29} ,{'Name' : Alex, Jerry, Tony, Tom}, {'Age' : 5,6,7,8}}

试试这个:

dict(zip(title, data))

或者使它们成为集合:

dict(zip(title, map(set, data)))

尝试:

output = {}
output["Date"] = set(data[0])
output["Name"] = set(data[1])
output["Age"] = set(data[2])
data = [('2015-10-08','2016-07-17,2015-09-09','2014-01-29'),('Alex', 'Jerry', 'Tony', 'Tom'), (5,6,7,8)]
title = ['Date', 'Name', 'Age']
your_dict = {key: value for key, value in zip(title, data)}

这是一个解决方案:

data = [('2015-10-08','2016-07-17','2015-09-09','2014-01-29'),('Alex', 'Jerry', 'Tony', 'Tom'), ('5','6','7','8')]
title = ['Date', 'Name', 'Age']
output = {}
for i in range(len(title)):
output[i+1] = {title[i]: ",".join(data[index])}

print (output)

输出:

{1: {'Date': '2015-10-08,2016-07-17,2015-09-09,2014-01-29'}, 2: {'Name': '2015-10-08,2016-07-17,2015-09-09,2014-01-29'}, 3: {'Age': '2015-10-08,2016-07-17,2015-09-09,2014-01-29'}}

相关内容

  • 没有找到相关文章

最新更新