I am trying to merge two lists depend on the criteria.
I have following codes.
R1=[10,20,30,40,50]
R2=[5,10,45,40,45]
for n,m in zip(R1,R2):
if n>m:
print(n)
else:
print(m)
When I run above code,the results is :
10
20
45
40
50
I don't know how to get that results as a new list like this:
results=[10,20,45,40,50]
How can I do this?
Thanks in advance.
You can use max and list comprehension.
result = [max(pair) for pair in zip(R1, R2)]
print result
Create a new list and append() the result:
In []:
R1=[10,20,30,40,50]
R2=[5,10,45,40,45]
results = []
for n, m in zip(R1,R2):
if n>m:
results.append(n)
else:
results.append(m)
results
Out[]:
[10, 20, 45, 40, 50]
You can look at a list comprehension to do the same thing:
In []:
results = [n if n>m else m for n, m in zip(R1, R2)]
results
Out[]:
[10, 20, 45, 40, 50]
Or even more simply:
In []:
results = [max(x) for x in zip(R1, R2)]
results
Out[]:
[10, 20, 45, 40, 50]
Functional solution
The map() and max() functions make short work of this problem:
>>> R1 = [10, 20, 30, 40, 50]
>>> R2 = [5, 10, 45, 40, 45]
>>> list(map(max, R1, R2))
[10, 20, 45, 40, 50]
List comprehension solution
Another technique to is to use a conditional expression in a list comprehension:
>>> [n if n>m else m for n, m in zip(R1, R2)]
[10, 20, 45, 40, 50]
R1=[10,20,30,40,50]
R2=[5,10,45,40,45]
temp_list = []
for n,m in zip(R1,R2):
if n>m:
temp_list.append(n)
else:
temp_list.append(m)
print(temp_list)
this should work.
you are trying to print each value, so it is printed on separate lines.
Use map with multiple iterables:
>>> R1=[10,20,30,40,50]
>>> R2=[5,10,45,40,45]
>>> it = map(max, R1, R2)
>>> print(list(it))
[10, 20, 45, 40, 50]
(Generally, the place to look is in the itertools module, but in this case the function is actually builtin).
Related
Unfortunately I was not able to be more specific with my title, but an example should make my problem clear. I have two lists f and a and if f[i] is equal to f[i-1]. I would like to have a new entry in f which is equal to f[i] and f[i-1] (obviously) and overwrites both of them. In a I would like to have to a_new = a[i] + a[i-1] replacing a[i] and a[i-1].
f = [10, 25, 50, 50, 75, 100, 1000, 1000, 1100, 1100]
a = [1, 3, 2, 4, 5, 3, 10, 15, 5, 5]
My desired output is:
f = [10, 25, 50, 75, 100, 1000, 1100]
a = [1, 3, 6, 5, 3, 25, 10]
I believe that some sort of list comprehension would be the optimal approach, but I couldn't figure it out yet. I doesn't need to be a list comprehension though, I am happy as long as there is some solution.
You can zip the lists together, use groupby to group the pairs by the f values, then sum the a values for each group. Then you just need to unzip them back into separate lists
from itertools import groupby
from operator import itemgetter
groups = groupby(zip(f, a), key=itemgetter(0))
f_a_generator = ((k, sum(map(itemgetter(1), pairs))) for k, pairs in groups)
f1, a1 = zip(*f_a_generator) # map(list, ...) If you need them as lists
print(f1, a1, sep='\n')
# (10, 25, 50, 75, 100, 1000, 1100)
# (1, 3, 6, 5, 3, 25, 10)
To answer your question in the comments, you can change the line
sum(map(itemgetter(1), pairs)))
to call some function other than sum:
def logarithmic_sum(values):
return 10*np.log10(sum((10**(val/10)) for val in values))
groups = groupby(zip(f, a), key=itemgetter(0))
f_a_generator = ((k, logarithmic_sum(map(itemgetter(1), pairs))) for k, pairs in groups)
f1, a1 = zip(*f_a_generator)
print(f1, a1, sep='\n')
# (10, 25, 50, 75, 100, 1000, 1100)
# (1.0000000000000002, 2.999999999999999, 6.124426027943397, 5.0, 2.999999999999999, 16.193310480660944, 8.010299956639813)
For first array case:
for i in range(1, len(f)-2):
if f[i] == f[i-1]:
del f[i]
print(f)
I have a python list like this.
[100, 150, 30, 50, 10, 20, 40]
Then I want to find 90 in list above (yes of course not in there), but my expected result are:
[50, 40]
or
[50, 30, 10]
How can I achieve that in python or other programming language?
You can use a list comprehension to iterate over all the combinations of the list elements that sum to your target value
>>> from itertools import combinations
>>> data = [100, 150, 30, 50, 10, 20, 40]
>>> target = 90
>>> [comb for n in range(1, len(data)+1) for comb in combinations(data, n) if sum(comb) == target]
[(50, 40), (30, 50, 10), (30, 20, 40)]
for this, you need to check each combination of numbers. for example:
take 100 and check if its less than 90, if yes, then check it with every other number less than 90 and see if they add to 90, but, if both numbers add to less than 90, check with the other numbers to see if the again add to 90.
Try using a list-comprehension and set:
>>> set([x for x in lst for y in lst if x+y == 90 and x != y])
{40, 50}
When declaring a list literal in Python, is it possible to merge an existing list into the declaration? For example:
l = [1, 2, 3]
n = [10, 20, 30, l, 50, 60, 70]
This results in the following:
[10, 20, 30, [1, 2, 3], 50, 60, 70]
Note that the list l itself has been added as a single element, but what I really want is for l's elements to be added individually to the target list, like this:
[10, 20, 30, 1, 2, 3, 50, 60, 70]
As of Python 3.5 (See https://www.python.org/downloads/release/python-350/, PEP448), This can be done using Python's unpack operator, which is an asterisk before the list:
l = [1, 2, 3]
n = [10, 20, 30, *l, 50, 60, 70]
The result is:
[10, 20, 30, 1, 2, 3, 50, 60, 70]
This will works with l on any place
from itertools import chain
for i in n:
if isinstance(i, list):
b+=list(chain(i))
else:
b+=[i]
In python, how can I remove all occurrences of an integer from a list. For example:
list_a = [5, 20, 15, 10, 55, 30, 65]
list_a.METHOD(5)
print(list_a)
[20, 10, 30]
Essentially, the method removes anything that contains a 5. Not specifically multiples of 5, given that 10 and 20 do not contain a 5. Thanks.
list_a = [5, 20, 15, 10, 55, 30, 65]
list_a = [x for x in list_a if "5" not in str(x)]
print(list_a)
[20, 10, 30]
This is one way:
[x for x in list_a if str(x).find('5') < 0]
That's a list comprehension that converts each number to a string and then searches the string for the character 5. find returns -1 if not found so we keep only things where the 5 wasn't found.
You can use filter and lambda.
filter(lambda x:"5" not in str(x) , [5, 20, 15, 10, 55, 30, 65])
I have two lists of numbers, say [1, 2, 3, 4, 5] and [7, 8, 9, 10, 11], and I would like to form a new list which consists of the products of each member in the first list with each member in the second list. In this case, there would be 5*5 = 25 elements in the new list.
I have been unable to do this so far with a while() loop.
This is what I have so far:
x = 0
y = 99
results = []
while x < 5:
x = x + 1
results.append(x*y)
while y < 11:
y = y + 1
results.append(x*y)
Use itertools.product to generate all possible 2-tuples, then calculate the product of that:
[x * y for (x, y) in itertools.product([1,2,3,4,5], [7,8,9,10,11])]
The problem is an example of an outer product. The answer already posted with itertools.product is the way I would do this as well.
But here's an alternative with numpy, which is usually more efficient than working in pure python for crunching numeric data.
>>> import numpy as np
>>> x1 = np.array([1,2,3,4,5])
>>> x2 = np.array([7,8,9,10,11])
>>> np.outer(x1,x2)
array([[ 7, 8, 9, 10, 11],
[14, 16, 18, 20, 22],
[21, 24, 27, 30, 33],
[28, 32, 36, 40, 44],
[35, 40, 45, 50, 55]])
>>> np.ravel(np.outer(x1,x2))
array([ 7, 8, 9, 10, 11, 14, 16, 18, 20, 22, 21, 24, 27, 30, 33, 28, 32,
36, 40, 44, 35, 40, 45, 50, 55])
Wht dont you try with known old ways;
list1 = range(1, 100)
list2 = range(10, 50, 5)
new_values = []
for x in list1:
for y in list2:
new_values.append(x*y)
Without any importing, you can do:
[x * y for x in range(1, 6) for y in range(7, 12)]
or alternatively:
[[x * y for x in range(1, 6)] for y in range(7, 12)]
To split out the different multiples, but it depends which order you want the results in.
from functools import partial
mult = lambda x, y: x * y
l1 = [2,3,4,5,5]
l2 = [5,3,23,4,4]
results = []
for n in l1:
results.extend( map( partial(mult, n) , l2) )
print results