格式化具有相等值的字典 Json Python
Formatting dictionaries with Equal values Json Python
我正在尝试修改 data
字典,以便只有 dictionary
中的 RSI, MOM
名称保留在 data
字典中。我如何创建一个函数来过滤和查找字典 data
和 dictionary
中的等效名称,然后删除其他所有内容?
代码:
dictionary= {'Account1': {'RSI': OrderedDict([('Exchange', 'Bybit'), ('AccountName', 'Account1'), ('StrategyName', 'RSI'), ('Script', 'MomentumStrats'), ('StratStatus', 'ACTIVE')]),'MOM': OrderedDict([('Exchange', 'Bybit'), ('AccountName', 'Account1'), ('StrategyName', 'MOM'), ('Script', 'MomentumStrats'), ('StratStatus', 'ACTIVE')])}, 'Account2': {}}
def reading():
with open('data.json') as f:
data = json.load(f)
return data
reading()
预期输出:
{
"RSI": [
{
"TradingPair": "BTCUSD",
"fetchSubscriptions": [0],
}
],
"MOM":[
{
"TradingPair": "BCHUSDT",
"fetchSubscriptions": [0],
}
]
}
JSON 文件:
{
"RSI": [
{
"TradingPair": "BTCUSD",
"fetchSubscriptions": [0],
}
],
"MOM":[
{
"TradingPair": "BCHUSDT",
"fetchSubscriptions": [0],
}
],
"MOM_RSI":[
{
"TradingPair": "BTCUSDT",
"fetchSubscriptions": [0],
}
]
}
您可以使用keys()
方法从dictionary
中获取相关名称。把它做成一套,这样我们就可以快速查看内容。
required_names = set(dictionary["Account1"].keys())
或在任何帐户下:
required_names = {key for account in dictionary for key in dictionary[account]}
然后您可以使用 dict-comprehension 仅过滤那些键。类似于:
filtered_data = {
key: value
for key, value in data.items()
if key in required_keys
}
您可以先为 dictionary
中的唯一值创建一个 set
,然后遍历 data
并删除不在唯一集中的所有键。所以,
set_unique = set()
for v in dictionary.values():
for k in v.keys():
set_unique.add(k)
print(set_unique) # Output: {'MOM', 'RSI'}
for key in list(data.keys()):
if key not in set_unique:
del data[key]
print(data) # Output: {'RSI': [{'TradingPair': 'BTCUSD', 'fetchSubscriptions': '[0]'}], 'MOM': [{'TradingPair': 'BCHUSDT', 'fetchSubscriptions': '[0]'}]}
我正在尝试修改 data
字典,以便只有 dictionary
中的 RSI, MOM
名称保留在 data
字典中。我如何创建一个函数来过滤和查找字典 data
和 dictionary
中的等效名称,然后删除其他所有内容?
代码:
dictionary= {'Account1': {'RSI': OrderedDict([('Exchange', 'Bybit'), ('AccountName', 'Account1'), ('StrategyName', 'RSI'), ('Script', 'MomentumStrats'), ('StratStatus', 'ACTIVE')]),'MOM': OrderedDict([('Exchange', 'Bybit'), ('AccountName', 'Account1'), ('StrategyName', 'MOM'), ('Script', 'MomentumStrats'), ('StratStatus', 'ACTIVE')])}, 'Account2': {}}
def reading():
with open('data.json') as f:
data = json.load(f)
return data
reading()
预期输出:
{
"RSI": [
{
"TradingPair": "BTCUSD",
"fetchSubscriptions": [0],
}
],
"MOM":[
{
"TradingPair": "BCHUSDT",
"fetchSubscriptions": [0],
}
]
}
JSON 文件:
{
"RSI": [
{
"TradingPair": "BTCUSD",
"fetchSubscriptions": [0],
}
],
"MOM":[
{
"TradingPair": "BCHUSDT",
"fetchSubscriptions": [0],
}
],
"MOM_RSI":[
{
"TradingPair": "BTCUSDT",
"fetchSubscriptions": [0],
}
]
}
您可以使用keys()
方法从dictionary
中获取相关名称。把它做成一套,这样我们就可以快速查看内容。
required_names = set(dictionary["Account1"].keys())
或在任何帐户下:
required_names = {key for account in dictionary for key in dictionary[account]}
然后您可以使用 dict-comprehension 仅过滤那些键。类似于:
filtered_data = {
key: value
for key, value in data.items()
if key in required_keys
}
您可以先为 dictionary
中的唯一值创建一个 set
,然后遍历 data
并删除不在唯一集中的所有键。所以,
set_unique = set()
for v in dictionary.values():
for k in v.keys():
set_unique.add(k)
print(set_unique) # Output: {'MOM', 'RSI'}
for key in list(data.keys()):
if key not in set_unique:
del data[key]
print(data) # Output: {'RSI': [{'TradingPair': 'BTCUSD', 'fetchSubscriptions': '[0]'}], 'MOM': [{'TradingPair': 'BCHUSDT', 'fetchSubscriptions': '[0]'}]}