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. If its present, then it should take all the matching indexes(here indexes 0 and 3). Now it should go to those index positions in 'a11' and compute the mean, so it will be (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. so 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=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=2.0
#ie average=2
--if there is an exact match, it should return that value.
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. Because when it enters else loop, its computing other values in a11 also.
If i am writing break statement after if loop, then its not computing value for second element of list.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.