简体   繁体   English

如何为特定键值创建新字典?

[英]How to create new dictionaries for a specific key values?

For each element in the file (row), I need to create a new dictionary where the keys are the property names from the header row (Atomic Number, Atomic Name, etc) and the values are the values for each property.对于文件(行)中的每个元素,我需要创建一个新字典,其中键是 header 行中的属性名称(原子数、原子名称等),值是每个属性的值。 However, I cannot create specific dictionary for each element.但是,我无法为每个元素创建特定的字典。 My output is totally different from the given output.我的 output 与给定的 output 完全不同。 That's an assignment therefore, getting the whole code doesn't make sense.因此,这是一项任务,获取整个代码没有意义。 Can you tell me what my mistake is, how I can fix it?你能告诉我我的错误是什么,我该如何解决?

This is my short raw data of the inputted file:这是我输入文件的简短原始数据:

Chemical symbol     Name    Origin of symbol    Atomic Number   Atomic mass     Density Melting point   Boiling point   Year of discovery   Discoverer
Ac  Actinium        89  227.0278    10.07   1047    3197    1899    Debierne
Ag  Silver  Latin Argentum  47  107.8682    10.49   961.9   2212    prehistoric     unknown
Al  Aluminium       13  26.981539   2.70    660.5   2467    1825    Oersted
Am  Americium       95  243.0614    13.67   994     2607    1944    Seaborg
Ar  Argon       18  39.948  1.66 g/l    -189.4  -185.9  1894    Ramsay and Rayleigh

My short output is like this:我的短 output 是这样的:

|Chemical symbol    Name    Origin of symbol    Atomic Number   Atomic mass     Density Melting point   Boiling point   Year of discovery   Discoverer : Co     Cobalt      27  58.9332     8.89    1495    2870    1735    Brandt
|Chemical symbol    Name    Origin of symbol    Atomic Number   Atomic mass     Density Melting point   Boiling point   Year of discovery   Discoverer : Cr     Chromium        24  51.9961     7.14    1857    2482    1797    Vauquelin 

However, the desired output is like this:但是,想要的 output 是这样的:

    |Element Name       :   Hydrogen
    |Chemical symbol    :   H
    |Origin of symbol   :   
    |Atomic Number      :   1.00
    |Atomic mass        :   1.01
    |Density            :   0.084 g/l 
    |Melting point      :   -259.1
    |Boiling point      :   -252.9
    |Year of discovery  :    1766
    |Discoverer         :   Cavendish

Here is my code:这是我的代码:

class Element:
    def __init__(self, eName, elementData):
        self.__eName = str(eName)
        self.elementData = dict(elementData)
    
        for e in self.elementData:
            if isinstance(self.elementData[e],int) or isinstance(self.elementData[e],float):
                float(elementData[e])

    def __eq__(self,other):
        if self.__eName == other.__eName:
            return True
        else:
            return False
            
    def __lt__(self, other):
        if self.elementData['Atomic Number'] < other.elementData['Atomic Number']:
            return True
        else:
            return False
        
    def __repr__(self):
        for k in self.elementData:
            return "|"+ k +' : '+ self.elementData[k]+'\n'
        
    def get_property(self, property):
        return self.elementData[property]

import numpy as np
from Element import *

file = np.loadtxt("data.txt",dtype = "str", delimiter = "|t")

dict1 = {}

for i in range(1,len(file)):
    for col in np.row_stack((file[0],file[i])).T:
        key = col[0]
        dict1[key] = col[1]
    
    el_Name = dict1[key]
    elem = Element(el_Name, dict1)
    print(elem)

The delimiter character ( |t ) in this line is incorrect:此行中的分隔符 ( |t ) 不正确:

file = np.loadtxt("data.txt",dtype = "str", delimiter = "|t")

it should be ( \t ) to represent a tab character ( see docs ):它应该是( \t )来表示一个制表符(参见文档):

file = np.loadtxt("data.txt",dtype = "str", delimiter = "\t")

With the incorrect delimiter, each row is read as a single column.使用不正确的分隔符,每行都被读取为单个列。 You can see the consequences of this by printing the value of dict1 , which reveals there is only a single key-value pair:您可以通过打印dict1的值来查看其后果,这表明只有一个键值对:

el_Name = dict1[key]
print(dict1)  # print the value of dict1 for debugging/illustration purposes
elem = Element(el_Name, dict1)
output: {'Chemical symbol\tName\tAtomic Number\tAtomic mass\tDensity\tMelting point\tBoiling point\tYear of discovery\tDiscoverer': 'Ac\tActinium\t89\t227.0278\t10.07\t1047\t3197\t1899\tDebierne'}

After fixing the delimiter, the value of dict1 looks like this:固定分隔符后,dict1 的值如下所示:

output: {'Chemical symbol': 'Ac', 'Name': 'Actinium', 'Atomic Number': '89', 'Atomic mass': '227.0278', 'Density': '10.07', 'Melting point': '1047', 'Boiling point': '3197', 'Year of discovery': '1899', 'Discoverer': 'Debierne'}

Now there are key-value pairs for each property of the chemical.现在,化学品的每个属性都有键值对。


After fixing that, you might run into another issue in your __repr__ function.修复该问题后,您可能会在__repr__ function 中遇到另一个问题。 It loops over each of the chemical properties, but it returns early in the first iteration of the loop, so you'll only see the first property for each chemical.它循环遍历每个化学属性,但它会在循环的第一次迭代早期返回,因此您只会看到每种化学物质的第一个属性。

    def __repr__(self):
        for k in self.elementData:
            return "|"+ k +' : '+ self.elementData[k]+'\n'  # this returns immediately! we only see the first value from self.elementData

You could fix this by building up the properties one by one, then return outside of the for loop, like this:您可以通过逐个构建属性来解决此问题,然后在 for 循环之外返回,如下所示:

    def __repr__(self):
        s = ''
        for k in self.elementData:
            s += "|"+ k +' : '+ self.elementData[k]+'\n'
        return s

With those changes my output looks like this:通过这些更改,我的 output 看起来像这样:

(I had to remove the 'Origin of symbol' column because of missing values in the example data) (由于示例数据中缺少值,我不得不删除“符号来源”列)

|Chemical symbol : Ac
|Name : Actinium
|Atomic Number : 89
|Atomic mass : 227.0278
|Density : 10.07
|Melting point : 1047
|Boiling point : 3197
|Year of discovery : 1899
|Discoverer : Debierne

Building up on honk's answer .喇叭的回答为基础。

To get the pretty indentation you initially requested and make a more pythonic dictionary iteration :为了获得您最初要求的漂亮缩进并进行更 Pythonic 的字典迭代

def __repr__(self):
       maxKeyLength = max([len(k) for k in self.elementData])
       s = ''
       for key, value in self.elementData.items():
           s += f"|{key:maxKeyLength}  :  {value}\n"
       return s

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

相关问题 如何插入到特定键中的字典值列表 - How to insert to a list of dictionaries values in a specific key 如何从词典列表中的特定键中获取唯一值? - How to get unique values from a specific key in a list of dictionaries? 如何编辑字典列表中的特定字典键值? - How to Edit Specific Dictionary Key Values in List of Dictionaries? 如何在字典列表中找到特定键的唯一值? - How do I find unique values for a specific key in a list of dictionaries? 如何从两个具有相同键值的字典创建熊猫数据框? - How to create a pandas dataframe from two dictionaries with same key values? Python:如何从具有特定值的列表中创建嵌套字典 - Python: How to create nested dictionaries out of lists with specific values 如何遍历嵌套字典并从包含特定键的子字典中提取键和值? - How to loop through nested dictionaries and extract keys and values from sub-dictionaries containing a specific key? 字典列表中特定键的值总和 python - Sum of values for specific key in a list of dictionaries python 为合并字典的每个键添加新值 - add new values for each key of merged dictionaries 从 Python 中的字典列表中创建以特定属性为键的新字典的简洁方法 - Concise way to create new dictionary with specific property as key, from list of dictionaries in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM