如何将多个词典放在列表中

弓箭

我创建了一个字典:

new_list = {}

这使,

    {'date': '13/09/2020', 'day': '13', 'month': '9', 'year': '2020', 'cases': '35', 'deaths': '0', 
    'countriesAndTerritories': 'Afghanistan', 'countryTerritoryId': 'AF', 'countryTerritoryCode': 'AFG', 
    'population2019': '38041757', 'continent': 'Asia', 'cumulativeper1000002Weeks': '1.3090878'}
    {'date': '12/9/20', 'day': '12', 'month': '9', 'year': '2020', 'cases': '34', 'deaths': '0', 
    'countriesAndTerritories': 'Afghanistan', 'countryTerritoryId': 'AF', 'countryTerritoryCode': 'AFG', 
    'population2019': '38041757', 'continent': 'Asia', 'cumulativeper1000002Weeks': '1.22496971'}
    {'date': '11/9/20', 'day': '11', 'month': '9', 'year': '2020', 'cases': '28', 'deaths': '0', 
    'countriesAndTerritories': 'Afghanistan', 'countryTerritoryId': 'AF', 'countryTerritoryCode': 'AFG', 
    'population2019': '38041757', 'continent': 'Asia', 'cumulativeper1000002Weeks': '1.16450983'}

但是现在我想将此字典添加到列表中

dataset= [new_list] 

这使,

[{'date': '13/09/2020', 'day': '13', 'month': '9', 'year': '2020', 'cases': '35', 'deaths': '0', 'countriesAndTerritories': 'Afghanistan', 'countryTerritoryId': 'AF', 'countryTerritoryCode': 'AFG', 'population2019': '38041757', 'continent': 'Asia', 'cumulativeper1000002Weeks': '1.3090878'}]
[{'date': '12/9/20', 'day': '12', 'month': '9', 'year': '2020', 'cases': '34', 'deaths': '0', 'countriesAndTerritories': 'Afghanistan', 'countryTerritoryId': 'AF', 'countryTerritoryCode': 'AFG', 'population2019': '38041757', 'continent': 'Asia', 'cumulativeper1000002Weeks': '1.22496971'}]
[{'date': '11/9/20', 'day': '11', 'month': '9', 'year': '2020', 'cases': '28', 'deaths': '0', 'countriesAndTerritories': 'Afghanistan', 'countryTerritoryId': 'AF', 'countryTerritoryCode': 'AFG', 'population2019': '38041757', 'continent': 'Asia', 'cumulativeper1000002Weeks': '1.16450983'}]

但是我试图得到的结果是(所以在开始和结束处只能是“ []”),因为这样我就可以执行dataset [0:2](否则它将给我“ Nonetype”对象不可下标)并且它只会给出前两个字典

[{'date': '13/09/2020', 'day': '13', 'month': '9', 'year': '2020', 'cases': '35', 'deaths': '0', 'countriesAndTerritories': 'Afghanistan', 'countryTerritoryId': 'AF', 'countryTerritoryCode': 'AFG', 'population2019': '38041757', 'continent': 'Asia', 'cumulativeper1000002Weeks': '1.3090878'},
{'date': '12/9/20', 'day': '12', 'month': '9', 'year': '2020', 'cases': '34', 'deaths': '0', 'countriesAndTerritories': 'Afghanistan', 'countryTerritoryId': 'AF', 'countryTerritoryCode': 'AFG', 'population2019': '38041757', 'continent': 'Asia', 'cumulativeper1000002Weeks': '1.22496971'},
{'date': '11/9/20', 'day': '11', 'month': '9', 'year': '2020', 'cases': '28', 'deaths': '0', 'countriesAndTerritories': 'Afghanistan', 'countryTerritoryId': 'AF', 'countryTerritoryCode': 'AFG', 'population2019': '38041757', 'continent': 'Asia', 'cumulativeper1000002Weeks': '1.16450983'}]

我的代码如下:

import csv
def read_dataset() -> List[Dict[str,str]]:
    dataset = []
    with open('covid-19.csv', 'r') as file:
        reader = csv.reader(file)
        no_head = next(reader, None)
       
        for row in reader:
            lst = [
                'date',
                'day', 
                'month', 
                'year',
                'cases', 
                'deaths', 
                'countriesAndTerritories', 
                'countryTerritoryId', 
                'countryTerritoryCode', 
                'population2019', 
                'continent',
                'cumulativeper1000002Weeks',
            ]
            new_list = {}
            for x in range(len(lst)):
                new_list[lst[x]] = row[x]
            dataset= [new_list]
            #print(dataset)
            
dataset = read_dataset()
print(dataset[0:2])
考斯101
import csv
def read_dataset() -> List[Dict[str,str]]:
    dataset = []
    with open('covid-19.csv', 'r') as file:
        reader = csv.reader(file)
        no_head = next(reader, None)
    
        for row in reader:
            lst = [
                'date',
                'day', 
                'month', 
                'year',
                'cases', 
                'deaths', 
                'countriesAndTerritories', 
                'countryTerritoryId', 
                'countryTerritoryCode', 
                'population2019', 
                'continent',
                'cumulativeper1000002Weeks',
            ]
            new_list = {}
            for x in range(len(lst)):
                new_list[lst[x]] = row[x]
            dataset.append(new_list)
            #print(dataset)
    return dataset
            
dataset = read_dataset()
print(dataset[0:2])

区别在于您必须使用:

dataset.append(new_list)

functon,在末尾附加new_list(这是一个字典,有点令人困惑,请注意变量名!)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将jpg放在JComboBox中?

来自分类Dev

Bulbflow –如何将列表/词典另存为属性

来自分类Dev

如何将列表项放在导航抽屉中的列表视图的底部,例如Foursquare

来自分类Dev

如何将ComboBoxTableCell放在TableView中?

来自分类Dev

如何将列表项放在无序列表的中央?

来自分类Dev

如何将重点放在榆木中?

来自分类Dev

如何将列表转换为JavaScript中的多个参数

来自分类Dev

如何将ID放在fnSelect()中?

来自分类Dev

熊猫:如何将列中的多个列表分成多行?

来自分类Dev

如何将字典词典转换为Ansible vars文件中的字典列表?

来自分类Dev

如何将列表的多个值放在MessageBox中

来自分类Dev

如何将QComboBox放在QColumnView中

来自分类Dev

如何将组合框项目放在列表中?

来自分类Dev

如何将Twitch IRC响应中的表情解析为词典列表?

来自分类Dev

如何将多个远程服务器的输出保存到单个词典中?

来自分类Dev

如何将词典列表转换为嵌套词典?

来自分类Dev

如何将多个演员放在列表中并让他们移动-Python pygame

来自分类Dev

如何将jpg放在JComboBox中?

来自分类Dev

如何将多个列表框值分配给一个变量,然后将其放在“电子邮件到”字段中

来自分类Dev

如何将项目从VM中的词典放置到有序列表

来自分类Dev

如何将项目添加到词典中的列表中

来自分类Dev

如何将多个表值放在百里香叶html文件中?

来自分类Dev

在Markdown中,如何将表格放在有序列表中?

来自分类Dev

如何将列表项放在无序列表的中央?

来自分类Dev

如何将列表转换为JavaScript中的多个参数

来自分类Dev

如何将多个fluidRows()放在一个Shiny输出中?

来自分类Dev

如何将函数放在宏中?

来自分类Dev

如何将 php 放在 php 中

来自分类Dev

如何将“AdaptiveActionSet”放在“AdaptiveColumn”中?

Related 相关文章

  1. 1

    如何将jpg放在JComboBox中?

  2. 2

    Bulbflow –如何将列表/词典另存为属性

  3. 3

    如何将列表项放在导航抽屉中的列表视图的底部,例如Foursquare

  4. 4

    如何将ComboBoxTableCell放在TableView中?

  5. 5

    如何将列表项放在无序列表的中央?

  6. 6

    如何将重点放在榆木中?

  7. 7

    如何将列表转换为JavaScript中的多个参数

  8. 8

    如何将ID放在fnSelect()中?

  9. 9

    熊猫:如何将列中的多个列表分成多行?

  10. 10

    如何将字典词典转换为Ansible vars文件中的字典列表?

  11. 11

    如何将列表的多个值放在MessageBox中

  12. 12

    如何将QComboBox放在QColumnView中

  13. 13

    如何将组合框项目放在列表中?

  14. 14

    如何将Twitch IRC响应中的表情解析为词典列表?

  15. 15

    如何将多个远程服务器的输出保存到单个词典中?

  16. 16

    如何将词典列表转换为嵌套词典?

  17. 17

    如何将多个演员放在列表中并让他们移动-Python pygame

  18. 18

    如何将jpg放在JComboBox中?

  19. 19

    如何将多个列表框值分配给一个变量,然后将其放在“电子邮件到”字段中

  20. 20

    如何将项目从VM中的词典放置到有序列表

  21. 21

    如何将项目添加到词典中的列表中

  22. 22

    如何将多个表值放在百里香叶html文件中?

  23. 23

    在Markdown中,如何将表格放在有序列表中?

  24. 24

    如何将列表项放在无序列表的中央?

  25. 25

    如何将列表转换为JavaScript中的多个参数

  26. 26

    如何将多个fluidRows()放在一个Shiny输出中?

  27. 27

    如何将函数放在宏中?

  28. 28

    如何将 php 放在 php 中

  29. 29

    如何将“AdaptiveActionSet”放在“AdaptiveColumn”中?

热门标签

归档