在Python 3中创建嵌套字典内的列表和元组。

huangapple go评论61阅读模式
英文:

Create Lists and Tuples inside a Nested Dictionary in Python 3

问题

这是您的翻译部分,代码部分已经被省略:

你目前编写的代码是:

import csv
results = {}
with open("file.txt", 'r') as f:
    for line in f:
        list = line.split(",")
        year= str.strip(list[0])
        letter=str.strip(list[1])
        for x in range(2, (len(list))):
            value= str.strip(list[x])
            if year not in results:
                results.update({year: {letter: value}},)
            elif state not in results[year].keys():
                results[year].update({letter:value}) 
            else:
                results[year][letter]=[].append(value)
print(results)

我无法弄清楚为什么列表给我返回None。如果我尝试这样做:results[year].update({letter:(value})...

注意:上述代码中还存在一些其他错误,但您的问题似乎主要集中在列表的问题上。如果您想要帮助修复这个问题,我可以提供一些建议。

英文:

I'm trying to read a text file into a nested dictionary which further goes onto a tuple within a list.
The contents of the file look like this:

1976,A,001,58906,98257,002,66288,90069,003,106935,0,004,141490,34531,005,113553,0,006,69384,92113,007,110496,0
1976,B,000,34194,83722
1976,C,001,68404,96397,002,106054,71765,003,88854,79162,004,92435,93154
1976,D,001,116217,52565,002,144780,22819,003,0,1,004,1,0
1976,E,001,160477,56539,002,88829,121290,003,139779,52075,004,75844,75193,005,103746,64008,006,86493,35359,007,147064,45863,008,122342,68374,009,116398,44607,010,111992,38088,011,107618,62435,012,61526,130332,013,135291,63130,014,123285,46674,015,92735,35700,016,104545,91160,017,103898,54270,018,56683,101658,019,68722,124201,020,71193,146158,021,101837,44094,022,68543,114769,023,130619,86434,024,108296,51478,025,57966,17737,026,59093,112619,027,94988,114623,028,114612,28303,029,82515,10852,030,82767,28503,031,83155,0,032,92034,35394,033,77807,95398,034,100988,98147,035,87472,76765,036,90830,49368,037,49021,133634,038,103317,59092,039,86745,122657,040,102132,148512,041,94590,128784,042,103062,32565,043,93475,173576
.... and so, on..

I had to achieve an output like this:

{
    '1976': {'A': [VALUES("001", 58906, 98257),
                         VALUES("002", 66288, 90069),
                         VALUES("003", 106935, 0),
                         VALUES("004", 141490, 34531),
                        VALUES("005", 113553, 0),
                         VALUES("006", 69384, 92113),
                         VALUES("007", 110496, 0)],
            'B':   [VALUES("000", 34194, 83722)],
            'C':  [VALUES("001", 68404, 96397),
                         VALUES("002", 106054, 71765),
                         VALUES("003", 88854, 79162),
                         VALUES("004", 92435, 93154)],
            ...etc 
        }

The code that I have currently written is:

import csv
results = {}
with open("file.txt", 'r') as f:
    for line in f:
        list = line.split(",")
        year= str.strip(list[0])
        letter=str.strip(list[1])
        for x in range(2, (len(list))):
            value= str.strip(list[x])
            if year not in results:
                results.update({year: {letter: value}},)
            elif state not in results[year].keys():
                results[year].update({letter:value}) 
            else:
                results[year][letter]=[].append(value)
print(results)

The output which I get is:

{'1976': {'A': None, 'B': None, 'C': None...} and so on

I can't figure out why the lists are giving me none. If I try this : results[year].update({letter:(value})

答案1

得分: 0

预期和实际输出:

{'1976': {'A': ['001', '58906', '98257', '002', '66288', '90069', '003', '106935', '0', '004', '141490', '34531', '005', '113553', '0', '006', '69384', '92113', '007', '110496', '0 1976', 'B', '000', '34194', '83722 1976', 'C', '001', '68404', '96397', '002', '106054', '71765', '003', '88854', '79162', '004', '92435', '93154 1976', 'D', '001', '116217', '52565', '002', '144780', '22819', '003', '0', '1', '004', '1', '0 1976', 'E', '001', '160477', '56539', '002', '88829', '121290', '003', '139779', '52075', '004', '75844', '75193', '005', '103746', '64008', '006', '86493', '35359', '007', '147064', '45863', '008', '122342', '68374', '009', '116398', '44607', '010', '111992', '38088', '011', '107618', '62435', '012', '61526', '130332', '013', '135291', '63130', '014', '123285', '46674', '015', '92735', '35700', '016', '104545', '91160', '017', '103898', '54270', '018', '56683', '101658', '019', '68722', '124201', '020', '71193', '146158', '021', '101837', '44094', '022', '68543', '114769', '023', '130619', '86434', '024', '108296', '51478', '025', '57966', '17737', '026', '59093', '112619', '027', '94988', '114623', '028', '114612', '28303', '029', '82515', '10852', '030', '82767', '28503', '031', '83155', '0', '032', '92034', '35394', '033', '77807', '95398', '034', '100988', '98147', '035', '87472', '76765', '036', '90830', '49368', '037', '49021', '133634', '038', '103317', '59092', '039', '86745', '122657', '040', '102132', '148512', '041', '94590', '128784', '042', '103062', '32565', '043', '93475', '173576']}}

我运行了你的代码,第13行的变量state未定义。此外,在if语句的else块中,你正在替换之前存储在results[year][letter]中的列表。为了修复这个问题,只需将值直接附加到列表中,而不创建一个新列表,像这样:results[year][letter].append(value)

可工作的代码

英文:

Expected and Achieved Output:

{'1976': {'A': ['001', '58906', '98257', '002', '66288', '90069', '003', '106935', '0', '004', '141490', '34531', '005', '113553', '0', '006', '69384', '92113', '007', '110496', '0 1976', 'B', '000', '34194', '83722 1976', 'C', '001', '68404', '96397', '002', '106054', '71765', '003', '88854', '79162', '004', '92435', '93154 1976', 'D', '001', '116217', '52565', '002', '144780', '22819', '003', '0', '1', '004', '1', '0 1976', 'E', '001', '160477', '56539', '002', '88829', '121290', '003', '139779', '52075', '004', '75844', '75193', '005', '103746', '64008', '006', '86493', '35359', '007', '147064', '45863', '008', '122342', '68374', '009', '116398', '44607', '010', '111992', '38088', '011', '107618', '62435', '012', '61526', '130332', '013', '135291', '63130', '014', '123285', '46674', '015', '92735', '35700', '016', '104545', '91160', '017', '103898', '54270', '018', '56683', '101658', '019', '68722', '124201', '020', '71193', '146158', '021', '101837', '44094', '022', '68543', '114769', '023', '130619', '86434', '024', '108296', '51478', '025', '57966', '17737', '026', '59093', '112619', '027', '94988', '114623', '028', '114612', '28303', '029', '82515', '10852', '030', '82767', '28503', '031', '83155', '0', '032', '92034', '35394', '033', '77807', '95398', '034', '100988', '98147', '035', '87472', '76765', '036', '90830', '49368', '037', '49021', '133634', '038', '103317', '59092', '039', '86745', '122657', '040', '102132', '148512', '041', '94590', '128784', '042', '103062', '32565', '043', '93475', '173576']}}

I ran your code and the variable state in line 13 is not defined. Also, in the else block of the if statement, you are replacing the list that was previously stored in results[year][letter]. To fix this, just append the value directly to the list (don't create a new one) like so: results[year][letter].append(value).

Working Code

import csv

results = {}
with open("yearsandvalues.txt", 'r') as f:
    for line in f:
        list = line.split(",")
        year = str.strip(list[0])
        letter = str.strip(list[1])
        for x in range(2, (len(list))):
            value = str.strip(list[x])
            if year not in results:
                results.update({year: {letter: [value]}},)
            elif letter not in results[year].keys():
                results[year].update({letter: [value]})
            else:
                results[year][letter].append(value)
print(results)

huangapple
  • 本文由 发表于 2023年2月18日 12:45:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定