Question

Using Python3, I have this list called L1 which contains the following: L1 = [4420, 4421,...

Using Python3,

I have this list called L1 which contains the following:

L1 = [4420, 4421, 4422, 4423, 4424, 4425, 4427, 4435, 5135, 5136, 5137, 5139, 5149, 6216, 6217, 6218, 6219, 6221, 6232, 7458, 7459, 7460, 7461, 7462, 7464, 7473]

However, I need to filter this list by intervals of 8 and record the first instance where it is inside this interval, and the filtered outputs are then put into a separate list called L2. To clarify what I mean, this interval of 8 originates from 0 and continues increasing by 8 so we will have (0,8,16,24,...,4416,4424,...), so the items I want in my filtered list should be:

L2 = [4420, 4424, 4435, 5135, 5136, 5149, 6216, 6232, 7458, 7464, 7473]

So basically every time an item % 8 == 0 or is the first instance within the interval of 8, the item is appended to L2.

.

.

To clarify how I need to filter the items in this original list L1, for every interval of 8 (so it will generate intervals like this: 0,8,16,24,32,..,4416,4424,4432,4440,...,5128,5136,5144,...), every time a number inside L1 is the first occurrence inside a given interval, that particular number is appended to L2 and any other numbers inside that same interval are ignored.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

'''

Python version : 3.6

Python program to filter the given list by intervals of 8 and record the first instance where it is inside this interval

'''

# given list

L1 = [4420, 4421, 4422, 4423, 4424, 4425, 4427, 4435, 5135, 5136, 5137, 5139, 5149, 6216, 6217, 6218, 6219, 6221, 6232, 7458, 7459, 7460, 7461, 7462, 7464, 7473]

# get the minimum element of L1

minItem = min(L1)

# get the maximum element of L1

maxItem = max(L1)

# get the start element for the range of interval of 8

minItem = (minItem-(minItem%8))

# get the last element for the range of interval of 8

maxItem = (maxItem + ((8-(maxItem%8))))

length = int((maxItem - minItem)/8) # get the total number of intervals

# create the list that contains if the element of a particulare interval is included in L2 or not

# initialize all the elements of this list to False

itemExist = [False]*length

L2 = []

# loop over the elements of L1

for item in L1:

               count = 0

               # get the interval range of this item

               for i in range(minItem,maxItem,8):

                              if item >=i and item <=i+7:

                                             if not itemExist[count]: # check if the item for this interval has been included or not

                                                            # if not included, include this item in L2 and reflect the same in the list itemList

                                                            itemExist[count] = True

                                                            L2.append(item)

                                             else:

                                                            break

                              else:

                                             count = count + 1

# print the resulting L2 list

print(L2)                                            

#end of program

Code Screenshot:

Python version 3.6 Python program to filter the given 1ist by intervals of 8 and record the first instance where it is inside23 L2= 24 25 # loop over the elements of L1 Efor item in L1 26 count 0 get the interval range of this item for i in range (mi

Output:

[4420, 4424, 4435, 5135, 5136, 5149, 6216, 6232, 7458, 7464, 7473]

Add a comment
Know the answer?
Add Answer to:
Using Python3, I have this list called L1 which contains the following: L1 = [4420, 4421,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Python3, How can I aggregate the output from each iteration to one single list? Because...

    Using Python3, How can I aggregate the output from each iteration to one single list? Because from the program I have right now, it gives me the result of every iteration and gives me something like this as individual results and not one large list: None None ... None 130.0 None ... 1764 1765 None To clarify, I have this program where it calculates the sum of bytes sent within each incrementation of every 1 second, but there are instances...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT