Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从列表中选择特定项目_Python_List_Matrix - Fatal编程技术网

Python 从列表中选择特定项目

Python 从列表中选择特定项目,python,list,matrix,Python,List,Matrix,我将以下内容作为我的输入: a10=[['p', 'e'], ['t', 'h','a'],['e', 'a'],['p','e'],['a']] c10=[['p','e'],['e','h'],['e', 'a']] a11=[4,5,2,1,3] 平均值的计算方法如下: 首先,它应该检查c10的第一个元素,即['p','e'],是否存在于a10中。如果它存在,那么它应该使用所有匹配的IndexeSher索引0和3。现在它应该转到'a11'中的那些指数位置并计算平均值,因此它将是4+1

我将以下内容作为我的输入:

a10=[['p', 'e'], ['t', 'h','a'],['e', 'a'],['p','e'],['a']]

c10=[['p','e'],['e','h'],['e', 'a']]

a11=[4,5,2,1,3]
平均值的计算方法如下:

首先,它应该检查c10的第一个元素,即['p','e'],是否存在于a10中。如果它存在,那么它应该使用所有匹配的IndexeSher索引0和3。现在它应该转到'a11'中的那些指数位置并计算平均值,因此它将是4+1/2=2.5

如果没有精确匹配,比如c10的第二个元素,即['e','h',],那么它应该检查单个元素,这里是'e'和'h',并搜索a10中存在这些字符的索引,这里的指数是:0,2,3,因为在这些位置上“e”存在,1因为在这些位置上“h”存在,在a11中取这些指数并计算平均值。 所以平均值=4+5+2+1/4

因此,, 我的输出应该是这样的:

average=2.5,即average=4+1/2——因此,如果存在精确匹配,则应从“a11”中提取相应元素……求和并计算平均值

平均值=3.0,即平均值=4+5+2+1/4——如果没有精确匹配,则应匹配列表中的各个元素,然后通过取a11中的值计算平均值

average=2.0,即average=2——如果存在精确匹配,则应返回该值

我编写了以下代码:

  a10=[['p', 'e'], ['t', 'h','e'],['e', 'a'],['p','e'],['a']]

  c10=[['p','e'],['e','h'],['e', 'a']]

  a11=[4,5,2,1,3]

  max_=3
  for i in range(len(a10)):
      if len(a10[i])<3:
          a10[i]=a10[i]+(3-len(a10[i]))*[str('')]

  for i in range(len(c10)):
      if len(c10[i])<3:
          c10[i]=c10[i]+(3-len(c10[i]))*[str('')]


  for w in range(len(c10)):
      total1=0
      count1=0
      for i in range(len(a10)):
          if c10[w] in a10:
              total1=total1+a11[i]
              count1=count1+1            
              average=float(total1/count1)
              #break

          else:
              total2=0
              count2=0
              for i in range(len(a10)):
                  for j in range(len(a10[i])):
                      for k in range(len(c10[w])):
                          if c10[w][k]==a10[i][j]:
                              total2=total2+a11[i]
                              count2=count2+1
                              average=float(total2/count2)'

                          else:
                              continue

      print 'average='+ str(average)

我不知道你到底在这里做什么,但我会这样做你所描述的

a10=[['p', 'e'], ['t', 'h','a'],['e', 'a'],['p','e'],['a']]

c10=[['p','e'],['e','h'],['e', 'a']]

a11=[4,5,2,1,3]

# Go through c10 element by element
for c10_elem in c10:
    # List of matching indexes. XXX: if same indexes can't be counted twice, 
    #                                use a set instead of a list
    id = []
    # Look for match in a10 and record it's index
    for i, a10_elem in enumerate(a10):
        if c10_elem == a10_elem:
            id.append(i)

    # We didn't find element level match
    if not id:
        # Look for char level match and record it's index
        for char in c10_elem:
            for i, a10_elem in enumerate(a10):
                if char in a10_elem:
                    id.append(i)

    # find average
    sum = 0
    for i in id:
        sum = sum + a11[i]

    average = sum / len(id)

    print("average {}".format(average))
输出-

$ python stack.py 
average 2.5
average 3.0
average 2.0
$ python stack.py 
average 2.5
average 3.0
average 2.0