简体   繁体   English

创建值是列表的字典

[英]Create dictionary whose value is a list

I'm trying to create a dictionary containing lists, based on a 'lists [i] [1]' key.我正在尝试基于'lists [i] [1]'键创建一个包含列表的字典。

lists = [['Col',1,5,9],
         ['Col',1,8,9],
         ['Col',2,1,9],
         ['Row',2,9,9],
         ['Row',2,7,8],
         ['Row',3,7,9],
         ['Row',3,6,9]]

dict_pis = {}
z_req_list = []
for i in range(len(lists)):

    if lists[i][0] == 'Row':

        key = lists[i][1]
        muv = lists[i][2]/lists[i][3]
        z_req = (muv / (0.9 * 25310506.54))*10000
#        z_req_list.append(z_req)
        dict_pis[key] = z_req
#        dict_pis[key] = z_req_list
        print(i+1,5*' ',key,5*' ', round(z_req,6))
print(dict_pis)
#        print(i+1,5*' ',key,5*' ', z_req_list)

So I should have something like this:所以我应该有这样的东西:

dict_pis = {2: [0.000439, 0.000384], 3: [0.000341, 0.000293]}

But what I get is the last value for each key, how can that be solved?但是我得到的是每个键的最后一个值,如何解决? I have tried to create an empty list to host my values by key, but it did not work for me.我试图创建一个空列表来按键托管我的值,但它对我不起作用。

I really appreciate the help, kind regards.我非常感谢您的帮助,亲切的问候。

You can use a defaultdict orsetdefault() to create a list for a key by default.默认情况下,您可以使用defaultdictsetdefault()为键创建列表。 It will also be a little clearer if you don't use indices in the loop, but just loop over the items:如果你不在循环中使用索引,它也会更清楚一点,而只是循环项目:

lists = [['Col',1,5,9],
         ['Col',1,8,9],
         ['Col',2,1,9],
         ['Row',2,9,9],
         ['Row',2,7,8],
         ['Row',3,7,9],
         ['Row',3,6,9]]

d = {}
for l in lists:
    kind, k, x, y = l
    if kind == "Row":
        d.setdefault(k, []).append(10000 * x / y / (0.9 * 25310506.54))

print(d)
# {2: [0.00043899204836345053, 0.00038411804231801923],
#  3: [0.0003414382598382393, 0.0002926613655756337]}

It's not clear from the question if you want the rounded values in the dict, but I think you know how to do that it you want it.从这个问题中不清楚你是否想要字典中的四舍五入值,但我认为你知道如何去做你想要的。

You have to check if there is list for current key and eventually create it您必须检查是否有当前key的列表并最终创建它

    if key not in dict_pis:
        dict_pis[key] = []

    dict_pis[key].append(z_req)

lists = [['Col',1,5,9],
         ['Col',1,8,9],
         ['Col',2,1,9],
         ['Row',2,9,9],
         ['Row',2,7,8],
         ['Row',3,7,9],
         ['Row',3,6,9]]

dict_pis = {}

for i in range(len(lists)):
    if lists[i][0] == 'Row':
        key = lists[i][1]
        muv = lists[i][2]/lists[i][3]

        z_req = (muv / (0.9 * 25310506.54))*10000

        if key not in dict_pis:
            dict_pis[key] = []

        dict_pis[key].append(z_req)

        print(i+1,5*' ',key,5*' ', round(z_req,6))

print(dict_pis)

Another solution with dictionary comprehension:字典理解的另一种解决方案:

lists = [['Col',1,5,9],
         ['Col',1,8,9],
         ['Col',2,1,9],
         ['Row',2,9,9],
         ['Row',2,7,8],
         ['Row',3,7,9],
         ['Row',3,6,9]]
def z_req(x, y):
    """ Calculate z req """
    muv = x / y
    return (muv / (0.9 * 25310506.54))*10000

# create dictionary 
dic = { value[1] : [] for value in lists }

# append z_req results
for type, key, x, y in lists:
    if type == 'Row':
        dic[key].append(z_req(x, y))

Just for fun, in one dictionary comprehension;只是为了好玩,在一本字典里理解;

def z_req(x, y):
    muv = x / y
    return (muv / (0.9 * 25310506.54))*10000

dic = { value[1] : [ z_req(v[2], v[3]) for v in lists \
                    if value[1] == v[1] and v[0] == 'Row' ] \
        for value in lists }

Although, you do have a problem with 1;虽然,您确实对 1 有疑问;

{1: [],
 2: [0.00043899204836345053, 0.00038411804231801923],
 3: [0.0003414382598382393, 0.00029266136557563367]}

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

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