输入:
dict1 = {'Category': ['item1','item2','item3','item4'],
'Freq': [71984.0, 8129.0, 3140.0, 0.0]}
输出:
dict2 = {'Category': ['item1','item2','item3'],
'Freq': [71984.0, 8129.0, 3140.0]}
要执行的任务:
制作一个新字典,其中不包含所有0.0频率值和类别中的各个项目。
答案 0 :(得分:2)
这是使用* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1m 59s
Command: C:\Users\IB\Desktop\Android\myapp\berry_networks\Dart test\Scaffold\intro_to_scaffold\android\gradlew.bat app:properties
Please review your Gradle project setup in the android/ folder.
Exited (sigterm)`
例如:
zip
输出:
dict1 = {'Category': ['item1','item2','item3','item4'],'Freq': [71984.0, 8129.0, 3140.0, 0.0]}
Category, Freq = zip(*[(m,n) for m,n in zip(*dict1.values()) if n])
result = {"Category":Category, "Freq":Freq}
print(result)
答案 1 :(得分:2)
如果可以使用pandas
,则可以执行以下操作:
首先通过将dict1
传递到DataFrame构造函数中来创建一个熊猫DataFrame:
import pandas as pd
df = pd.DataFrame(dict1)
print(df)
# Category Freq
#0 item1 71984.0
#1 item2 8129.0
#2 item3 3140.0
#3 item4 0.0
现在仅保留Freq!=0
中的行:
print(df[df['Freq']!=0])
# Category Freq
#0 item1 71984.0
#1 item2 8129.0
#2 item3 3140.0
要将其重新放入字典中:
dict2 = df[df['Freq']!=0].to_dict(orient='list')
print(dict2)
#{'Category': ['item1', 'item2', 'item3'], 'Freq': [71984.0, 8129.0, 3140.0]}
使用此方法的优点是可以轻松应用于两个以上的列表。
答案 2 :(得分:2)
下面的代码可以解决问题。
dict1 = {'Category': ['item1','item2','item3','item4'],
'Freq': [71984.0, 8129.0, 3140.0, 0.0]}
zipped = zip(*dict1.values())
filtered = filter(lambda x : x[1] != 0.0, zipped)
result = map(list, zip(*filtered))
print {
'Category': result[0],
'Freq': result[1]
}
答案 3 :(得分:2)
Rakesh答案的版本稍干净
frequency_key = 'Freq'
category_key = 'Category'
freq,category = zip(*[[(i),(j)] for i,j in zip(dict1.get(frequency_key),dict1.get(category_key)) if i!=0])
{category_key:list(category),frequency_key:list(freq)}
输出
{'Category': ['item1', 'item2', 'item3'], 'Freq': [71984.0, 8129.0, 3140.0]}
答案 4 :(得分:1)
dict1 = {
'Category': ['item1','item2','item3','item4'],
'Freq': [71984.0, 8129.0, 3140.0, 0.0]
}
indices_to_delete = []
for i, e in enumerate(dict1['Freq']):
if e == 0:
indices_to_delete.append(i)
dict2 = {
key: [
v
for i, v in enumerate(dict1[key])
if i not in indices_to_delete
]
for key in dict1.keys()
}
dict2
返回:
{'Category': ['item1', 'item2', 'item3'], 'Freq': [71984.0, 8129.0, 3140.0]}
答案 5 :(得分:1)
也许有一种更优雅的解决方法,但这确实可以解决问题:
<key>GADApplicationIdentifier</key>
<string>APP_KEY</string>
输出:
dict1 = {'Category': ['item1','item2','item3','item4'],
'Freq': [71984.0, 8129.0, 3140.0, 0.0]}
# create an empty list to save indices that you want to delete from both lists
del_ind = []
# iterate over the list of Frequencies
for x in dict1['Freq']:
# if the value euquals 0.0, append the index of this value to your list
if x == 0.0:
ind = dict1['Freq'].index(x)
del_ind.append(ind)
# then iterate over the list of indices you want to delete and use the pop method to delete those entries from both of your lists
for x in del_ind:
dict1['Category'].pop(x)
dict1['Freq'].pop(x)
print(dict1)