Getting a key of Nested Dic using multiple values criteria

Mcavalca

I have the following Nested Dictionary:

go._Order_Data_DB.items()

Out[62]: dict_items([(84852344, {'_action': 'OPEN', '_type': 0, '_symbol': 'EURUSD', '_price': 0.0, '_SL': 50, '_TP': 50, '_comment': 'DWX_Python_to_MT', '_lots': 0.01, '_magic': 123456, '_ticket': 0}), (84852345, {'_action': 'CLOSE', '_type': 0, '_symbol': 'EURUSD', '_price': 0.0, '_SL': 50, '_TP': 50, '_comment': 'DWX_Python_to_MT', '_lots': 0.01, '_magic': 123456, '_ticket': 84852345}), 
(84852374, {'_action': 'OPEN', '_type': 0, '_symbol': 'GBPUSD', '_price': 0.0, '_SL': 50, '_TP': 50, '_comment': 'DWX_Python_to_MT', '_lots': 0.01, '_magic': 123456, '_ticket': 84852345})])

I would like to retrieve the Order Numbers (key) if two conditions are met: '_action' == 'OPEN' and '_symbol' == ccy. I tried using the function below but it only looks at the first criteria, would be great if someone knows how to make both criteria work, as it seems to be ignoring the 'and'.

def get_order_num(ccy): 
    for k, v in go._Order_Data_DB.items():
        for k1, v1 in v.items():
            if v1 == ccy and v1 == 'OPEN':
                return(k)
jpp

There are two main issues:

  1. You don't need a nested loop to check specific keys in your sub-dictionaries. Your if statement can query keys from the values of your outer dictionary.
  2. If multiple sub-dictionaries satisfy your criteria, only the first will be captured by return. To extract all items satisfying your conditions, yield results instead and then exhaust your generator via list.

Here's a demonstration;

d = dict([(84852344, {'_action': 'OPEN', '_type': 0, '_symbol': 'EURUSD', '_price': 0.0, '_SL': 50, '_TP': 50, '_comment': 'DWX_Python_to_MT', '_lots': 0.01, '_magic': 123456, '_ticket': 0}),
          (84852345, {'_action': 'CLOSE', '_type': 0, '_symbol': 'EURUSD', '_price': 0.0, '_SL': 50, '_TP': 50, '_comment': 'DWX_Python_to_MT', '_lots': 0.01, '_magic': 123456, '_ticket': 84852345}), 
          (84852374, {'_action': 'OPEN', '_type': 0, '_symbol': 'GBPUSD', '_price': 0.0, '_SL': 50, '_TP': 50, '_comment': 'DWX_Python_to_MT', '_lots': 0.01, '_magic': 123456, '_ticket': 84852345})])

def get_order_num(ccy): 
    for k, v in d.items():
        if v['_symbol'] == ccy and v['_action'] == 'OPEN':
            yield k

res1 = list(get_order_num('EURUSD'))  # [84852344]
res2 = list(get_order_num('GBPUSD'))  # [84852374]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Powershell - Getting multiple values from a nested object

Seeking excel formula based on multiple criteria using a key

Search nested json / dict for multiple key values matching specified keys

Multiple Values using single Criteria

key value pattern with multiple criteria using positive look overhead

Using multiple search criteria in SQLite LIKE clause based on a nested SELECT

Getting distinct values using tiered criteria

Split multiple nested key,values and extract to text file

Search using multiple criteria

SUMIFS with Multiple Criteria with Multiple Values

Return and sum multiple values using a wildcard criteria

Returning multiple values using multiple matching criteria

code to filter values using multiple criteria

Getting values from nested JSON using AngularJS

Getting key of unique values in a nested dict within lists

Multi-criteria search and returning multiple values using array functions

Python append multiple values to nested dictionary by key

countif unique values using multiple criteria

Getting multiple Instances of key using bsearch()

Using Countifs to count distinct values and multiple criteria?

Using nested lapply to filter for criteria and create multiple data frames

DCount using Multiple Criteria

Getting Key in Nested Dictionary

How to sum multiple values using offset based off criteria?

Getting multiple values per group based on two criteria

Getting values from nested JSON with a dynamic key in a database

Grouping values in a column by a criteria and getting their mean using Python / Pandas

AttributeError: 'tuple' object has no attribute 'values' getting error while concatenate multiple nested dictionaries using single function

How to change multiple values in a json using jq by selecting them by key and getting whole json output

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive