Python Forum
Remove specific elements from list with a pattern
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove specific elements from list with a pattern
#1
#Hi everyone, here is my problem:

# My Printer-Software got an error while selecting the double printing feature
# My initial goal was to print 4 pages on each side of the printing paper
#(4 in the front and 4 in the back)

#I need a list that follows this Pattern:
#[1, 2, 3, 4, 9, 10, 11, 12, 17, 18, 19, 20, 25, 26, 27, 28] (for the front pages)
#[5, 6, 7, 8, 13, 14, 15, 16, 21, 22, 23, 24, 29, 30, 31, 32] (for the back pages)

# I need to expand this list to 8000 but with a decent code...

input = 8000
output = list(range(input + 1))
del output[0]

print(output)

#Here I am stuck because this list follows this order [1, 2, 3, 4,...]. How can I change it to the desired list structure like above?
Reply
#2
Try the following:
>>> N = 9
>>> sum([list(range(i, j)) for i, j in zip([4*k+1 for k in range(N)][1::2], [4*k + 5 for k in range(N)][1::2])], [])
[5, 6, 7, 8, 13, 14, 15, 16, 21, 22, 23, 24, 29, 30, 31, 32]
>>> sum([list(range(i, j)) for i, j in zip([4*k+1 for k in range(N)][0::2], [4*k + 5 for k in range(N)][0::2])], [])
[1, 2, 3, 4, 9, 10, 11, 12, 17, 18, 19, 20, 25, 26, 27, 28, 33, 34, 35, 36]
Reply
#3
Maybe not the most standard way, but I would use compress and cycle here to create two iterators for the front and the back.  

from itertools import cycle, compress

n = 32
front = compress(range(1,n+1), cycle([1]*4 + [0]*4))
rear = compress(range(1,n+1), cycle([0]*4 + [1]*4))

print(f"front: {list(front)}")
print(f"rear: {list(rear)}")
Output:
front: [1, 2, 3, 4, 9, 10, 11, 12, 17, 18, 19, 20, 25, 26, 27, 28] rear: [5, 6, 7, 8, 13, 14, 15, 16, 21, 22, 23, 24, 29, 30, 31, 32]
Reply
#4
(Oct-11-2020, 01:30 AM)bowlofred Wrote: Maybe not the most standard way, but I would use compress and cycle here to create two iterators for the front and the back.  

from itertools import cycle, compress

n = 32
front = compress(range(1,n+1), cycle([1]*4 + [0]*4))
rear = compress(range(1,n+1), cycle([0]*4 + [1]*4))

print(f"front: {list(front)}")
print(f"rear: {list(rear)}")
Output:
front: [1, 2, 3, 4, 9, 10, 11, 12, 17, 18, 19, 20, 25, 26, 27, 28] rear: [5, 6, 7, 8, 13, 14, 15, 16, 21, 22, 23, 24, 29, 30, 31, 32]

an elegant way. thank you!!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 422 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  How to remove some elements from an array in python? gohanhango 9 1,133 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 465 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Checking if a string contains all or any elements of a list k1llcod3 1 1,089 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  Remove numbers from a list menator01 4 1,320 Nov-13-2022, 01:27 AM
Last Post: menator01
  How to change the datatype of list elements? mHosseinDS86 9 1,952 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  search a list or tuple for a specific type ot class Skaperen 8 1,915 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,106 May-17-2022, 11:38 AM
Last Post: Larz60+
  Remove empty keys in a python list python_student 7 3,011 Jan-12-2022, 10:23 PM
Last Post: python_student
Question How to gather specific second-level items from a list chatguy 2 1,545 Dec-17-2021, 05:05 PM
Last Post: chatguy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020