This question already has answers here:
How to get all possible combinations of a list’s elements?
(32 answers)
Closed 6 years ago.
I have 2 identical lists
a = [a1,a2,a3]
b = [a1,a2,a3]
What is a most effective way to iterate over these 2 list simultaneously while i am interesting only in combination of different elements from both lists despite order, i.e a1a2 and a1a3. Combinations a1a1, a2a2, a3a3, a2a1, a3a1 i am interesting to skip, but interesting keep iterators values avaliable.
Want to re phrase questions:
interesting in possible combinations of 2 elements from list a = [a1,a2,a3]
Use combinations,
from itertools import combinations
for i in combinations(['a1','a2','a3'],2):
print i
List comprehensions!
a = ['1', '2', '3']
b = ['1', '2', '3']
c = [i + j for i in a for j in b if j != i]
print(c) # prints -> ['12', '13', '21', '23', '31', '32']
EDIT
if you consider a1a2 and a2a1 to be duplicates you can use some clever slicing to skip them like so:
c = [ia + ib for i, ia in enumerate(a) for ib in a[i+1:]]
print(c) # prints -> ['12', '13', '23']
As you might notice, list b is not used in the second one.
Related
This question already has answers here:
Generate string of numbers python
(2 answers)
Closed 2 years ago.
I would like to generate a list of numbers such as this:
['1','2','3','4','5','6']
but all I can seem to get is:
[1, 2, 3, 4, 5, 6]
using this code:
values = list(range(1,7))
Is there some easy way to generate a list of consecutive numbers, that are within apostrophes, since I want the code to treat them like a string.
I have tried:
values = str(list(range(1,7)))
but that just gave me the same result as above.
>>> [str(i) for i in range(1,7)]
['1', '2', '3', '4', '5', '6']
The comprehension proposed by #CoryKramer has you covered, but given your attempts, you might be looking for:
>>> list(map(str, range(1, 7)))
['1', '2', '3', '4', '5', '6']
Try the following:
list(map(str, range(1,7)))
I want to generate a nested 2 level list from the input numbers. The end of the line is 'enter'.
a = [[i for i in input().split()] for i in input().split (sep = '\ n')]
In this case, this takes only the second line.
For example:
1 2 3
4 5 6
7 8 9
It will output like this:
[['4', '5', '6']]
I want to get the final result like this:
[['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']]
Help find a mistake. Thanks.
One way to do it would be:
[x.split() for x in data.splitlines()]
Or if you want the items to be an int:
[[int(x) for x in x.split()] for x in data.splitlines()]
Code:
a = [[j for j in i.split()] for i in input().split(sep = '\n')]
You want the inside list to enumerate over the elements of the outside list.
Besides, remove the extra spaces.
This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 5 years ago.
I frequently run into a problem where I'm trying to make a list of lists of a certain length from a string.
This is an example where I have a string, but would like to make a list of lists of length 3:
x = '123456789'
target_length = 3
new = [i for i in x]
final = [new[i:i+target_length] for i in range(0, len(x), target_length)]
print(final)
Output:
[['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']]
So, this works, but feels so clunky.
Is there a better way to combine these arguments into one line or do you think that would make the code unreadable?
If you want to do it in one line you can just create the lists inside your comprehension:
x = '123456789'
target_length = 3
[list(x[i:i+target_length]) for i in range(0, len(x), target_length)]
>> [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']]
This does it in one line:
f2 = [[x[(j * target_length) + i] for i in range(target_length)] for j in range(target_length)]
I'm trying to remove the odd-indexed elements from my list (where zero is considered even) but removing them this way won't work because it throws off the index values.
lst = ['712490959', '2', '623726061', '2', '552157404', '2', '1285252944', '2', '1130181076', '2', '552157404', '3', '545600725', '0']
def remove_odd_elements(lst):
i=0
for element in lst:
if i % 2 == 0:
pass
else:
lst.remove(element)
i = i + 1
How can I iterate over my list and cleanly remove those odd-indexed elements?
You can delete all odd items in one go using a slice:
del lst[1::2]
Demo:
>>> lst = ['712490959', '2', '623726061', '2', '552157404', '2', '1285252944', '2', '1130181076', '2', '552157404', '3', '545600725', '0']
>>> del lst[1::2]
>>> lst
['712490959', '623726061', '552157404', '1285252944', '1130181076', '552157404', '545600725']
You cannot delete elements from a list while you iterate over it, because the list iterator doesn't adjust as you delete items. See Loop "Forgets" to Remove Some Items what happens when you try.
An alternative would be to build a new list object to replace the old, using a list comprehension with enumerate() providing the indices:
lst = [v for i, v in enumerate(lst) if i % 2 == 0]
This keeps the even elements, rather than remove the odd elements.
Since you want to eliminate odd items and keep the even ones , you can use a filter as follows :
>>>filtered_lst=list(filter(lambda x : x % 2 ==0 , lst))
this approach has the overhead of creating a new list.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Merge two lists in python?
How can I prepend list elements into another list?
A = ['1', '2']
B = ['3', '4']
A.append(B)
print A
returns
['1', '2', ['3', '4']]
How can I make it
['1', '2', '3', '4']?
A.extend(B)
or
A += B
This text added to let me post this answer.
list.extend, for example, in your case, A.extend(B).
this can also be done as
for s in B:
A.append(B)
of course A.extend(B) does the same work but using append we need to add in the above f