简体   繁体   中英

How to stop appending values after length of list hits a certain limit?

I'm trying to create a function which allows a up to x amount of items from one list to be appended to another. The counter will return the amount of items that were appended to the list before the limit was reached (the limit being 10 in this case)

My code as of now is:

x = 10

def multienqueue(queue, items):
    counter = 0
    while len(queue) < x:
        for i in items:
            queue.append(i)
            counter += 1
    return counter

However, the output I receive is:

list = [4, 5, 6, 7, 8, 9, 'cow']
Trying to enqueue the list ['a', 'b', 'c', 'd', 'e']
The number added should be 3.
The number added was 5
The queue should now be: [4, 5, 6, 7, 8, 9, 'cow', 'a', 'b', 'c']
Your queue is: [4, 5, 6, 7, 8, 9, 'cow', 'a', 'b', 'c', 'd', 'e']

['a', 'b', 'c', 'd', 'e'] is passed as the items argument and [4, 5, 6, 7, 8, 9, 'cow'] is passed as the queue, any help on what I'm doing wrong is much appreciated!

The condition in your while loop is only checked when the end of the loop body gets reached and it tries to restart. That never happens in your code. Instead, your for loop adds all the values from items to queue , and you always return the number of values in items . The while loop never runs again, because the return statement ends the function first.

If you want to keep the same general structure of your code, you need to change it so that the check for the list being long enough gets run after each item is added. That means you want just one loop, not two nested inside each other. You can make it work with either the foo loop (checking the length separately from the looping logic, and probably using break to quit early), or the while loop (using different logic to figure out which item to append, such as queue.append(items[count]) ).

But a better approach might be to calculate how many items you're going to be adding to the queue up front. Then you can use a slice to get the right number values from items and add them to the queue in one go using list.extend .

def multienqueue(queue, items):
    num = max(0, min(x - len(queue), len(items)))
    queue.extend(items[:num])
    return num

Note that a more Pythonic approach would probably be to use iterators, rather than slicing from a list. itertools.islice can take a specific number of values from an iterator. You might not need to return the count in that case, as the iterator would still have in it only the values left un-appended.

If you just want to fix your function with minimum updates you can try the code bellow. Otherwise Blckknght gave a more pythonic and efficient solution.

x = 10

def multienqueue(queue, items):
    counter = 0
    for i in items:
        if len(queue) < x:
            queue.append(i)
            counter += 1
    return counter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM