【问题标题】:How to combine Python tuple and list into a list with embedded dictionary如何将 Python 元组和列表组合成带有嵌入式字典的列表
【发布时间】:2021-12-01 10:56:18
【问题描述】:

我有 5 个属性要在几千条记录中进行比较,以便将记录群拆分为属性与特定组合匹配的离散记录集。

当与包含相同属性的现有数据集进行比较时,每个属性都可以采用三个值之一,即。 =、> 或

本质上,组合的数量包括我使用 itertools.product 生成的笛卡尔积,代码如下:sn-p:

from itertools import product

tc = ("=", ">", "<")
dr = ("=", ">", "<")
bd = ("=", ">", "<")
fr = ("=", ">", "<")
ch = ("=", ">", "<")

permutations = list(product(tc, dr, bd, fr, ch))

结果列表采用以下形式:

('=', '=', '=', '=', '=')
('=', '=', '=', '=', '>')
('=', '=', '=', '=', '<')
('=', '=', '=', '>', '=')
...

我之前编写了一个 Python 脚本来动态生成所需的 SQL 查询,运行它们并将结果写入文本文件 (CSV)

代码依赖于遍历一个包含字典的列表,该字典当时定义如下(显然这种方法在引入更多属性时不可行。):​​

permutations = []

permutations.append({'tc': "=", 'dr': "=", 'bd': "=", 'fr': "="})
permutations.append({'tc': ">", 'dr': "=", 'bd': "=", 'fr': "="})
permutations.append({'tc': "<", 'dr': "=", 'bd': "=", 'fr': "="})
permutations.append({'tc': "=", 'dr': ">", 'bd': "=", 'fr': "="})
permutations.append({'tc': ">", 'dr': ">", 'bd': "=", 'fr': "="})

然后我会在每次迭代中使用各自的 'tc'、'dr'、'bd' 和 'fr' 值动态地遍历列表,然后生成 SQL 查询 sn-p:

for permutation in permutations:
    tc = permutation.get('tc')
    dr = permutation.get('dr')
    bd = permutation.get('bd')
    fr = permutation.get('fr')

    criteria = (f"a.f1 {tc} b.f1 AND "
                f"a.f2 {dr} b.f2 AND "
                f"a.f3 {bd} b.f3 AND "
                f"a.f4 {fr} b.f4")

问题:将 itertools.product 生成的列表转换为我的代码一直在利用的嵌入字典的等效列表的最有效方法是什么 - 还是修改我的代码以直接引用列表的元素会更好?

【问题讨论】:

  • 那么最后你想要的是列表“排列”吗?
  • for tc, dr, bd, fr, ch in permutations:?
  • 感谢@IainShelvington,它成功了。

标签: python list dictionary tuples


【解决方案1】:
import pandas as pd
import sqlite3
from itertools import product


def run_query(query1, query2, iteration, dbconn):

    """ generate a dataframe containing the query results and if a result is returned,
        write it out to file and delete the corresponding records from the table """

    df = pd.read_sql( query1, dbconn)
    if len(df) > 0:
        df.to_csv(f"/tmp/db/results/{iteration}alib_has_{tc}tc_{dr}dr_{bd}bd_{fr}fr_{ch}ch.csv", index = False, sep="|")
        dbcursor.execute( query2 )

""" set up all the global variables for the script """

""" define the fields we want to return from the query """
fields = ("b.__dirpath jbod_dirpath")       


""" name the tables we will be iterating """
table1 = "alib_dirnames"
table2 = "jbod_dirnames"


""" Define the datapoints and their possible values to iterate """
tc = ("=", ">", "<")
dr = ("=", ">", "<")
bd = ("=", ">", "<")
fr = ("=", ">", "<")
ch = ("=", ">", "<")


""" Generate the resulting Cartesian product """
permutations = list(product(tc, dr, bd, fr, ch))

""" establish database connection """
conn = sqlite3.connect("/tmp/db/alib_jbod.db")
dbcursor = conn.cursor()



""" now iterate the queries """

for tc, dr, bd, fr, ch in permutations:

    """ define select criteria - 1st query is highest order match: albumartist, album & version"""
    criteria = (f"a.albumartist IS NOT NULL AND "
            f"b.albumartist IS NOT NULL AND "
            f"a.album IS NOT NULL AND "
            f"b.album IS NOT NULL AND "
            f"a.version IS NOT NULL AND "
            f"b.version IS NOT NULL AND "            
            f"a.albumartist = b.albumartist COLLATE NOCASE AND "
            f"a.album = b.album COLLATE NOCASE AND "
            f"a.version = b.version COLLATE NOCASE AND "
            f"a.track_count {tc} b.track_count AND "
            f"a.dynamicrange {dr} b.dynamicrange AND "
            f"a.__bitspersample {bd} b.__bitspersample AND "
            f"a.__frequency_num {fr} b.__frequency_num AND "
            f"a.__channels {ch} b.__channels")

    """ define the SQL query to be run """
    query1 = (f"SELECT {fields} "
          f"FROM {table1} a "
          f"INNER JOIN {table2} b "
          f"ON ( {criteria} ) "
          f"WHERE a.__dirname NOT LIKE 'CD%' "
          f"ORDER BY b.__dirpath;")

    """ define the corresponding delete query """
    query2 = (f"DELETE FROM {table2} "
          f"WHERE {table2}.__dirpath IN ( "
          f"SELECT b.__dirpath "
          f"FROM {table1} a "
          f"INNER JOIN {table2} b "
          f"ON ( {criteria} ) "
          f"WHERE a.__dirname NOT LIKE 'CD%');")

    run_query(query1, query2, 1, conn)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多