【问题标题】:arithmetics on the elements of list of tuples and detecting the required combinations as pairs [closed]关于元组列表元素的算术并将所需组合检测为对[关闭]
【发布时间】:2016-06-07 16:27:37
【问题描述】:

在此处输入和说明其说明

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)

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

【问题讨论】:

  • 对于那些还不清楚的,我自己添加了答案。唯一的一点是,它写的是20行。任何专家都可以像@rrauenza 所做的那样,以更少的行数或至少一个方向提出建议。

标签: python linux list tuples


【解决方案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

【讨论】:

  • 我已经再次解释了问题并更改了数据以更清楚地了解长度。您的代码以长度的形式给出输出并使其配对。我需要元组的 ID 作为满足条件的夫妇。感谢您的宝贵时间。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多