>>> l1 = ['A', 'B', 'C']
>>> l2 = ['G', 'F', 'K', 'J']
>>> for (i, j) in zip(l1, l2):
print(i, j)
l1.pop(0)
print(l1)
print(l2)
output is :
'A'
A G
['B', 'C']
['G', 'F', 'K', 'J']
'B'
C F
['C']
['G', 'F', 'K', 'J']
Expected :
'A'
A G # line 2
['B', 'C']
['G', 'F', 'K', 'J']
'B'
C F # line 3
['C']
['G', 'F', 'K', 'J']
l2 still has ['C']. Why ? Also, at line 2 how come A is there, when A is already popped.
If it means, that i took the value already before popping, then why line 3 has C F. It should be B F.
Please explain. I am totally confused here.
I don't see a problem because you are popping A after the zip function is executed. So even if you remove all elements in l1 and l2 in the first iteration, the loop executes the same because the zip() function had already returned the values before first iteration. range(), zip() or whatever function you provide on the for in , it will execute first, then it will iterate, modifying the original values won't change anything, because it is already returned as a separate value.
Related
I want to generate different combinations of 3 elements a, b, and c. The length of these combinations needs to be 4. I want to have a maximum of 4 times from 'a' and a maximum 1 time from each 'b' and 'c' element. So, for example, we can have ['a',' a',' a','a'] or ['a','a','b','c'] but not ['a','b','b','b'].
There is a similar question in 1, but, as far as I know, using the last 'gen' function, the length of a generation is controlled by the multiplication of a maximum number of repetitions (4 in my case). Also, cases were limited to tuples with exactly 1 'b' and 1 'c' and the rest are 'a'. For the last issue, I replaced 'combinations' with 'combinations_with_replacement', but it still produces tuples with 4 elements and there is no ['a',' a',' a','a'].
How can I tackle this problem?
Here is the code:
from itertools import combinations_with_replacement
def gen(ns, elems=None, C=None, out=None):
if elems is None:
elems = list(range(len(ns)))
else:
assert len(elems) == len(ns)
if out is None:
N = 1
for n in ns:
N *= n
out = [elems[0]]*N
C = range(N)
if len(ns) == 1:
yield out
else:
n = ns[-1]
e = elems[-1]
for c in combinations_with_replacement(C,n):
out_ = out.copy()
for i in c:
out_[i] = e
C_ = [i for i in C if i not in c]
yield from gen(ns[:-1], elems[:-1], C_, out_)
for tmp_list in gen([4,1,1], elems=['a', 'b', 'c'], C=None, out=None):
print(tmp_list)
output:
['c', 'b', 'a', 'a']
['c', 'a', 'b', 'a']
['c', 'a', 'a', 'b']
['b', 'c', 'a', 'a']
['a', 'c', 'b', 'a']
['a', 'c', 'a', 'b']
['b', 'a', 'c', 'a']
['a', 'b', 'c', 'a']
['a', 'a', 'c', 'b']
['b', 'a', 'a', 'c']
['a', 'b', 'a', 'c']
['a', 'a', 'b', 'c']
Please note that since I care about the execution time, I want to generate the tuples in a loop.
Given a list of lists:
list_format = [['a', 'c', 'f', 'b'], ['j', 'l', 'o', 'c'], ['q', 's', 'v', 'e']]
'c', 'f', 'b' must be mapped to 'a'
'l', 'o', 'c' must be mapped to 'j'
's', 'v', 'e' must be mapped to 'q'
The output should look like this:
[['a','c'],['a','f'],['a','b'],['j','l'],['j','o'],['j','c'],['q','s'],['q','v'],['q','e']]
I've tried so far:
list_dict = {element[0]:element[1:] for element in list_format}
newer_lst = []
for key, value in list_dict.items():
newer_lst.append((key, value))
newer_lst
Gives me the output of tuples:
[('a', ['c', 'f', 'b']), ('j', ['l', 'o', 'c']), ('q', ['s', 'v', 'e'])]
I'm newer at this and trying to rearrange, any advice would be awesome, been stuck for days with trial and error(searched google countless times and constantly googling. I feel I'm getting close but can't seem to put it together.
Here is a one-liner, using slicing:
[[i[0],j] for i in list_format for j in i[1:]]
gives:
[['a', 'c'], ['a', 'f'], ['a', 'b'], ['j', 'l'], ['j', 'o'], ['j', 'c'], ['q', 's'], ['q', 'v'], ['q', 'e']]
Also, if you iterate through your value variable, you get your result:
list_dict = {element[0]:element[1:] for element in list_format}
newer_lst = []
for key, value in list_dict.items():
for i in value:
newer_lst.append((key, i))
print(newer_lst)
You don't need to create a loop, just loop on sub array and append new sub array to main output array on the fly, like new_list.append([lst[0], item])
new_list = []
for lst in list_format:
for item in lst[1:]:
new_list.append([lst[0], item])
print(new_list)
#output
#[['a', 'c'], ['a', 'f'], ['a', 'b'], ['j', 'l'], ['j', 'o'], ['j', 'c'], ['q', 's'], ['q', 'v'], ['q', 'e']]
I need to do this:
from collections import deque
def list3_to2(list1, list2, list3):
Left = []
Right = []
q = deque()
for a, b, c in list1, list2, list3:
q.append(a)
q.append(b)
q.append(c)
tmp = 1
while q:
if tmp % 2 == 0:
Left.append(q.popleft())
else:
Right.append(q.popleft())
tmp += 1
return Left, Right
a = ['a', 'b', 'c']
b = ['d', 'e', 'f']
c = ['g', 'h', 'i']
l, r = list3_to2(a, b, c)
print(l)
print(r)
But instead of two lists in result i got four lists.
Output:
['b', 'd', 'f', 'h']
['a', 'c', 'e', 'g', 'i']
['b', 'd', 'f', 'h']
['a', 'c', 'e', 'g', 'i']
What i'm doing wrong?
Basically i need to transform 3 lists into 2 lists using deque with correct order.
Thank to everyone. I got it. My function just returning a tuple. That's why i got both lists in varibles l and r. Just need to type l = list3_to2(a, b, c)[0] and r = list3_to2(a, b, c)[1]
I am very new to python and i was stuck with this. I need to create a list of lists that is
formed from this list c: ['asdf','bbnm','rtyu','qwer'].
I need to create something like this:
b: [['a','s','d','f'],['b','b','n','m'],['r','t','y','u'],['q','w','e','r']]
I tried using a for-loop, but it is not working out. I don't know what mistake I am making.
You can use a list comprehension with list():
>>> c = ['asdf','bbnm','rtyu','qwer']
>>>
>>> b = [list(s) for s in c]
>>> b
[['a', 's', 'd', 'f'], ['b', 'b', 'n', 'm'], ['r', 't', 'y', 'u'], ['q', 'w', 'e', 'r']]
Notice that calling list() with a string argument returns a list containing the characters of that string:
>>> list('abc')
['a', 'b', 'c']
What we're doing above is applying this to every element of the list via the comprehension.
Use map function.
>>> a= ['asdf','bbnm','rtyu','qwer']
>>> map(list ,a )
[['a', 's', 'd', 'f'], ['b', 'b', 'n', 'm'], ['r', 't', 'y', 'u'], ['q', 'w', 'e', 'r']]
>>>
You could do something that's easier to understand:
b = []
for x in c:
list(x).append(b)
How do I write a syntax for something like this?
if our list 1 = ['F', 'W', 'I', 'N', 'T', 'E', 'R', 'S']
our second list looks like = ['WINTER']
how would I write that list 1 contains a sequence of strings WINTER?
You could convert both to sets and use issubset ?
>>> list1
['F', 'W', 'I', 'N', 'T', 'E', 'R', 'S']
>>> list2
['W', 'I', 'N', 'T', 'E', 'R']
>>> set(list2).issubset(set(list1))
True
Or maybe convert them both to sets and then test list2 - list1 ?
Or (taken straight from the docs):
{x for x in list2 if x not in list1}