Python 元组列表元素的算法及成对组合的检测
在这里输入和说明它的解释Python 元组列表元素的算法及成对组合的检测,python,linux,list,tuples,Python,Linux,List,Tuples,在这里输入和说明它的解释 a= [( 0.1, 0.2, 0.3), (0.2, 0.8, 0.9, 4.5), ( 0.11, 0.22, 0.33), (0.2, 0.46, 0.475, 1.8, 1.95)] 元组的每个元素表示一个和弦的长度。与第一个元组一样,元组由3个和弦组成,表示边长为0.1、0.2和0.3的三角形。第二个是矩形,依此类推。在每个元组中可以有数量可变的元素。就像第一个元组中有3个元素一样,第二个元组中有4个元素,第三个元组中又有3个元素,而第四个元组中有5个元素。
a= [( 0.1, 0.2, 0.3), (0.2, 0.8, 0.9, 4.5), ( 0.11, 0.22, 0.33), (0.2, 0.46, 0.475, 1.8, 1.95)]
元组的每个元素表示一个和弦的长度。与第一个元组一样,元组由3个和弦组成,表示边长为0.1、0.2和0.3的三角形。第二个是矩形,依此类推。在每个元组中可以有数量可变的元素。就像第一个元组中有3个元素一样,第二个元组中有4个元素,第三个元组中又有3个元素,而第四个元组中有5个元素。所以它也可以是可变的
现在任务来了
在某些元组中,其一个元素的长度可以是另一个元组元素长度的一半或两倍。在这种情况下,需要输出(成对)元组的ID,而不是它本身的长度。元组的ID将有助于识别它是第一个元组、第二个元组还是另一个元组。元素的ID不是必需的,只是元组的ID
就像给定的情况一样,结果是
1 2 because 0.1 exists in tuple 1 and 0.2 exists in tuple 2
1 4 because 0.1 exists in tuple 1 and 0.2 exists in tuple 4
2 4 because 0.9 exists in tuple 2 and 1.8 exists in tuple 4
请注意
0.1 and 0.2 exist in tuple 1
但是它不会作为1输出,因为它在同一个元组中。如果同一元组中存在半个或两个长度,则不应排除此项。
是否可以应用长度为+- 5%的屏障,仅仅考虑数值和舍入误差?
我刚刚学会了元组的串联用法,所以尝试一下
couples = []
for lengths in a:
for length in lengths:
if [length==i*2 for i in tup) for tup in b)]:
couples.append(length,i)
elif [length==i*0.5 for i in tup) for tup in b)]:
couples.append(length,tup)
但我不知道它是否适合用于比较 你的问题很难理解,但我可以从你的
if
陈述中看出你的一些麻烦。根据我对你的问题的理解,这里有几个例子。如果这还不够帮助,请在问题中澄清您的问题
#!/usr/bin/python
a = [( 1, 2, 3), (1, 2, 3, 6, 4.5), ( 0, 0, 0), (4, 1.1, 99)]
couples = []
for lengths in a:
for length in lengths:
if length*2 in lengths:
couples.append((length, length*2))
elif length*0.5 in lengths:
couples.append((length, length*0.5))
print couples
或
第一个示例使用中的来测试元素是否在列表中,而无需显式循环值。这只允许您检查显式值
第二个示例使用lambda
函数根据某种更灵活的规则过滤长度。如果希望允许5%的近似值,可以修改表达式lambda v:length*2==v
,解决方案如下所示。虽然这不是一种优雅的方式,但从零开始,简单的命令如下所示
a= [( 0.1, 0.2, 0.3), (0.2, 0.8, 0.9, 4.5), ( 0.11, 0.22, 0.33), (0.2, 0.46, 0.475, 1.8, 1.95)]
couples = [] # define an empty list
totalTupleIDs= len(a) # get the length of list of tuples to loop over tuples
for i in range(totalTupleIDs): # Start the loop over tuples
totalElementsInTuple=len(a[i]) # get the length of tuple to loop over its elements
for j in range(totalElementsInTuple): # start loop over elements of tuples
currentElement1= a[i][j] # get the current element inside tuple
print currentElement1
for k in range(totalTupleIDs): # Start the second loop for comparison purpose over the same list of tuples!
print a[k]
totalElementsInTuple=len(a[k])
print totalElementsInTuple
for l in range(totalElementsInTuple): #start the second loop for comparison over the elements of tuples
currentElement2= a[k][l]
print currentElement2
if currentElement1==currentElement2: # compare the elements
if i==k: # if the IDs of tuples are same, its a redundant result, leave!
print "Redundant"
else:
print " Similar at %d %d " % (i , k) # if there is no self comparison , append the result
couples.append((i, k))
print couples # output and compare the result
我已经再次解释了这个问题,并更改了数据,以便更清楚地了解长度。您的代码以长度的形式给出输出,并使其成对。我需要元组的ID作为满足条件的偶。谢谢你的时间。对于那些仍然不清楚的问题,我自己补充了答案。唯一的一点是,它写的是20行。任何一位专家都可以建议在较少的线数内,或至少在@rrauenza所做的方向上。
a= [( 0.1, 0.2, 0.3), (0.2, 0.8, 0.9, 4.5), ( 0.11, 0.22, 0.33), (0.2, 0.46, 0.475, 1.8, 1.95)]
couples = [] # define an empty list
totalTupleIDs= len(a) # get the length of list of tuples to loop over tuples
for i in range(totalTupleIDs): # Start the loop over tuples
totalElementsInTuple=len(a[i]) # get the length of tuple to loop over its elements
for j in range(totalElementsInTuple): # start loop over elements of tuples
currentElement1= a[i][j] # get the current element inside tuple
print currentElement1
for k in range(totalTupleIDs): # Start the second loop for comparison purpose over the same list of tuples!
print a[k]
totalElementsInTuple=len(a[k])
print totalElementsInTuple
for l in range(totalElementsInTuple): #start the second loop for comparison over the elements of tuples
currentElement2= a[k][l]
print currentElement2
if currentElement1==currentElement2: # compare the elements
if i==k: # if the IDs of tuples are same, its a redundant result, leave!
print "Redundant"
else:
print " Similar at %d %d " % (i , k) # if there is no self comparison , append the result
couples.append((i, k))
print couples # output and compare the result