This question already has answers here:
Efficient way to rotate a list in python
(27 answers)
Closed 6 years ago.
In detail, ex: I have a list weekday:
list_week_day = [0,2,4,5,6]
if today.weekday() = 3 , then order list_week_day = [4,5,6,0,2]
So, how to do that ???
new = old[n:] + old[:n]
You append the front part of the list to the back part. Can you finish after that hint? n is your weekday.
Could also try:
wday = 3
[(x + wday) % 7 for x in list_week_day]
# [3, 4, 5, 6, 0, 1, 2]
Did you try the following? I suspect it is not the most efficient way to get what you desire, but it certainly is a way to get it.
list_week_day[today.weekday() : ] + list_week_day[ : today.weekday()]
As suggested here, you can use numpy's roll command, choosing a suitable value to roll by:
>>> import numpy
>>> a=numpy.arange(1,10) #Generate some data
>>> numpy.roll(a,1)
array([9, 1, 2, 3, 4, 5, 6, 7, 8])
>>> numpy.roll(a,-1)
array([2, 3, 4, 5, 6, 7, 8, 9, 1])
>>> numpy.roll(a,5)
array([5, 6, 7, 8, 9, 1, 2, 3, 4])
>>> numpy.roll(a,9)
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
Related
This question already has answers here:
How do I reverse a part (slice) of a list in Python?
(8 answers)
Reverse part of an array using NumPy
(3 answers)
Closed 3 years ago.
What is the best (fastest/most pythonic) way to reverse a part of an array in-place?
E.g.,
def reverse_loop(l,a,b):
while a < b:
l[a],l[b] = l[b],l[a]
a += 1
b -= 1
now after
l = list(range(10))
reverse_loop(l,2,6)
l is [0, 1, 6, 5, 4, 3, 2, 7, 8, 9] as desired.
Alas, looping in Python is inefficient, so a better way is needed, e.g.,
def reverse_slice(l,a,b):
l[a:b+1] = l[b:a-1:-1]
and reverse_slice(l,2,6) restores l to its original value.
Alas, this does not work for the border cases: reverse_slice(l,0,6)
truncates l to [7, 8, 9] because l[a:-1:-1] should be l[a::-1].
So, what is The Right Way?
How about this?
def reverse_slice(l, a, b):
l[a:b] = l[a:b][::-1]
l = list(range(10))
reverse_slice(l, 0, 6) # excludes l[6]
print(l)
Output:
[5, 4, 3, 2, 1, 0, 6, 7, 8, 9]
An alternative with the inbuilt function reversed:
def reverse_func(l, a, b):
l[a:b] = reversed(l[a:b])
In my tests, slicing is faster than using reversed by a factor of 1.2x-1.5x.
[6::-1] can be written as [6:None:-1]:
def reverse_slice(l,a,b):
a1 = None if a==0 else a-1
l[a:b+1] = l[b:a1:-1]
In [164]: y=x.copy(); reverse_slice(y,1,6);y
Out[164]: [0, 6, 5, 4, 3, 2, 1, 7, 8, 9]
In [165]: y=x.copy(); reverse_slice(y,0,6);y
Out[165]: [6, 5, 4, 3, 2, 1, 0, 7, 8, 9]
This question already has answers here:
How to delete an element from a list while iterating over it in Python? [duplicate]
(2 answers)
Closed 5 years ago.
I want to know how to append all the items that weren't removed into a new list.
challenge = [1, 0, 9, 8, 5, 4, 1, 9, 3, 2, 3, 5, 6, 9]
def remove_values(thelist, value):
newlist = []
while value in thelist:
thelist.remove(value)
newlist.append()
bye = remove_values(challenge, max(challenge))
For example, if I remove all the 9s (the max), how do I append the rest into a new list?
challenge = [1, 0, 9, 8, 5, 4, 1, 9, 3, 2, 3, 5, 6, 9]
# This will append every item to a new List where the value not is max
# You won't need 2 lists to achieve what you want, it can be done with a simple list comprehension
removed_list = [x for x in challenge if x != max(challenge)]
print(removed_list)
# will print [1, 0, 8, 5, 4, 1, 3, 2, 3, 5, 6]
This question already has answers here:
Compact way to assign values by slicing list in Python
(5 answers)
Closed 2 years ago.
Is there a fast way to get the 1st, 3rd and 5th element from an array in Python like a[0,2,4]? Thanks.
Using operator.itemgetter:
>>> lst = [1,2,3,4,5,6,7]
>>> import operator
>>> get135 = operator.itemgetter(0, 2, 4)
>>> get135(lst)
(1, 3, 5)
You could just do this, a simple method with no imports necessary:
>>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>> [a[i] for i in (0, 2, 4)]
[1, 3, 5]
Slicing is the simplest way to do this. You'll want to slice it with [0:5:2].
>>> range(100)[0:5:2]
[0, 2, 4]
This is the equivalent of saying "Starting from element 0, up to (but not including) element 5, give me every 2nd element."
You can use slicing to get this.
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
d = a[0:5:2]
print d
[1, 3, 5]
If you want to generalize to every other entry you would use
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = a[::2]
print b
[1, 3, 5, 7, 9]
You can use ,
Slicing operation on list.
>>> a=[i for i in range(10)]
>>> a[::2]
Ouput:
[0, 2, 4, 6, 8]
perhaps:
[list[0], list[2], list[4]]
This question already has answers here:
Sorting list based on values from another list
(20 answers)
Closed 9 years ago.
I have a list of objects and I'd like to sort them based on a parallel array. So, as I operate over a list of data I construct a parallel array (where each entry in that list corresponds to an entry in the original list). Then (let's say the parallel array is filled with numbers)
list_a = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9 )
list_b = (4, 2, 5, 6, 1, 7, 3, 9, 0, 8 )
I want to sort the original list of objects based on the parallel arrays values so that the original list is sorting in ascending order by the numerical value in the other array. Is there any way to do this built into python?
sort_a_by_b(list_a, list_b)
Expected result would be:
list_a_sorted_by_b = (8, 4, 1, 6, 0, 2, 3, 5, 9, 7 )
>>> list_a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list_b = [4, 2, 5, 6, 1, 7, 3, 9, 0, 8]
>>>
>>> import operator
>>>
>>> [k for k, v in sorted(zip(list_a, list_b), key=operator.itemgetter(1))]
[8, 4, 1, 6, 0, 2, 3, 5, 9, 7]
Call the object list objects and the other list sort_keys. If you can compute sort_keys[i] from just the value of objects[i], you don't even need to build sort_keys. You should just do this:
objects.sort(key=compute_sort_key_for_object)
where compute_sort_key_for_object is the function you would use to compute sort_keys[i] from objects[i]. It's faster and more readable.
If the processing to compute sort_keys is more complex, you'll want Rohit's answer:
import operator
[k for k, v in sorted(zip(objects, sort_keys), key=operator.itemgetter(1))]
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 9 years ago.
I have one list like:
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
I want to create a function that takes a single list (see above) and concatenates all the sublists that are part of it into a single list.
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
nn = [ x for y in n for x in y]
>>> lst = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
>>> from itertools import chain
>>> list(chain.from_iterable(lst))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
For completeness, here is a very short way to write this
>>> sum(n, [])
[1, 2, 3, 4, 5, 6, 7, 8, 9]
But although it is tempting, you shouldn't because it has quadratic performance. ie a new list is created as each term is added, and all the previous items will be copied over and over
It's ok to use list.extend though
reduce(lambda x,y: x.extend(y) or x, n, [])
You can also concatenate by doing simply:
print n[0]+n[1]
In general this would be:
def concatenate(list):
x=[]
for i in list:
x+=i
return x
But this is not particularly efficent, just quite straightforward for a beginner.