简体   繁体   English

如何将列表转换为字典,其中第一个列表是键,第二个列表是字典中的值?

[英]how to convert the list to dictionary where first list is key and second list is value in dictionary?

I was trying following question我正在尝试以下问题

I tried all my best but got stucked at the end part to convert the list to dictionary given in question above ie {1.3:[1.2,1.4]} here is how i did my solution for above question.我尽我所能,但在最后部分被卡住了,将列表转换为上面给出的字典,即{1.3:[1.2,1.4]}这是我如何解决上述问题的方法。

lst=[]
l=[1.3]
k=[1.2,1.4,1.5,1.6,2.3,3.4,3.6,3.8,4.2,5.4,5.8]
for x in range(len(l)):
    for z in range(len(k)):
        b=k[z]-l[x]
        if b>0.1:
            l.append(k[z])
        elif b<=0.1:
            print(lst.append(k[z]))
print(lst)
print(l)

how can I convert the list to dictionary at the end?最后如何将列表转换为字典? ie {1.3:[1.2,1.4]}即 {1.3:[1.2,1.4]}

Check this,检查这个,

lst=dict() # create an empty dictionary
l=[1.3]
k=[1.2,1.4,1.5,1.6,2.3,3.4,3.6,3.8,4.2,5.4,5.8]
for x in range(len(l)):
    lst[str(l[x])] = [] # create a new dict entry with a key from l and an empty list
    for z in range(len(k)):
        b=k[z]-l[x]
        if b>0.1:
            l.append(k[z])
        elif b<=0.1:
            print(lst[str(l[x])].append(k[z]))  # add to the empty list inside the dictionary of corresponding entry in l that you are checking with
print(lst)
print(l)
{'1.3': [1.2, 1.4]}
[1.3, 1.5, 1.6, 2.3, 3.4, 3.6, 3.8, 4.2, 5.4, 5.8]

I finally used fromkey() method and it solved my question我终于使用了fromkey()方法,它解决了我的问题

new_dict=[]
compare_value = 0.1
list_main=[1.3]
test_set=[1.2,1.4,1.5,1.7,2.3,3.4,3.6,3.8,4.2,5.4,5.8]
for x in range(len(list_main)):
   for z in range(len(test_set)):
       diff=test_set[z]-list_main[x]
       if diff>compare_value:
           list_main.append(test_set[z])
       elif diff<=compare_value:
           new_dict.append(test_set[z])
           dictionary=dict.fromkeys(list_main, new_dict)
print('dictionary----',dictionary)
print('list-------',list_main)

暂无
暂无

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

相关问题 如何将列表转换为键和值都相同的字典? - How to convert a list to a dictionary where both key and value are the same? 如何将两个列表的值存储在单个字典中,其中第一个列表的值是键,第二个列表的值是属性? - How to store the values of two lists in a single dictionary where values of first list is a key and the values of second list are attributes? 如何将具有键值对的列表转换为字典 - How to convert a list with key value pairs to dictionary 如何将三元素字典列表(第一个元素:术语,第二个元素:位置,第三个:值)转换为两个元素字典的列表 - How do I convert list of three element dictionary (First element: term,Second Element: position,Third : Value) into list of two element dictionary 如何将列表中的键值对列表转换为python中的字典 - How to convert list of key value pair in list to dictionary in python 按键将列表转换为字典 - Convert List to dictionary by key 如何根据条件将第二个字典列表的键及其值添加到字典的第一个列表中 - How shall I add one of the key and its values of second list of dictionary to the first list of dictionary based on the condition 如何获取字典键列表中第二项的最低值? - How to get the lowest value of second item in key list of dictionary? 如何从列表中向字典键添加第二个值? - How to add second value to dictionary key from list? 如何在以lst [0] [0]为键的Python中将嵌套列表转换为字典 - How to Convert Nested List into dictionary in Python where lst[0][0] is the key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM