Let's assume I have the following list names = ['tan','2','alp','3','tan','4','alp','3','tan','1']
the odd indexed elements are some values and even indexed elements are their number of occurrences (so alp happened 3+3 = 6 times for example)
I am trying to make code to do that.
enter code here
names = ['tan','2','alp','3','tan','4','alp','3','tan','1']
i = 1;
dd = names[0::2]
nn = names[1::2]
ct = [0 for cc in range(len(dd))];
le = len(dd);
for i in range(0, le):
ct[i] = int(nn[i])
for j in range(i+1, le):
if (j < le) and (dd[i] == dd[j]):
ct[i] += int(nn[j]);
del(dd[j])
# del(nn[j])
le -= 1
the output i get for ct is [9, 7, 4, 3, 1] however it should be [7, 6] --- 7 for tan and 6 for alp
if i uncommented del(nn[j]) --- i will be equal to le and code will stop
but i should delete the element and the number of occurrences it happened any time after first time (after adding it in ct for sure) so the counting process doesn't get repeated
any idea how to do that?
We can use a dictionary to keep track of the counts more easily than a list. Below I use a defaultdict
which is a dict subclass from the standard library that supports default values.
from collections import defaultdict
names = ['tan','2','alp','3','tan','4','alp','3','tan','1']
d=defaultdict(int)
for name, count in zip(*[iter(names)]*2):
d[name] += int(count)
That's relatively easy - build a lookup map from your odd elements and then sum the matching even values. You can use collections.defaultdict()
to make your life easier:
import collections
names = ['tan', '2', 'alp', '3', 'tan', '4', 'alp', '3', 'tan', '1']
lookup_map = collections.defaultdict(int)
for element, value in zip(names[0::2], names[1::2]):
lookup_map[element] += int(value)
print(dict(lookup_map)) # {'tan': 7, 'alp': 6}
If you really need only the values and need to keep the order, you can add an additional step:
result = [lookup_map.pop(element) for element in names[0::2] if element in lookup_map]
print(result) # [7, 6]
If you're after the occurrences / total occurrences per element calculation, you can just apply the sum-total over the whole lookup map:
sum_total = sum(lookup_map.values())
for name in lookup_map:
lookup_map[name] /= sum_total
print(dict(lookup_map)) # {'alp': 0.46153846153846156, 'tan': 0.5384615384615384}
You could use un object to store the occurences of each name
names = ['tan','2','alp','3','tan','4','alp','3','tan','1']
# Use an object to store occurences
results = {}
# Iterate through 'names' to get occurences
for i in range(len(names)):
if i % 2 == 0:
# Store them in the object
if names[i] in results: # if 'name' exist in results object
results[names[i]] += int(names[i+1])
else: # if it doesn't
results[names[i]] = int(names[i+1])
# Get the value only array
res_arr = [v for k,v in results.items()]
print results
print res_arr
This would output :
{'tan': 7, 'alp': 6}
[7, 6]
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.