Splitting a python list with common end points [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 4 months ago.
Improve this question
Suppose I have a python list as: points_list = [1, 2, 3, 4, 5, 6, 7, 8, 9,1]
And I need to split this list containing the number of elements in the index_list=[2, 2, 6, 3] But having common endpoints
That is:
First 2 elements from points_list : [1,2]
Next 2 elements from the points_list but it should start from the place it stopped before : [2,3]
Then the next 6 elements : [3,4,5,6,7,8]
Finally, 3 elements in the same way : [8,9,1]
Ultimately, what I expect is to have something like: [[1,2],[2,3],[3,4,5,6,7,8],[8,9,1]] which corresponds to the number of elements mentioned in the index_list=[2, 2, 6, 3]
Can you please help me to perform this task

Here's my code:
points_list = [1, 2, 3, 4, 5, 6, 7, 8, 9,1]
index_list=[2, 2, 6, 3]
res = []
end_point = 0
for i in index_list:
temp_list = points_list[end_point:end_point+i] # Get the list
res.append(temp_list) # Append it
end_point = points_list.index(temp_list[-1]) # Get the index of the last value
print(res)
Support for repeating numbers (thanks to azro and S.B):
points_list = [3, 2, 3, 4, 5, 6, 2, 8, 9, 1]
index_list=[2, 2, 6, 3]
res = []
for i in index_list:
res.append(points_list[:i]) # Append it
points_list = points_list[i-1:] # Reconfigure list
print(res)

Related

Finding changes made to lists [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 11 months ago.
Improve this question
So I'm trying to find the items that went in and went out of a list before and after it changed
For example, at first my list is:
[1, 1, 2, 5, 7, 7]
And then a minute later it's:
[1, 2, 2, 5, 6, 7, 4]
How would I end up with 2 lists that show what items went out and which went in like so,
itemsOut = [1,7]
itemsIn = [2, 6, 4]
Position and length can change
You can use Counter from the built-in collections module:
>>> list1 = [1, 1, 2, 5, 7, 7]
>>> list2 = [1, 2, 2, 5, 6, 7, 4]
>>> from collections import Counter
>>> counter1 = Counter(list1)
>>> counter2 = Counter(list2)
>>> l_diff = counter1-counter2
>>> r_diff = counter2-counter1
>>> print(list(l_diff))
[1, 7]
>>> print(list(r_diff))
[2, 6, 4]
You could use something like this, to detect wich items changed.
old_arr = [1, 2, 2, 5, 7, 7]
new_arr = [1, 1, 2, 5, 7, 7]
items_out = []
for element in old_arr:
if new_arr.count(element) > 0:
new_arr.pop(new_arr.index(element))
else:
items_out.append(element)
print("Items out:", items_out)
print("Items in:", new_arr)

How to compare two list and print the difference number [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
I have two unordered list
x = [1, 2, 3, 4, 5]
b = [1, 1, 2, 3, 4, 5]
I want to compare two list and print the new item added to list b
after comparing two list
I need to get the value 1 from the above list
I tried compare and sort libraries, but didn't found the right solution, any help would be appreciated
A nice use of Counter here, subtract the occurence of x to the ones of b and keep the keys only
from collections import Counter
x = [1, 2, 3, 4, 5]
b = [1, 1, 2, 3, 4, 5]
c = Counter(b) - Counter(x)
keep = list(c) # keys only
print(keep) # [1]

How do you count the values in a 2d 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 3 years ago.
Improve this question
I am trying to count how many numbers appear in each row in a 2d list and then return the values with a new variable . For example, I would to like to return the items that are greater than or equal to that length.
Here my list below
df = [[2,4,6,7],[3,4,],[2,4,6,8,12,24],[3,5,7,333,450],[4,20]]
I would like to create a new variable and return each row that is longer than 3.
df2 =[[2,4,6,7],[2,4,6,8,12,24],[3,5,7,333,450]]
I would like to create a new variable and return each row with exactly 2 elements.
df3 = [[3,4,],[4,20]]
Probably not the most efficient solution but a succinct way to do this:
df = [[2,4,6,7],[3,4,],[2,4,6,8,12,24],[3,5,7,333,450],[4,20]]
df3 = filter(lambda l: len(l) > 3, df)
df2 = filter(lambda l: len(l) == 2, df)
print(list(df2))
print(list(df3))
output:
[[3, 4], [4, 20]]
[[2, 4, 6, 7], [2, 4, 6, 8, 12, 24], [3, 5, 7, 333, 450]]
You can use list comprehensions to do this. In this case, you can check the len of each sublist, and use that to filter out which sublists you want to keep/discard.
>>> df = [[2,4,6,7],[3,4,],[2,4,6,8,12,24],[3,5,7,333,450],[4,20]]
>>> [i for i in df if len(i) > 3]
[[2, 4, 6, 7], [2, 4, 6, 8, 12, 24], [3, 5, 7, 333, 450]]
>>> [i for i in df if len(i) == 2]
[[3, 4], [4, 20]]

replace variables in a list with combinations of elements [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 3 years ago.
Improve this question
I want to replace variables in a list with combination of elements.
To be more specific:
I have these two lists
liste1 = [1,2,3,'X','X',4]
liste2 = [5,6,7]
and I want to get a list containing the elements below :
[1,2,3,5,6,4]
[1,2,3,5,7,4]
[1,2,3,6,7,4]
[1,2,3,6,5,4]
[1,2,3,7,5,4]
[1,2,3,7,6,4]
Does anyone have an idea how to make it ?
You can do it this way:
from itertools import permutations
liste1 = [1, 2, 3, 'X', 'X', 4]
liste2 = [5, 6, 7]
def replacements(liste1, liste2):
x_indices = [i for i, val in enumerate(liste1) if val == 'X']
nb = len(x_indices)
for perm in permutations(liste2, nb):
l1 = liste1[:] # if we want to preserve the original and yield different lists
for i, new_val in zip(x_indices, perm):
l1[i] = new_val
yield l1
for r in replacements(liste1, liste2):
print(r)
Output:
[1, 2, 3, 5, 6, 4]
[1, 2, 3, 5, 7, 4]
[1, 2, 3, 6, 5, 4]
[1, 2, 3, 6, 7, 4]
[1, 2, 3, 7, 5, 4]
[1, 2, 3, 7, 6, 4]
We first list the indices where 'X' appears, then generate the permutations of as many elements of liste2. For each permutation, we replace the 'X's.

Creating a List of like Indicies [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 5 years ago.
Improve this question
new here and somewhat new to Python. I am having a blank and would appreciate some guidance.
I am trying to write a nested list, then write a for loop to print a comma seperated list of the numbers using like indicies from each list to pair-up the numbers. I have my code below(2.7.14):
FirstNumbers = [1, 2, 3]
SecondNumbers = [4, 5, 6]
ThirdNumbers = [7, 8, 9]
NestedNumbers = [FirstNumbers, SecondNumbers, ThirdNumbers]
for i in range(0, 3):
for each_number in NestedNumbers:
print each_number[i],
#Output
1 4 7 2 5 8 3 6 9
My current issue is trying to get the numbers to read [1, 4, 7] and so on. I would appreciate any guidance.
Thank you
I think you need empty list so you can append the numbers to that i.e
FirstNumbers = [1, 2, 3]
SecondNumbers = [4, 5, 6]
ThirdNumbers = [7, 8, 9]
NestedNumbers = [FirstNumbers, SecondNumbers, ThirdNumbers]
m = []
for i in range(0, 3):
k = []
for each_number in NestedNumbers:
k.append(each_number[i])
m.append(k)
Output m : [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
You need to put "print "[" after "for i in range(0,3)" and a print "]" after the "for each_number" part
so that it will print:
[1 4 7][2 5 8][3 6 9]
Now you need to insert a print ", " before "print each_number[i]"
You will end up with:
[, 1, 4, 7][, 2, 5, 8][, 3, 6, 9] You need to not use print ", " on the first run-through as that is printing the comma before the first number.
You will need to change the second for statement to not go through "NestedNumbers" because you cannot know if it is the first one, but instead "range(0,len(NestedNumbers))" so that you can check to see if it is the first iteration and whether you need to add a comma.
change it to:
FirstNumbers = [1, 2, 3]
SecondNumbers = [4, 5, 6]
ThirdNumbers = [7, 8, 9]
NestedNumbers = [FirstNumbers, SecondNumbers, ThirdNumbers]
for x in range(0,3):
print "["
for y in range(0,len(NestedNumbers)):
if y!=0: print ", "
print NestedNumbers[y][i]
print "]"
Now it will print:
[1, 4, 7][2, 5, 8][3, 6, 9]
Try this
import itertools
for n in itertools.chain(*zip(FirstNumbers,SecondNumbers,ThirdNumbers))
print n,
The call to zip combines the lists as you want as a list of tuples. Then itertools.chain flattens this to a single list.
if you actually want this output:
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
Just print the result of calling zip

Categories

Resources