如何在key:value对的values部分中定位特定元素?

时间:2019-05-05 09:10:03

标签: python list dictionary

我要按如下所示将键:值对中的特定元素作为目标,但我很困惑。我想求和并返回所有带有键的元素的值作为“项”。

[{  'actcost': 5.56,
    'bnfcode': '0101010G0AAABAB',
    'bnfname': 'Co-MagaldroxSusp 195mg/220mg/5ml S/F',
    'items': 2,
    'nic': 5.98,
    'practice': 'N81013',
    'quantity': 1000},
{   'actcost': 2.78,
    'bnfcode': '0101010G0AAABAB',
    'bnfname': 'Co-MagaldroxSusp 195mg/220mg/5ml S/F',
    'items': 1,
    'nic': 2.99,
    'practice': 'N81029',
    'quantity': 500},
{   'actcost': 5.56,
    'bnfcode': '0101010G0AAABAB',
    'bnfname': 'Co-MagaldroxSusp 195mg/220mg/5ml S/F',
    'items': 2,
    'nic': 5.98,
    'practice': 'N81088',
    'quantity': 1000}]

2 个答案:

答案 0 :(得分:1)

所以你先

DECLARE @NumberRecords INT
DECLARE @RowCount INT
DECLARE @Name NVARCHAR(MAX)
DECLARE @Command NVARCHAR(MAX)
DECLARE @Result int
DECLARE @Names TABLE (
    [RowId] INT NOT NULL    IDENTITY(1, 1),
    [Name]  NVARCHAR(MAX),
    [Type]  NVARCHAR(MAX)
)

INSERT INTO @Names
SELECT
        QUOTENAME(SCHEMA_NAME([Objects].schema_id)) + '.' + QUOTENAME(OBJECT_NAME([Objects].object_id)) [Name],
        type_desc [Type]
FROM sys.objects [Objects]
WHERE type_desc IN ('SQL_STORED_PROCEDURE',
                    'SQL_TRIGGER',
                    'SQL_SCALAR_FUNCTION',
                    'SQL_TABLE_VALUED_FUNCTION',
                    'SQL_INLINE_TABLE_VALUED_FUNCTION',
                    'VIEW')
ORDER BY [Name]

SET @RowCount = 1
SET @NumberRecords = (SELECT COUNT(*) FROM @Names)
WHILE (@RowCount <= @NumberRecords)
BEGIN

    SELECT @Name = [Name]
    FROM @Names
    WHERE [RowId] = @RowCount

    SET @Command = N'EXEC sp_refreshsqlmodule ''' + @Name + ''''

    BEGIN TRY

        EXEC @Result = sp_executesql @Command

        IF @Result <> 0
        BEGIN

            RAISERROR('Failed', 16, 1)

        END
        ELSE
        BEGIN

            IF (NOT EXISTS (SELECT *
                            FROM sys.dm_sql_referenced_entities(@Name, 'OBJECT')
                            WHERE [is_incomplete] = 1))
            BEGIN

                DELETE
                FROM @Names
                WHERE [RowId] = @RowCount

            END

        END

    END TRY
    BEGIN CATCH

        -- Nothing

    END CATCH

    SET @RowCount = @RowCount + 1

END

SELECT  [Name],
        [Type]
FROM @Names

该字符串,然后进行简单循环

obj = json.loads(txt)

可以缩写为

result = 0
for dct in obj:
    result += dct.get('items', 0)

如果您想对其他字段求和,则

result = sum(dct.get('items', 0) for dct in obj)

可以缩写为

result = 0
for dct in obj:
    if 'items' in dct:
        result += dct['quantity']

答案 1 :(得分:0)

您可以使用ipython或闲置代码段:

vagrant@vagrant:~$ ipython
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.4.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: d = [{'bnfcode': '0101010G0AAABAB', 'items': 2, 'practice': 'N81013', 'bnfname': 'Co-MagaldroxSusp 195mg/220mg/5m
   ...: l S/F', 'nic': 5.98, 'actcost': 5.56, 'quantity': 1000}, {'bnfcode': '0101010G0AAABAB', 'items': 1, 'practice': '
   ...: N81029', 'bnfname': 'Co-MagaldroxSusp 195mg/220mg/5ml S/F', 'nic': 2.99, 'actcost': 2.78, 'quantity': 500}, {'bnf
   ...: code': '0101010G0AAABAB', 'items': 2, 'practice': 'N81088', 'bnfname': 'Co-MagaldroxSusp 195mg/220mg/5ml S/F', 'n
   ...: ic': 5.98, 'actcost': 5.56, 'quantity': 1000}]

In [2]: sum([i.get('items', 0) for i in d])
Out[2]: 5