这是一个令人费解的问题.
这段代码应该定位日期中现有的对等对,以便我可以处理它们(在另一个函数中).它必须返回往复运动的位置,以及它们发生的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])]