关于元组列表元素的算术,并检测成对的所需组合

时间:2016-06-07 16:27:38

标签: 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个元素。所以它也可以变化。

现在完成任务。

在某些元组中,其中一个元素的长度可以是另一个元组元素长度的一半或两倍。在这种情况下,需要输出(耦合)元组的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 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)

但我不知道它适合用于比较。

2 个答案:

答案 0 :(得分:1)

您的问题非常难以理解,但我可以在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                                                                   

或者

couples = []                                                                    

for lengths in a:                                                               
    for length in lengths:                                                      
        for f in filter(lambda v: length * 2 == v, lengths):                    
            couples.append((f, length * 2))                                     
        for f in filter(lambda v: length * 0.5 == v, lengths):                  
            couples.append((f, length * 0.5))                                   

print couples               

第一个示例使用in来测试元素是否在list中,而不需要显式循环这些值。这只允许您检查显式值。

第二个示例使用lambda函数根据某些规则过滤长度,这更灵活。如果您想允许5%的近似值,则可以修改表达式lambda v: length * 2 == v

答案 1 :(得分:0)

解决方案如下所示。虽然它不是一种优雅的方式,但从头开始和简单的命令,它看起来像下面

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