简体   繁体   English

带有字典的嵌套列表中的Python元素

[英]Python elements in nested lists withing a dictionary

I have a project that requires me to read an Excel worksheet with openpyxl. 我有一个项目,要求我使用openpyxl阅读Excel工作表。 In the worksheet I need to find several columns that have similar names, read the data in each column, and compare the values on each row, keeping only the rows that have identical values and storing a blank in the other rows. 在工作表中,我需要找到几列名称相似的列,读取每一列中的数据,并比较每一行的值,仅保留具有相同值的行,并在其他行中存储空白。

I don't know how many columns are going to match the input string the user enters, and I don't know how many rows are going to be in the spreadsheet, so I've created a dictionary with the column headers as keys and the column values as lists. 我不知道有多少列将与用户输入的输入字符串匹配,我也不知道电子表格中将有多少行,因此我创建了一个字典,其中以列标题为键,列值作为列表。

How do I compare the values row-by row to find out if the values in each list match? 如何逐行比较值以找出每个列表中的值是否匹配?

Here's the code that I have: 这是我的代码:

for row in range(1, num_rows):
        for cell in range(1, num_cols):
            header_row = str(work_sheet.cell(1, cell).value)
            cell_val = work_sheet.cell(row, cell).value
            cell_list.append(cell_val)
            if header_row in col_dict:
                col_dict[header_row].append(cell_val)
            else:
                col_dict[header_row] = [cell_val]

This gives me a dictionary like so: 这给了我这样的字典:

{
    'Col B': ['Col B', 'x', 'x', None, 'x', 'x', 'x'], 
    'Col C': ['Col C', 'x', 'x', 'None', 'x', 'None', 'None'],
    'Col A': ['Col A', 'x', 'x', 'None', 'x', 'None', 'x']
}

I want to compare the elements at corresponding indices in each list(if ColB[1]=='x' and ColA[1]=='x' and ColC[1]=='x' , put an 'x' in output_list . Else, put a "" in that index of the output list. I can see the lists, but how do I compare the elements in them? 我想比较每个列表中对应索引处的元素(如果ColB[1]=='x'ColA[1]=='x'ColC[1]=='x' ,则在其中添加一个'x' output_list 。否则,在输出列表的索引中放置一个"" ,我可以看到列表,但是如何比较它们中的元素呢?

for key in col_dict.keys():
        for i in range(len(col_dict[key])):
            print(col_dict[key][i])

I eventually figured out the answer: I created a new list, then iterated through each column. 我最终弄清楚了答案:我创建了一个新列表,然后遍历每一列。 If a value in the column matched 'x', I wrote 'x' to the new list. 如果该列中的值与“ x”匹配,则将“ x”写入新列表。 If the value did not match 'x', or if the value in the the new list was 'None,' I placed 'None' in the new dict. 如果该值与“ x”不匹配,或者新列表中的值为“ None”,则在新字典中放置“ None”。 The else clause handles the first copies the first list in the dictionary into the new list so the other dictionary entries are compared to the new list. else子句处理第一个将字典中的第一个列表复制到新列表中,以便将其他字典条目与新列表进行比较。 Here's the solution: 解决方法如下:

                            if cell_val is None:
                                cell_val = ''
                            new_list.append(cell_val)
                            if column_name in new_list:
                                new_list[column_name].append(cell_val)
                            else:
                                new_list[column_name] = [cell_val]

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

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