Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在字典中选择项目_Python - Fatal编程技术网

Python 在字典中选择项目

Python 在字典中选择项目,python,Python,我有下面的字典 d={“月”:[1,2,3,4,5,6],“雨”:[30,40,50,20,30,70]} 我想按月份选择特定的雨,所以我得到 比如说 new_d={“月”:[2,4,6],“雨”:[40,20,70]} 我一直在思考代码的这一方面,我对编码还是新手。请帮助我,如果没有字典理解,我怎么做 for key in d: if filter_string in key: # do something else: # do nothing,

我有下面的字典

d={“月”:[1,2,3,4,5,6],“雨”:[30,40,50,20,30,70]}

我想按月份选择特定的雨,所以我得到

比如说

new_d={“月”:[2,4,6],“雨”:[40,20,70]}

我一直在思考代码的这一方面,我对编码还是新手。请帮助我,如果没有字典理解,我怎么做

for key in d:
    if filter_string in key:
        # do something
    else:
        # do nothing, continue
见下文

d = {"Month": [1, 2, 3, 4, 5, 6], "Rain": [30, 40, 50, 20, 30, 70]}
interesting_month = [2, 4, 6]
d1 = {'Month': [m for m in interesting_month], 'Rain': [d["Rain"][d['Month'].index(m)] for m in interesting_month]}
print(d1)
输出

{'Month': [2, 4, 6], 'Rain': [40, 20, 70]}

首先使用for循环,然后作为理解-作为前面答案的补充:

d = {"Month":[1,2,3,4,5,6],"Rain":[30,40,50,20,30,70]}
#new_d = {"Month":[2,4,6],"Rain":[40,20,70]}
mths_to_choose = [2,4,6]

for keyval in d.items():
    new_d[keyval[0]] = []
    for i in mths_to_choose: # if filter_string in key: # do something
        new_d[keyval[0]].append(keyval[1][i-1])
    # else# do nothing, continue  
print(new_d)

new_d = {ditms[0]: [ditms[1][i-1] for i in mths_to_choose] for ditms in d.items()}
print(new_d)
输出:

{'Month': [2, 4, 6], 'Rain': [40, 20, 70]}
{'Month': [2, 4, 6], 'Rain': [40, 20, 70]}

我希望这可能会有所帮助

d = {"Month": [1, 2, 3, 4, 5, 6], "Rain": [30, 40, 50, 20, 30, 70]}

#Creating a new dictionary
new_d = {}

#First Loop: will get the keys from your original dictionary
for keys in d:

    #new_d[keys] = [] - will create an array value for each key found
    new_d[keys] = []

    #Second Loop: will get the values from the array on each key
    for values in d[keys]:

        #If the position of "values" is odd then is inserted on new_d[keys] with the append() method
        if d[keys].index(values) % 2 != 0:
            new_d[keys].append(values)

#Print the outcome
print(new_d)

a:谢谢,如果我的月份是这样的呢?={“月”:[2013-04-012013-07-012014-03-012014-10-012014-02-012014-04-01],“雨”:[30,40,50,20,30,70]}@Rich-试试看会发生什么。如果我有约会呢。d={“日期”:[2013-04-01,2013-07-01,2014-03-01,2014-10-01,2014-02-01,2014-04-01],“雨”:[30,40,50,20,30,70]?只需将d={“月份”:[1,2,3,4,5,6],“雨”:[30,40,50,20,30,70]}替换为d={“日期”:[2013-04-01,2013-07-01,2014-03-01,2014-04-01],“雨”:[40,50,70]:如果我有一个日期而不是月份d={“month”:[2013-04-012013-07-012014-03-012014-10-012014-02-012014-04-01],“Rain”:[30,40,50,20,30,70]}你能为这段代码添加一些解释吗?@Rich只需将“month”的新值替换为[2013-04-012013-07-012014-03-01,2014-01,2014-10-01,2014-02-01,2014-04-01]并相应地调整所选的月份。
d = {"Month":[1,2,3,4,5,6], "Rain":[30,40,50,20,30,70]} 
#Desired Output: {"Month":[2,4,6],"Rain":[40,20,70]}

selected_month = [2,4,6] #list of months

new_d = {} #create a new dictionary to store selected month and rain

for key in d.items(): 
    new_d[key[0]] = [] 
    for i in selected_month: 
        new_d[key[0]].append(key[1][i-1])

print(new_d)