我想使用for循环来找到python2.7
的每个三元素的最小差异这是我的test.csv数据
E1-2 u7 4
E1-2 u7 7
E1-2 u7 8
F(1)-1 u7 3
F(1)-1 u7 9
F(1)-1 u7 8
.. .. ..
我希望得到以下结果
E1-2 u7 7
E1-2 u7 8
F(1)-1 u7 8
F(1)-1 u7 9
.. .. ..
因为在组“E1-2”中,最小差异元素是7和8
8-4=5
8-7=1 # the minimal difference
7-4=3
这是我的感冒,但有一些问题。谁能帮我修改一下?感谢。
from itertools import tee, izip
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return izip(a, b)`
def mins (datas):
mi=min(pairwise(sorted(datas)), key=lambda x: x[1] - x[0])
return list(mi)`
import pandas as pd
qrt=pd.read_csv("test.csv")
def cul(dat,sam):
#results=pd.DataFrame()
a=dat.iloc[0:2]
b=pd.Series(mins(dat[sam].tolist()))
a.loc[:,'D']=b
return a`
for i in range(0,len(qrt["Ct"])-1,3):
results=pd.DataFrame()
group=qrt[i:i+3]
c=cul(group,"Ct")
results=results.append(c)
print results.head()
输出:
Sample Detector Ct D
0 E1-2 u7 4 7
1 E1-2 u7 7 8
Sample Detector Ct D
3 F(1)-1 u7 3 NaN
4 F(1)-1 u7 9 NaN
答案 0 :(得分:0)
from collection import defaultdict
import itertools
import csv
d = defaultdict(list)
with open("test.csv", newline='') as f:
r = csv.reader(f)
for row in r:
row[2] = float(row[2])
d[row[0]].append(row)
for k, v in d.items():
a, b = min(itertools.combinations(v, 2), key=lambda x: abs(x[0][2]-x[1][2]))
print(a)
print(b)
我们将使用itertools.combinations
查找所有元素对,然后找到给出最小差异的对并打印该对中的每个元素。您可以将这些print
替换为您想要对该数据执行的操作
答案 1 :(得分:0)
data = [("a", 3), ("a", 5), ("a", 8), ("b", 18), ("b", 13), ("b", 19)]
indexes = set(index for index, _ in data)
index_values = {index:[value for i, value in data if i == index] for index in indexes}
from itertools import combinations
def difference(pair):
x, y = pair
return abs(x - y)
def smallest_difference(values):
min_diff, min_pair = min((difference(pair), pair) for pair in combinations(values,2))
return min_pair
index_min_pairs = {index:smallest_difference(values) for index, values in index_values.items()}
answer = [(index, min_value) for index in index_min_pairs for min_value in index_min_pairs[index]]