This question already has answers here:
Efficient way to rotate a list in python
(27 answers)
Closed 9 years ago.
Say you have a list [1,2,3,4]
And I want to get [2,3,4,1] or [3,4,1,2].
Basically I am using the list with a different starting point each time, but then continuing through the list. How would I create something to recognize that in python.
What i have now is list[n:] where n is the shifted value, say 2, making you start at three.
someList[n:] + someList[:n]
would solve your purpose if n <= len(someList)
Also, collections.deque is the efficient way.
I believe this is what you want
>>> def startAt(index, list):
... print list[index:] + list[:index]
...
>>> l = [0,1,2,3,4,5]
>>> startAt(3, l)
[3, 4, 5, 0, 1, 2]
>>>
Related
This question already has answers here:
How do I get the last element of a list?
(25 answers)
Cycle through list starting at a certain element
(7 answers)
Closed 4 years ago.
list1 = [1,2,3,4]
If I have list1 as shown above, the index of the last value is 3, but is there a way that if I say list1[4], it would become list1[0]?
You can you modulo math like:
Code:
list1 = [1, 2, 3, 4]
print(list1[4 % len(list1)])
Results:
1
In the situation you described, I myself use the method #StephenRauch suggested. But given that you added cycle as a tag, you might want to know there exists such a thing as itertools.cycle.
It returns an iterator for you to loop forever over an iterable in a cyclic manner. I don't know your original problem, but you might find it useful.
import itertools
for i in itertools.cycle([1, 2, 3]):
# Do something
# 1, 2, 3, 1, 2, 3, 1, 2, 3, ...
Be careful with the exit conditions though, you might find yourself in an endless loop.
You could implement your own class that does this.
class CyclicList(list):
def __getitem__(self, index):
index = index % len(self) if isinstance(index, int) else index
return super().__getitem__(index)
cyclic_list = CyclicList([1, 2, 3, 4])
cyclic_list[4] # 1
In particular this will preserve all other behaviours of list such as slicing.
This question already has answers here:
list comprehension with multiple conditions (python)
(2 answers)
Closed 6 years ago.
In my code, I'm trying to loop through an enumeration object and selectively add some values from the enumeration to a new list using a list comprehension.
This works:
a = [1, 2, 3, 4, 5]
s = [i[1] for i in enumerate(a)]
Of course, that basically just copies list a over to s.
This doesn't work (a being the same list):
s = [i[1] if i[1] != 2 for i in enumerate(a)]
I would think that this would just copy every element of list a over to s besides the 2, but instead I get a syntax error. Anybody know what's going on here?
You misplaced the if part:
s = [i[1] for i in enumerate(a) if i[1] != 2]
This question already has answers here:
Understanding the map function
(6 answers)
Closed 6 years ago.
Is there a way to sort a lot of lists without having to write:
list.sort(list1)
list.sort(list2)
list.sort(list3)
...
for every single list?
It is very tedious when you have a lot of lists
Probably best to use a for-loop:
lists = [1, 2, -1], [2, 0, 6], [91, 3, 82]
for l in lists: l.sort()
list1, list2, list3 = lists
Now each list is sorted accordingly.
You could of course map it, but that's overkill since you'll also need to expand it into a list with list and dump away the resulting Nones produced as a side-effect:
_ = list(map(list.sort, lists))
you don't even Combine your lists to a named variable, so the code takes only 2 lines
l1 = [6,5,4,3,2,1]
l2 = [16,9,4,1]
#start of code
for my_l in [l1,l2]:
list.sort(my_l)
#stop of code
print l1
print l2
This question already has answers here:
Python max and min
(5 answers)
Closed 9 years ago.
I am new to python and am trying to work with lists.
how can i get my function to accept a list of integers and then returns the largest integer in the list?
Use the built-in max function:
>>> L=[2,-7,3,3,6,2,5]
>>> max(L)
6
If you want to use max in a custom function, you could do this:
def getMaxOfList(L):
return max(L)
I don't know why you would want to do this though, since it provides absolutely no new functionality
If you want to write your own implementation of max:
def myMax(L):
answer = None
for i in L:
if i > answer:
answer = i
return answer
use max function:
>>> L=[2,-7,3,3,6,2,5]
>>> L
[2, -7, 3, 3, 6, 2, 5]
>>> max(L)
6
This question already has answers here:
Python Referenced For Loop
(3 answers)
Closed 8 years ago.
I have a for loop as follows:
a=[1,2,3,4,5]
for i in a:
i=6
What I would like is for every element of a to become 6.
Now I know that this for loop won't do it, because I am merely changing the what i refers to.
I instead could write:
a=[1,2,3,4,5]
for i in len(range(a)):
a[i]=6
But that doesn't seem very Pythonic. What are good ways of doing this sort of thing? Obviously, setting everything to 6 is contrived example and, in reality, I would be doing more complicated (and wonderful) things. Therefore, answers should be generalised.
The for-variable is always a simple value, not a reference; there is no way to know that it came from a list and thus write back to the list on changing.
The len(a) approach is the usual idiom, although you need range (or xrange) too:
for i in range(len(a)):
or, just as commonly, use the enumerate function to get indexes as well as values:
for i, v in enumerate(a):
a[i]= v+1
a list comprehension might be a good alternative, eg:
a= [6 for v in a]
This would probably be a good use for map() depending on what you're doing in real life. For example:
a = map(lambda x: 6, a)
You could use:
>>> a = [1, 2, 3, 4, 5]
>>> a = [6] * len(a)
>>> a
[6, 6, 6, 6, 6]