[英]select specific items from a list
I am having the following as my input: 我输入以下内容:
a10=[['p', 'e'], ['t', 'h','a'],['e', 'a'],['p','e'],['a']]
c10=[['p','e'],['e','h'],['e', 'a']]
a11=[4,5,2,1,3]
average is calculated in this way: 平均值是通过以下方式计算的:
first it should check if first element of c10
ie ['p','e']
is present in a10
or not. 首先,它应该检查
c10
第一个元素,即['p','e']
是否存在于a10
。 If its present, then it should take all the matching indexes(here indexes 0 and 3). 如果存在,则应采用所有匹配的索引(此处为索引0和3)。 Now it should go to those index positions in 'a11' and compute the mean, so it will be
(4+1)/2=2.5
现在它应该转到“ a11”中的那些索引位置并计算平均值,因此它将是
(4+1)/2=2.5
If there is no exact match, say for second element of c10
ie ['e','h']
, then it should check with individual element, here 'e'
and 'h'
and search for the indexes in which these charachters are present in a10
, here indexes are: 0,2,3 (since in these positions 'e' is present) and 1(since in these positions 'h' is present) and go for these indexes in a11
and compute average. 如果没有完全匹配,则说出
c10
第二个元素,即['e','h']
,那么它应该检查单个元素,这里是'e'
和'h'
并搜索这些字符所在的索引在a10
存在,这里的索引是:0,2,3(因为在这些位置存在'e')和1(因为在这些位置存在'h'),并在a11
选择这些索引并计算平均值。 so average=(4+5+2+1)/4
所以
average=(4+5+2+1)/4
Hence, My output should be like this: 因此,我的输出应如下所示:
average=2.5
#ie average=(4+1)/2
--so if there is an exact match it should take the respective element from 'a11'..do the sum and compute mean. average=2.5
#ie average=(4+1)/2
因此,如果存在精确匹配,则应取自'a11'中的相应元素。求和并计算平均值。
average=3.0
#ie average=(4+5+2+1)/4
--if there is no exact match, it should match for the individual elements in the list of list and then compute mean by taking values in a11
. average=3.0
#ie average=(4+5+2+1)/4
如果没有精确匹配,则应与列表列表中的各个元素匹配,然后通过取a11
值来计算均值。
average=2.0
#ie average=2
--if there is an exact match, it should return that value. average=2.0
#ie average=2
如果存在完全匹配,则应返回该值。
I have written the following code: 我写了以下代码:
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)
But, the problem with this is- 但是,这个问题是-
If the first element of list, ie ['p','e']
is present, it should do the computation and return the value and it should not enter the else loop. 如果存在列表的第一个元素,即
['p','e']
,则它应该进行计算并返回值,并且不应该进入else循环。 Because when it enters else loop, its computing other values in a11 also. 因为当它进入else循环时,它也在a11中计算其他值。
If i am writing break statement after if loop, then its not computing value for second element of list. 如果我在if循环后编写break语句,则它不计算列表第二个元素的值。
Output should be: 输出应为:
average=2.5
average=3.0
average=2.0
I couldn't exactly what you are doing here, but this is how I would do what you've described. 我无法确切地知道您在这里做什么,但这就是我将按照您的描述去做。
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))
Output - 输出-
$ python stack.py
average 2.5
average 3.0
average 2.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.