Change initial element of a list keeping the sequence [duplicate] - python

This question already has answers here:
Efficient way to rotate a list in python
(27 answers)
Circular list iterator in Python
(9 answers)
Closed 2 years ago.
I have an algorithm that outputs a list in a specific order, for example:
[0 4 3 2 1 5]
I want to reorder the list to start with the element '1' and keep the sequence, so my output would be:
[1 5 0 4 3 2]
I've searched and tried different possibilities but I'm still struggling with it.
How can I make this work?

lst = [0, 4, 3, 2, 1, 5]
to rotate it into position:
i = lst.index(1)
lst = lst[i:] + lst[:i]

Related

Python - Iterate over 2 lists of a different length in a specific way [duplicate]

This question already has answers here:
How to zip two differently sized lists, repeating the shorter list?
(15 answers)
Closed 7 months ago.
I have 2 lists of a different length
list_1 = [1, 2, 3, 4, 5]
list_2 = ['a', 'b']
If I'd do:
for (i,j) in itertools.zip_longest(list_1, list_2):
print (i,j)
The output would be
1 a
2 b
3 None
4 None
5 None
What I want is to iterate over those 2 lists, but repeat the shorter list when it gets exhausted any amount of times while the longer list still iterates. In this example I'd want output to be
1 a
2 b
3 a
4 b
5 a
There's an itertools for that. You want to cycle the second list and do a vanilla zip on the first. cycle will remember and reemit values from list_2 and zip will stop at the end of list_1.
>>> import itertools
>>> list_1 = [1, 2, 3, 4, 5]
>>> list_2 = ['a', 'b']
>>> for i,j in zip(list_1, itertools.cycle(list_2)):
... print(i, j)
...
1 a
2 b
3 a
4 b
5 a
if you want the result to always be the longer of the two lists (either could cycle), you'd need to choose which one uses itertools.cycle. There are a dozen ways to do that, but here's one
>>> zipper = zip(list_1, itertools.cycle(list_2)) if len(list_1) >= len(list_2) else zip(itertools.cycle(list_1), list_2)
>>> for i, j in zipper:
... print(i, j)
...
1 a
2 b
3 a
4 b
5 a
And if you want something that works for iterators in general (they wouldn't know their size in advance), you could make them into a list first.

is index same for same elements? [duplicate]

This question already has answers here:
Index of duplicates items in a python list
(21 answers)
Closed 1 year ago.
nums = [11, 2,4, 2, 5]
for i in nums:
print(nums.index(i),i )
I run the above code and it uses same index for similar elements(here 2 at index 1 and 3).
I wasn't aware of this python list behavior, does it mean the use of lists can be restrictive for similar elements in same list?
The index() method returns the position at the first occurrence of the specified value.
So it returns first index of number 2.
nums = [11, 2,4, 2, 5]
You can enumerate() to get all indexes.
nums = [11, 2,4, 2, 5]
for i,n in enumerate(nums):
print(i, n)
0 11
1 2
2 4
3 2
4 5

how to pick items from different list inside for loop? [duplicate]

This question already has answers here:
An elegant and fast way to consecutively iterate over two or more containers in Python?
(10 answers)
Closed 4 years ago.
i have two list and i want to use items from both lists inside for loop
here's my code
list1 = ['a','b','c']
list2 = ['1','2','3']
for x in list1:
print(x)
for y in list2:
print(y)
This is what it prints
a
1
2
3
b
1
2
3
c
1
2
3
this is want i want it to print
a
1
b
2
c
3
list1 = ['a','b','c']
list2 = ['1','2','3']
for x,y in zip(list1,list2):
print(x)
print(y)
zip iterates the lists together.

How do I iterate through combinations of a list [duplicate]

This question already has answers here:
Making all possible combinations of a list [duplicate]
(6 answers)
Closed 6 years ago.
I have a list of elements, say
list = [1, 2, 3, 4]
and I would like to iterate through couples of distinct elements of this list, so
for x, y in some_iterator(list):
print x, y
should show
1 2
1 3
1 4
2 3
2 4
3 4
Note that I don't want all combinations of list as in this question. Just the combinations of a given length.
What would be the most pythonic way of doing this ?
What if I wanted to do the same with n-uples ? For instance with combinations of 3 elements out of n
for x, y, z in another_iterator(list):
print x, y, z
would show
1 2 3
1 2 4
2 3 4
Use itertools.combinations:
from itertools import combinations
for combo in combinations(lst, 2): # 2 for pairs, 3 for triplets, etc
print(combo)

Can't delete not last single object [duplicate]

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 6 years ago.
My code should recieve a list of numbers and then output on the screen the only numbers which repeat more then once. I don't know why but it don't work with the numbers in the middle of list. My code:
a = [int(i) for i in (input().split())]
a.sort()
for number in a:
if a.count(number)==1:
a.remove(number)
else:
a.remove(a.count(number)-a.count(number)+number)
for number in a:
print(number, end=' ')
I tried changing if on while on 4th string, but then the last number is left in the list.
It should work like:
Sample Input 1: 4 8 0 3 4 2 0 3 Sample Output 1: 0 3 4
Sample Input 2: 10 Sample Output 2:
Sample Input 3: 1 1 2 2 3 3 Sample Output 3: 1 2 3
Sample Input 4: 1 1 1 1 1 2 2 2 Sample Output 4: 1 2
You could approach this problem using set instead:
a = list(map(int, input().split()))
print(" ".join(map(str, set(i for i in a if a.count(i) > 1))))
Explanation:
Firstly, it looks like you should read up on the map function. Instead of a = [int(i) for i in (input().split())], you can just use list(map(int, input().split())), which is much more Pythonic.
Secondly, the important part of the second line is set(i for i in a if a.count(i) > 1), which just creates a new list containing only the duplicates from a (i.e. [1, 2, 3, 2, 3] becomes [2, 3, 2, 3] and then applies set to it, which converts [2, 3, 2, 3] into {2, 3} (which is a set object).
In case you're wondering what map(str, ...) is for, it's so that you can print each of the elements inside your new set (e.g. {2, 3}).
You can use built-in lib collections to count list items and filter it by a required condition.
import collections
ints = map(int, input().split())
count = collections.Counter(ints)
print filter(lambda x: count[x] > 1, count)

Categories