I have two lists of tuples (character, percentage) sorted by percentage. Ultimately, I need to keep appending characters to List_A until it reaches a predefined length (that logic has already been taken care of).
Example:
List_A
('a', 0.077)
('b', 0.070)
('c', 0.020)
('d', 0.015)
('z', 0.010)
List_B
('x', 0.060)
('y', 0.059)
('z', 0.055)
('a', 0.030)
('b', 0.010)
I need to:
1) Select a character from List_B and see if exists in List_A
2) If it does, calculate if it's percentage in List_B > it's percentage in List_A
3) If the percentage is greater, append that character to List_A
So, in this example, 'a' and 'b' won't ever get appended to List_A since List_B percentage < List_A percentage
'x' and 'y' won't ever get appended to List_A due to them not existing in List_A
'z' WILL get appended to List_A, likely multiple times, until it's percentage in List_A > the percentage in List_B
The looping and percentage logic has already been taken care of outside of this function. How do I apply these three conditions to these two lists?
Desired output of the above example would be List_A with another 'z' appended, something like:
('a', 0.077)
('b', 0.070)
('c', 0.020)
('d', 0.015)
('z', 0.010)
('z', 0.055)
Thanks!
First, you should create a dictionary mapping the characters in List_A to their percentage values. They are already in the right format (assuming no duplicates) so you can use the dict function. Then, iterate the elements in List_B, look up the values in the dictionary, and append them to the list. Also, don't forget to update the dictionary with the new (higher) percentages.
dict_a = dict(List_A)
for c, p in List_B:
if c in dict_a and dict_a[c] < p:
List_A.append((c, p))
dict_a[c] = p
Related
I have this list:
my_list = [('a',1), ('b',2), ('c',3)]
How can i write a foreach loop to find the sum of 1+2+3?
Simplest approach:
list = [('a',1), ('b',2), ('c',3)]
summ = 0 # variable to store sum
for i in list:
summ = summ + i[1]
print(summ)
This returns 6
Short approach using a comprehension:
items = [('a', 1), ('b', 2), ('c', 3)]
print(sum(item[1] for item in items))
Please avoid naming your list list, it's the name of the list type in Python, so you're "hiding" the list type, which can cause strange bugs if you need the real list later.
This question already has answers here:
Finding the average of a list
(25 answers)
Closed 1 year ago.
I have the following list of tuples:
list = [(120, 'x'), (1120, 'y'), (1330, 'x'), (0, 't'), (1, 'x'), (0, 'd'), (2435, 'x')]
I would like to calculate the mean of the first component of all tuples. I did the following:
s = []
for i in range(len(list)):
a = list[0][i]
if a =! 0:
s.append(a)
else:
pass
mean = sum(s) / len(s)
and it works, but my question is whether there is any way to avoid using for loops? since I have a very large list of tuples and due to time calculation I need to find another way if that possible.
According to the above stated for loop method. How could I find the mean with regard to the wights? I mean, e.g. the last element in the list is (2435, 'x') and the number 2435 is very large in comparison to that one in (1, 'x') which is 1. Any ideas would be very appreciated. Thanks in advance.
The loop is unavoidable as you need to iterate over all the elements at least once as John describes.
However, you can use an iterator based approach to get rid of creating a list to save on space:
mean = sum(elt[0] for elt in lst)/len(lst)
Update: I realize you only need the mean of elements that are non-zero. You can modify your approach to not store the elements in the list.
total = 0
counts = 0
for elt in lst:
if elt[0]:
total += elt[0]
counts += 1
mean = total/counts
A pandas approach:
import pandas as pd
tuples = [(120, 'x'), (1120, 'y'), (1330, 'x'), (0, 't'), (1, 'x'), (0, 'd'), (2435, 'x')]
df = pd.DataFrame(tuples)
df[0][df[0]!=0].mean() #1001.2
Careful timing would be needed to see if this is any better than what you are currently doing. The actual mean calculation should be faster, but the gain could well be negated by the cost of conversion.
You do need a for loop, but you can use list comprehension to make it cleaner.
Also, python standard library has a very nice statistics module that you can use for the calculation of the mean.
As extra note, please, do not use list as a variable name, it can be confused with the type list.
from statistics import mean
mylist = [(120, 'x'), (1120, 'y'), (1330, 'x'), (0, 't'), (1, 'x'), (0,'d'), (2435, 'x')]
m = mean([item[0] for item in mylist if item[0] != 0])
print(m)
1001.2
In Python 2.7
items = [item[0] for item in mylist if item[0] != 0]
mean = sum(items)/len(items)
print(mean)
1001.2
Finish up by refactoring the list comprehension to show more meaningful variable names, for example items = [number for number, letter in mylist if number != 0]
Let's say I have a multidimensional list of tuples i.e.
[[], [('A', 'Alpha'), ('B', 'Beta')]
but the first list of that multidimensional list is not always filled with elements.
How to remove i.e. the 'A' element, alongside with its partner 'Alpha'?
I must also add that the tuples are dynamic, meaning that in the second array there might be tuples such as:
[[], [('A', 'Alpha'), ('B', 'Beta'), ('C', 'Cocoa', 'Ccocoa2')]
due to the API creating ResourceSet objects, which I compare and then get results.
Thanks in advance for help!
I am aware of dictionaries and collection.Counters in Python.
My question is how can I make one that takes index of the string into account?
For example for this string: aaabaaa
I would like to make a tuples that contain each string in progression, keeping track of the count going left to right and resetting the count once a new alphanumeric is found.
For example, I like to see this output:
[('a', 3), ('b', 1), ('a', 3)]
Any idea how to use the dictionary / Counter/ or is there some other data structure built into Python I can use?
Regards
You could use groupby:
from itertools import groupby
m = [(k, sum(1 for _ in v)) for k, v in groupby('aaabaaa')]
print(m)
Output
[('a', 3), ('b', 1), ('a', 3)]
Explanation
The groupby function makes an iterator that returns consecutive keys and groups from the iterable, in this case 'aaabaaa'. The key k is the value identifying of the group, ['a', 'b', 'a']. The sum(1 for _ in v) count the amount of elements in the group.
I'm making a gravity simulator and I need to calculate the resultant force acting upon each body.
In order to do this, I need to iterate through every pair of bodies in a dictionary (id: instance of Body class) and get the gravitational force between those two bodies. Then, I would add up all the forces and get the resultants.
But, how do I iterate over each pair of items in a dictionary only once in Python? If the celestial bodies were kept in a list, it would be simple:
for i in range(len(bodies)):
for j in range(len(bodies) - i - 1):
k = j - i + 1
b1 = bodies[i]
b2 = bodies[k]
values() and itertools' combinations are ideal for this use case.
from itertools import combinations
for a, b in combinations(bodies.values(), 2):
print a, b
you're looking for itertools.combinations():
An example:
In [76]: lis=['a','b','c','d'] #consider these as your dictionary items
In [77]: [x for x in combinations(lis,2)]
Out[77]: [('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd'), ('c', 'd')]
The itertools module provides an excellent combinations method you could use:
from itertools import combinations
bodies = {}
# add bodies
for a,b in combinations(bodies.values(), 2):
# a and b are a pair of bodies. do stuff
pass
Incidentally, this will still work even if you use a list:
from itertools import combinations
bodies = []
# add bodies
for a,b in combinations(bodies, 2):
pass