I am very new to coding and I was wondering if it is possible to limit an array in python to 100 items.
If it is possible, can you keep adding to that array and pushing out the old numbers in the array? So the oldest number should be pushed out to make room each time a new number is added.
Thank you very much in advance!
Yes, it's possible via collections.deque:
from collections import deque
lst = deque([], 100)
Like list.append, deque.append works in place:
A = deque(range(10), maxlen=10)
print(A)
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxlen=10)
A.append(10)
print(A)
deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], maxlen=10)
What about creating a simple function to do this:
def add_to_array(lst, item, maxsize):
if len(lst) >= maxsize:
lst.pop(0)
lst.append(item)
Which works like this:
>>> lst = [i for i in range(1, 10)]
>>> lst
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> add_to_array(lst, 10, 10)
>>> lst
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> add_to_array(lst, 11, 10)
>>> lst
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Note: If your looking for something more efficient, you can use collections.deque, as pointed out in the other answer.
Here is an example of using deque to emulate your desired behaviour:
>>> lst = deque((i for i in range(1, 10)), maxlen=10)
>>> lst
deque([1, 2, 3, 4, 5, 6, 7, 8, 9], maxlen=10)
>>> lst.append(10)
>>> lst
deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], maxlen=10)
>>> lst.append(11)
>>> lst
deque([2, 3, 4, 5, 6, 7, 8, 9, 10, 11], maxlen=10)
Related
Lets say I have a list called l1:
l1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
And I want to print it as
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
How would I do that in python on 3.9
I tried to us a \n, but that has not worked for me.
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(list[:5])
print(list[5:])
outputs:
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
if you want them to be printed on the same line then you can use:
print(list[:5],list[5:])
which will return:
[1, 2, 3, 4, 5] [6, 7, 8, 9, 10]
Slicing would probably work for you.
l1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(l1[:5]) # print first half
print(l1[5:]) # print second half
This should print both halves on separate lines, which is what I think you're trying to do with \n?
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
I'm trying to simplify this code so it doesn't use two for loops. The aim is to end up with a list of numbers that exist in one list but not the other ie. mutually exclusive.
Here is the code:
list1 = [1, 1, 2, 4, 6, 6, 9]
list2 = [1, 2, 3, 5, 6, 7, 8]
def mutually_exclusive(list1,list2):
new_list = []
for x in list1:
if x not in list2:
new_list.append(x)
else:
pass
for y in list2:
if y not in list1:
new_list.append(y)
else:
pass
return new_list
mutually_exclusive(list1,list2)
and the desired result:
[4, 9, 3, 5, 7, 8]
any help much appreciated thanks.
I have tried zip but doesn't yield all results.
You could also do it like this:
list1 = [1, 1, 2, 4, 6, 6, 9]
list2 = [1, 2, 3, 5, 6, 7, 8]
def mutually_exclusive(list1,list2):
return list(set(list1)^set(list2))
print(mutually_exclusive(list1, list2))
Result:
[3, 4, 5, 7, 8, 9]
You can do the following using symmetric_difference:
l1 = [1, 1, 2, 4, 6, 6, 9]
l2 = [1, 2, 3, 5, 6, 7, 8]
list(set(l1).symmetric_difference(set(l2)))
In [7]: l1
Out[7]: [1, 1, 2, 4, 6, 6, 9]
In [8]: l2
Out[8]: [1, 2, 3, 5, 6, 7, 8]
In [9]: list(set(l1).symmetric_difference(set(l2)))
Out[9]: [3, 4, 5, 7, 8, 9]
I'm looking to slice a list across two or more slices. For example, there is a list:
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Let's say I want to slice the list as items 1 to 4 and 6 to 9.
If we do:
a[1:5]
the output:
[1, 2, 3, 4]
If we do:
a[6:10]
the output is:
[6, 7, 8, 9]
But is there someway to combine multiple slices. Something like:
a[1:5 and 6:10]
to output:
[1, 2, 3, 4, 6, 7, 8, 9]
There is no special syntax, just append the lists slices:
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# [1, 2, 3, 4, 6, 7, 8, 9]
print(a[1:5]+a[6:10])
If you want to avoid creating the intermediate lists for the individual slices, you could use itertools.islice and chain.from_iterable to get and combine the slices as iterators.
>>> from itertools import chain, islice
>>> slc = [(1,5), (6,10)]
>>> list(chain.from_iterable(islice(a, *s) for s in slc))
[1, 2, 3, 4, 6, 7, 8, 9]
Also works with 1- or 3-tuples, for just end-, or start-end-step slices.
Based on napuzba's suggestion, I'm thinking that the following might be the most efficient way to do this:
all_slice = [*a[1:5], *a[6:10]]
Where all_slice holds:
[1, 2, 3, 4, 6, 7, 8, 9]
This seems pretty pythonic.
You can use list.extend for this task.
slice1 = a[1:5]
slice2 = a[6:10]
slice1.extend(slice2)
# now use slice1
It appends all the items of the slice2 to the first slice1.
If you have several ranges you are trying to slice, you can use the built-in slice() with a list comprehension:
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
ranges = [(1, 5), (6, 10)]
[n for s in ranges for n in a[slice(*s)]]
# [1, 2, 3, 4, 6, 7, 8, 9]
Inspired by the answer:
There is no special syntax, just append the lists slices:
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(a[1:5]+a[6:10])
FROM -> Aviv Yaniv
b, a = a[1:5], a[6:10]
print(b+a)
I was just wondering how would I be able to remove a part of a list purely based on position.
rando = keywords[random.randint(0, 14)]
h = 0
for h in range(len(keywords)):
if rando == keywords[h]:
position = h
realAns = definitions[position]
I tried
rando.remove[h]
but it didn't seem to work :(
What code would I be able to use to remove that keyword (not definition) once correct. Thanks.
Use del and specify the element you want to delete with the index.
>>> a=[i for i in range(1,11)]
>>>a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> del a[0]
>>> a
[2, 3, 4, 5, 6, 7, 8, 9, 10]
>>>
In addition you can also use pop , pop return deleted element.
>>> a=[i for i in range(1,11)]
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> a.pop(1)
2
>>> a
[1, 3, 4, 5, 6, 7, 8, 9, 10]
>>>
If you not specified any argument in pop it removes the last item.
>>> a=[i for i in range(1,11)]
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> a.pop()
10
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
If you also want to get what your removing, you can do:
rando.pop(h)
Otherwise just do:
del rando[h]
If I have a list where all values are unique, the code runs fine. However, if there is a duplicate value within the list, when finding the minimum value for the next iteration, it pulls from the entire list rather than just the remainder of the list.
for n in range(0,len(lst)):
a = min(lst[n:]) #minimum value within remainder of set
i = lst.index(a) #index value for minimum value within remainder of set
temp = lst[n]
lst[n] = a
lst[i] = temp
Results look like this:
lst = [6, 8, 9, 1, 3, 4, 7, 5, 4]
[1, 8, 9, 6, 3, 4, 7, 5, 4]
[1, 3, 9, 6, 8, 4, 7, 5, 4]
[1, 3, 4, 6, 8, 9, 7, 5, 4]
[1, 3, 6, 4, 8, 9, 7, 5, 4]
[1, 3, 6, 8, 4, 9, 7, 5, 4]
[1, 3, 6, 8, 9, 4, 7, 5, 4]
[1, 3, 6, 8, 9, 7, 4, 5, 4]
[1, 3, 6, 8, 9, 7, 5, 4, 4]
[1, 3, 6, 8, 9, 7, 5, 4, 4]
I'm looking for it to return this:
[1, 3, 4, 4, 5, 6, 7, 8, 9]
When n is 4, the next minimum is 4 again, but lst.index() finds the first 4 at position 3 instead.
Start searching for the miminum from n; the .index() method takes a second argument start, from where to start searching:
i = lst.index(a, n)
Note that Python can assign to two targets in place, no need to use a temporary intermediate. range() with just one argument starts from 0:
for n in range(len(lst)):
a = min(lst[n:])
i = lst.index(a, n)
lst[n], lst[i] = a, lst[n]
Demo:
>>> lst = [6, 8, 9, 1, 3, 4, 7, 5, 4]
>>> for n in range(0,len(lst)):
... a = min(lst[n:])
... i = lst.index(a, n)
... lst[n], lst[i] = a, lst[n]
...
>>> lst
[1, 3, 4, 4, 5, 6, 7, 8, 9]