Multiplying only naturally adjacent integers in a list [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have a list:
l = [1,2,3,4,6,7,9,10]
I want to multiply only adjacent numbers whose difference is 1 to get a final list.
The process in this example would be:
[1*2*3*4, 6*7, 9*10]
[24, 42, 90]

Convert to an array then split after taking the np.diff then use np.prod:
l = [1,2,3,4,6,7,9,10]
a = np.array(l)
outlist = [*map(np.prod,np.split(a,np.where(np.diff(a)!=1)[0]+1))]
print(outlist)
#[24, 42, 90]

Related

Compare two lists and return a matching set iteratively [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
So it's easy to compare to get matching values for when you have two lists, i.e
set(a) & set(b)
But how would I do it in a function, i.e. when I have n lists, which depends on the input parameter n?
This is what functools.reduce is for
from functools import reduce
total = reduce(set.intersection, map(set, list_of_lists))
This is just a pre-defined function that wraps the idea of doing the following:
total = set()
for x in list_of_lists:
total &= set(x)
The below will get the intersection of the first set with rest of the sets.
Is that what you are looking for?
def do_it(*sets):
return set.intersection(*sets)
print(do_it({1, 2, 34}, {3, 6, 8, 34}, {3, 65, 86, 34}))
output
{34}

How do you find all connected sublists in a list? Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
So let's say we have the list [2,2,3,4]
Then how would you find all possible sublists that don't have any gaps in it
The the outcome would be-
[]
[2]
[2]
[3]
[4]
[2,2]
[2,3]
[3,4]
[2,2,3]
[2,3,4]
[2,2,3,4]
This should do.
a = [2,2,3,4]
print([]) # print just empty list
for i in range(len(a)): # get the rest
for j in range(i+1, len(a)+1):
print(a[i:j])

Make a list from another list in the most "python way" [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I just started coding and I am stuck here.
Suppose I have a list:
arr=[["ashley",25,399.9],["tracey",26,990.45],["jimmy",23,987],["nancy",20,1000.1]]
I want all integer value in another list
age=[]
You can use List Comprehension
arr = [["ashley",25,399.9],["tracey",26,990.45],["jimmy",23,987],["nancy",20,1000.1]]
age = [v for i in arr for v in i if str(v).isnumeric()]
Out: [25, 26, 23, 987, 20]
This one is from #Leo Arad makes slightly faster:
age = [v for i in arr for v in i if isinstance(v, int)]
Out: [25, 26, 23, 987, 20]
yes you make
arr=[["ashley",25,399.9],["tracey",26,990.45],["jimmy",23,987],["nancy",20,1000.1]]
age=[i[1] for i in arr]

How to obtain the index of the certain data type in a list? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a list containing string, int, and float data.
For example:
a = ['a', 'b', 1, 2, 3.5, 4.6]
I want to have the float data index such as [4,5] from the example above.
How can I do that?
Keep it simple:
[i for i, x in enumerate(a) if isinstance(x, float)]
You could find them using a list comprehension:
[i for i in range(len(a)) if isinstance(a[i], float)]
Result:
[4, 5]
Use list comprehensions to solve this -
index_float = [i for i in range(len(a)) if type(a[i])==float]
print(index_float)
[4, 5]
Depending upon the index of which datatype you want, you can equate it to type()

How to create an array from two others that will contain common pairs (sequences) of numbers? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
a = [1,2,3,4,5,6,8]
b = [6,8,9,4,5,3,2,1]
final result should be
c = [6,8,4,5]
This array contains the same pair of numbers in both arrays - how to write this kind of code in python?
I only known how to create an array with duplicated values
a = [1,2,3,4,5,6,8]
b = [6,8,9,4,5,3,2,1]
c = [x for x in a if x in b]
print (c)
>>> [e for t in [t for t in zip(b,b[1:]) if t in zip(a,a[1:])] for e in t]
[6, 8, 4, 5]

Categories

Resources