如何以怪异列表格式创建字典?

发布于 2025-02-11 05:26:36 字数 1088 浏览 1 评论 0原文

因此,我使用SQL尝试描述一个表并获得了一个怪异的输出,因为我的功能返回了列表,因此我有一个怪异的格式,我想将其制作到字典列表中。但是不确定我将如何处理。并且想知道是否有人可以将我指向正确的方向。 列表

[['Field', 'Type', 'Null', 'Key', 'Default', 'Extra'],
(('_id', 'bigint(20) unsigned', 'NO', 'PRI', None, ''), 
('_load_dt', 'date', 'NO', '', None, ''), 
('_load_dt_time', 'timestamp', 'YES', 'MUL', 'current_timestamp()', ''), 
('_data_hash', 'char(160)', 'YES', 'UNI', None, ''), 
('_host', 'char(200)', 'YES', '', None, ''), 
('_port', 'int(6)', 'YES', '', None, ''), 
('_schema', 'char(200)', 'YES', '', None, ''), 
('_deleted', 'tinyint(1)', 'YES', '', '0', ''), 
('acct_id', 'varchar(200)', 'NO', 'MUL', None, ''), 
('account_title', 'varchar(200)', 'NO', 'MUL', None, ''), 
('signup_date', 'varchar(200)', 'NO', 'MUL', None, ''), 
('admin_email', 'varchar(200)', 'NO', 'MUL', None, ''))]

所以这是我想最终结果的 [{field:_id,type:bigint(20)unsigned,null:no,key:pri:pri,extra:none},{field:_load_dt,type:date:date,null:date,null:no,键:'','',默认:默认:没有,额外:''},.....]

我不确定从哪里开始,也许有两个循环,一个通过仅通过第一个支架循环而创建键,然后是另一个循环,然后循环通过括号中的每个位置以获取每个键的值?

So I used SQL to try to describe a table and got a weird output because my function returns a list so I have this weird format that I want to make into a list of dictionaries. But not sure how I would go about it. And was wondering if someone could point me in the right direction. So here is the list I have

[['Field', 'Type', 'Null', 'Key', 'Default', 'Extra'],
(('_id', 'bigint(20) unsigned', 'NO', 'PRI', None, ''), 
('_load_dt', 'date', 'NO', '', None, ''), 
('_load_dt_time', 'timestamp', 'YES', 'MUL', 'current_timestamp()', ''), 
('_data_hash', 'char(160)', 'YES', 'UNI', None, ''), 
('_host', 'char(200)', 'YES', '', None, ''), 
('_port', 'int(6)', 'YES', '', None, ''), 
('_schema', 'char(200)', 'YES', '', None, ''), 
('_deleted', 'tinyint(1)', 'YES', '', '0', ''), 
('acct_id', 'varchar(200)', 'NO', 'MUL', None, ''), 
('account_title', 'varchar(200)', 'NO', 'MUL', None, ''), 
('signup_date', 'varchar(200)', 'NO', 'MUL', None, ''), 
('admin_email', 'varchar(200)', 'NO', 'MUL', None, ''))]

I want to the end result to look something like this
[{Field:_id, Type:bigint(20) unsigned, Null:No, Key:PRI, Extra:None}, {Field:_load_dt, Type:date, NULL:No, Key:'', Default:None, Extra:''}, .....]

I'm not sure where to begin, maybe have two loops one that creates the keys by looping through just the first bracket and then another loop that loops through each position in parentheses to get the value for each of those keys?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

赠我空喜 2025-02-18 05:26:36

尝试以下尝试:

# This is the info you provided in the original question:
data = [['Field', 'Type', 'Null', 'Key', 'Default', 'Extra'],
(('_id', 'bigint(20) unsigned', 'NO', 'PRI', None, ''), 
('_load_dt', 'date', 'NO', '', None, ''), 
('_load_dt_time', 'timestamp', 'YES', 'MUL', 'current_timestamp()', ''), 
('_data_hash', 'char(160)', 'YES', 'UNI', None, ''), 
('_host', 'char(200)', 'YES', '', None, ''), 
('_port', 'int(6)', 'YES', '', None, ''), 
('_schema', 'char(200)', 'YES', '', None, ''), 
('_deleted', 'tinyint(1)', 'YES', '', '0', ''), 
('acct_id', 'varchar(200)', 'NO', 'MUL', None, ''), 
('account_title', 'varchar(200)', 'NO', 'MUL', None, ''), 
('signup_date', 'varchar(200)', 'NO', 'MUL', None, ''), 
('admin_email', 'varchar(200)', 'NO', 'MUL', None, ''))]

# Separate the column definitions and the items themselves:
(cols, items) = data

# Use a list comprehension to create a new list that looks the way you expect:
result = [dict(zip(cols, item)) for item in items]

# Done
print(result)

有关更多信息,请访问:

Try this:

# This is the info you provided in the original question:
data = [['Field', 'Type', 'Null', 'Key', 'Default', 'Extra'],
(('_id', 'bigint(20) unsigned', 'NO', 'PRI', None, ''), 
('_load_dt', 'date', 'NO', '', None, ''), 
('_load_dt_time', 'timestamp', 'YES', 'MUL', 'current_timestamp()', ''), 
('_data_hash', 'char(160)', 'YES', 'UNI', None, ''), 
('_host', 'char(200)', 'YES', '', None, ''), 
('_port', 'int(6)', 'YES', '', None, ''), 
('_schema', 'char(200)', 'YES', '', None, ''), 
('_deleted', 'tinyint(1)', 'YES', '', '0', ''), 
('acct_id', 'varchar(200)', 'NO', 'MUL', None, ''), 
('account_title', 'varchar(200)', 'NO', 'MUL', None, ''), 
('signup_date', 'varchar(200)', 'NO', 'MUL', None, ''), 
('admin_email', 'varchar(200)', 'NO', 'MUL', None, ''))]

# Separate the column definitions and the items themselves:
(cols, items) = data

# Use a list comprehension to create a new list that looks the way you expect:
result = [dict(zip(cols, item)) for item in items]

# Done
print(result)

For more info, check out:

梨涡 2025-02-18 05:26:36

简单解决方案

In [2]: data = [['Field', 'Type', 'Null', 'Key', 'Default', 'Extra'], 
   ...: (('_id', 'bigint(20) unsigned', 'NO', 'PRI', None, ''),  
   ...: ('_load_dt', 'date', 'NO', '', None, ''),  
   ...: ('_load_dt_time', 'timestamp', 'YES', 'MUL', 'current_timestamp()', ''),  
   ...: ('_data_hash', 'char(160)', 'YES', 'UNI', None, ''),  
   ...: ('_host', 'char(200)', 'YES', '', None, ''),  
   ...: ('_port', 'int(6)', 'YES', '', None, ''),  
   ...: ('_schema', 'char(200)', 'YES', '', None, ''),  
   ...: ('_deleted', 'tinyint(1)', 'YES', '', '0', ''),  
   ...: ('acct_id', 'varchar(200)', 'NO', 'MUL', None, ''),  
   ...: ('account_title', 'varchar(200)', 'NO', 'MUL', None, ''),  
   ...: ('signup_date', 'varchar(200)', 'NO', 'MUL', None, ''),  
   ...: ('admin_email', 'varchar(200)', 'NO', 'MUL', None, ''))] 

In [5]: fields = data[0]                                                                                                                               

In [6]: res = []                                                                                                                                       

In [7]: items = data[1] 

In [13]: for item in items: 
    ...:     resItem = {} 
    ...:     for index,fieldItem in enumerate(fields): 
    ...:         resItem[fieldItem] = item[index] 
    ...:     res.append(resItem) 

In [14]: res                                                                                                                                           

simple solution

In [2]: data = [['Field', 'Type', 'Null', 'Key', 'Default', 'Extra'], 
   ...: (('_id', 'bigint(20) unsigned', 'NO', 'PRI', None, ''),  
   ...: ('_load_dt', 'date', 'NO', '', None, ''),  
   ...: ('_load_dt_time', 'timestamp', 'YES', 'MUL', 'current_timestamp()', ''),  
   ...: ('_data_hash', 'char(160)', 'YES', 'UNI', None, ''),  
   ...: ('_host', 'char(200)', 'YES', '', None, ''),  
   ...: ('_port', 'int(6)', 'YES', '', None, ''),  
   ...: ('_schema', 'char(200)', 'YES', '', None, ''),  
   ...: ('_deleted', 'tinyint(1)', 'YES', '', '0', ''),  
   ...: ('acct_id', 'varchar(200)', 'NO', 'MUL', None, ''),  
   ...: ('account_title', 'varchar(200)', 'NO', 'MUL', None, ''),  
   ...: ('signup_date', 'varchar(200)', 'NO', 'MUL', None, ''),  
   ...: ('admin_email', 'varchar(200)', 'NO', 'MUL', None, ''))] 

In [5]: fields = data[0]                                                                                                                               

In [6]: res = []                                                                                                                                       

In [7]: items = data[1] 

In [13]: for item in items: 
    ...:     resItem = {} 
    ...:     for index,fieldItem in enumerate(fields): 
    ...:         resItem[fieldItem] = item[index] 
    ...:     res.append(resItem) 

In [14]: res                                                                                                                                           
风情万种。 2025-02-18 05:26:36

您可以做的一件事是导入pandas和做df = pandas.dataframe(data [1],columns = data [0])。这将使您的数据变成数据框,这可能比词典列表更有用。从数据框架中获取字典列表并不难,例如df.to_dict('records')。如果您只想直接获取字典列表,则可以执行list_of_dicts = [{键:key的值,zip中的值(data [0],row)} for data [1]] 。

One thing you can do is import pandas and do df = pandas.DataFrame(data[1], columns = data[0]). This will turn your data into a dataframe, which likely will be more useful than a list of dictionaries. It's not too difficult to get a list of dictionaries out of a dataframe, such as with df.to_dict('records'). If you want to just get a list of dictionaries directly, you can do list_of_dicts = [{key: value for key, value in zip(data[0], row)} for row in data[1]].

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