这是一个令人费解的问题.

这段代码应该定位日期中现有的对等对,以便我可以处理它们(在另一个函数中).它必须返回往复运动的位置,以及它们发生的df线.相互意义(a,b)==(b,a).

Explaining my scenario:

我使用"random"来模拟这样一个事实:在我所在的代码点上,一些倒数已经通过其他过滤器/处理消除了.

然后,我现在使用的函数"reciprocals\u locator"应该会将我指向数据帧中仍然存在交互并需要进一步处理的行.

我正在try 重构的原始解决方案是一个复杂的循环,它在循环中以数据帧的方式进行操作.所有这些我都不喜欢在我的最终代码中.

So here goes question (1):是否有更好的方法/算法或甚至外部/导入的函数以更精简的方式完成这一技巧?

如下图所示,我知道行(1,5)中存在重复项,但它也显示了倒数解(5,1)!我最初认为这是一个简单的"挑选一半"结果列表的例子,但这可能不会起作用,因为itertools.product的工作方式,坦率地说,我还没有深入研究.

在我最初的循环代码实现中也会发生同样的情况.也许我现在知道有一个更简单的itertools-based个解决方案.

**希望下面的所有代码都足够简单

### Problem scenario recreation

import itertools as it
import pandas as pd
import random as rd

## Original dataset
sourcelst = ['a','b','c','d','e']
pairs = [list(perm) for perm in it.permutations(sourcelst,2)]
df = pd.DataFrame(pairs, columns=['y','x'])

## Dataset after some sort of processing, some reciprocals are eliminated,
## but some stay and we nee to process them separately
drop_rows = [rd.randint(0, len(pairs)-1) for _ in range(2)]
df.drop(index=drop_rows, inplace=True)
df.reset_index(inplace=True, drop=True)


## Finding reciprocals

### Original LOOPS implementation, this one shows the actual pairs
### for ease of analysis
def reciprocal_pairs_orig(df):
    reciprocals = []
    row_y = 0
    while row_y < len(df.index):
        for row_x in range(len(df.index)):
            if (df['x'].iloc[row_x] == df['y'].iloc[row_y]) and (df['y'].iloc[row_x] == df['x'].iloc[row_y]):
                
                reciprocals.append([[df['y'].iloc[row_x], df['x'].iloc[row_x]],[df['y'].iloc[row_y], df['x'].iloc[row_y]]])
        row_y += 1
    return reciprocals

### List comprehension refactor, showing the pairs
def reciprocal_pairs_refactor(df):
    return [[[df['y'].iloc[row_x], df['x'].iloc[row_x]], [df['y'].iloc[row_y], df['x'].iloc[row_y]]]
        for row_y, row_x in it.product(range(len(df.index)), range(len(df.index)))
        if (df['x'].iloc[row_x] == df['y'].iloc[row_y]) and (df['y'].iloc[row_x] == df['x'].iloc[row_y])]


### This is the actual function that I want to use
def reciprocal_locator_orig(df):
    reciprocals = []
    row_y = 0
    while row_y < len(df.index):
        for row_x in range(len(df.index)):
            if (df['x'].iloc[row_x] == df['y'].iloc[row_y]) and (df['y'].iloc[row_x] == df['x'].iloc[row_y]):
                
                reciprocals.append([row_y, row_x])
        row_y += 1
    return reciprocals

### List comprehension refactor
def reciprocal_locator_refactor(df):
    return [[row_y, row_x]
        for row_y, row_x in it.product(range(len(df.index)), range(len(df.index)))
        if (df['x'].iloc[row_x] == df['y'].iloc[row_y]) and (df['y'].iloc[row_x] == df['x'].iloc[row_y])]

推荐答案

product(range(n),range(n))将为您提供n**2对-例如,正如您所指出的,(1,5)(5,1)都是,因此这不是一个好的解决方案.您的原始代码具有嵌套的for个循环,执行相同的操作.

第一个改进是

for x in range(n-1):
    for y in range(x,n):
        ...

这仍然是O(n2),但至少它只会判断一次每个可能的配对.

但另一种方法可能更有效:只在行上循环一次,同时保持frozensets中的dict(正常的sets不可散列),其中包含已遇到的对作为键,行号作为值.对于每一行,您只需判断该对是否已经在dict中-然后它是当前行和其编号存储在dict中的行之间的"倒数"-否则添加新对,依此类推.例如:

seen = {}
for row in range(len(df.index)):
    fs = frozenset(df['x'].iloc[row], df['y'].iloc[row])
    if fs in seen:
        reciprocals.append((seen[fs],row))
    else:
        seen[fs] = row

Python相关问答推荐

海运图:调整行和列标签

处理带有间隙(空)的duckDB上的重复副本并有效填充它们

当从Docker的--env-file参数读取Python中的环境变量时,每个\n都会添加一个\'.如何没有额外的?

numpy卷积与有效

将输入聚合到统一词典中

什么是最好的方法来切割一个相框到一个面具的第一个实例?

通过ManyToMany字段与Through在Django Admin中过滤

Flash只从html表单中获取一个值

如何防止Pandas将索引标为周期?

交替字符串位置的正则表达式

如何删除重复的文字翻拍?

从嵌套极轴列的列表中删除元素

Seaborn散点图使用多个不同的标记而不是点

如何提高Pandas DataFrame中随机列 Select 和分配的效率?

如何将列表从a迭代到z-以抓取数据并将其转换为DataFrame?

通过对列的其余部分进行采样,在Polars DataFrame中填充_null`?

当lambda函数作为参数传递时,pyo3执行

如何批量训练样本大小为奇数的神经网络?

GEKKO中若干参数的线性插值动态优化

搜索结果未显示.我的URL选项卡显示:http://127.0.0.1:8000/search?";,而不是这个:";http://127.0.0.1:8000/search?q=name";