Python output in Excel - excel

Trying to get the output of the cartesian product in excel using xlsxwriter and arrange them column-wise but have not been successful.
Intended Result:
If the first output is (A,B,C,D,E) the output should be displayed in excel in the following manner:
Row 0, Col 0 = A
Row 0, Col 1 = B
Row 0, Col 2 = C
Row 0, Col 3 = D
Row 0, Col 4 = E
then row+=1 and the next set of results are displayed in the same manner.
Code:
list1 = ['A', 'B', 'C', 'D', 'E', 'F']
list2 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I']
list3 = ['A', 'B', 'C', 'D', 'E']
list4 = ['A', 'B', 'C', 'D']
list5 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I','J','K','L','M']
list = [(p,q,r,s,t) for p in list1 for q in list2 for r in list3 for s in
list4 for t in list5]
x = print(list)
import xlsxwriter
workbook = xlsxwriter.workbook('Stat.xlsx')
worksheet = workbook.add_worksheet()
row = 0
col = 0
for Apple, Boy, Cat, Dog, Eagle in (x):
worksheet.write (row, col, Apple)
worksheet.write(row, col + 1, Boy)
worksheet.write(row, col + 1, Cat)
worksheet.write(row, col + 1, Dog)
worksheet.write(row, col + 1, Eagle)
row += 1
workbook.close()

Does this do what you want?
list1 = ['A', 'B', 'C', 'D', 'E', 'F']
list2 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I']
list3 = ['A', 'B', 'C', 'D', 'E']
list4 = ['A', 'B', 'C', 'D']
list5 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I','J','K','L','M']
# You probably could use product from itertools for this: https://docs.python.org/2/library/itertools.html#itertools.product
list_combined = [(p,q,r,s,t) for p in list1
for q in list2
for r in list3
for s in list4
for t in list5]
import xlsxwriter
workbook = xlsxwriter.Workbook('Stat.xlsx')
worksheet = workbook.add_worksheet()
for row, group in enumerate(list_combined):
for col in range(5):
worksheet.write (row, col, group[col])
workbook.close()

Related

Set itertools product maximum repeat value per element

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.

Appending no of lists to an existing excel file starting from a specific cells with Python, Openpyxl

I'm getting no of list's in my_list variable trough iterating a dictionary -> employee_data, its not a list of list its coming trough in for loop one by one, look into code below...
print(my_list)
['A', 'B', 'C', 'D', 'E']
['B', 'C', 'B', 'B', 'B']
['C', 'C', 'C', 'C', 'C']
['A', 'B', 'C', 'D', 'E']
['D', 'D', 'D', 'D', 'D']
I just want to add those list's item in excel from cell between ws['C3':'H' + str(ws.max_row)] like this is it possible in loop ?
and my code is
for key,day_value in employee_data.items():
attendance_array = []
for val_key, get_value in day_value.items():
attendance_array.append(get_value)
my_list = attendance_array[n:]
print(my_list)
for rw in ws['C3':'H' + str(ws.max_row)]:
for cell, val in zip(rw, my_list):
cell.value = val

Is there any way to add this two array to get a new list in python

I want to simulate a left outer join of these two arrays
array = ['a', 'b','c', 'd', 'e']
array2 = ['a', 'a', 'f', 'g', 'b']
which the results will be as follows.
opt = ['aa', 'aa', ,'bb','c_', 'd_', 'e_']
If you want to do an outer left join this might work:
array = ['a', 'b', 'c', 'd', 'e']
array2 = ['a', 'a', 'f', 'g', 'b']
array3 = []
for i in array:
subarray = []
for j in array2:
if i == j:
subarray.append(i+j)
else:
subarray.append(i+"_")
if any([n[1] != "_" for n in subarray]):
for n in subarray:
if n[1] != "_":
array3.append(n)
else:
array3.append(i + "_")
The question was a bit ambiguous as the comments have pointed out.
But it appears from the question that you are trying to get all the combinations (and not just add the two lists)...
If this is the case then nested for loops (or list comprehension) are two basic methods:
array = ['a', 'b', 'c', 'd', 'e']
array2 =['a', 'a', 'f', 'g', 'b']
all = []
for i in array:
for j in array2:
all.append(i+j)
print(all)
returns this:
['aa', 'aa', 'af', 'ag', 'ab', 'ba', 'ba', 'bf', 'bg', 'bb', 'ca', 'ca', 'cf', 'cg', 'cb', 'da', 'da', 'df', 'dg', 'db', 'ea', 'ea', 'ef', 'eg', 'eb']
If you are combining the lists then this would work:
array = ['a', 'b','c', 'd', 'e']
array2 =['a', 'a', 'f', 'g', 'b']
length = len(array)
new = []
for i in range(length):
# some rules here
v = array[i] + array2[i]
new.append(v)
new
which gives this:
['aa', 'ba', 'cf', 'dg', 'eb']
In both cases, you can add rules in the (nested) for loops.

How do I remove all elements that occur more than once in a list? [duplicate]

Given a list of strings I want to remove the duplicates and original word.
For example:
lst = ['a', 'b', 'c', 'c', 'c', 'd', 'e', 'e']
The output should have the duplicates removed,
so something like this ['a', 'b', 'd']
I do not need to preserve the order.
Use a collections.Counter() object, then keep only those values with a count of 1:
from collections import counter
[k for k, v in Counter(lst).items() if v == 1]
This is a O(N) algorithm; you just need to loop through the list of N items once, then a second loop over fewer items (< N) to extract those values that appear just once.
If order is important and you are using Python < 3.6, separate the steps:
counts = Counter(lst)
[k for k in lst if counts[k] == 1]
Demo:
>>> from collections import Counter
>>> lst = ['a', 'b', 'c', 'c', 'c', 'd', 'e', 'e']
>>> [k for k, v in Counter(lst).items() if v == 1]
['a', 'b', 'd']
>>> counts = Counter(lst)
>>> [k for k in lst if counts[k] == 1]
['a', 'b', 'd']
That the order is the same for both approaches is a coincidence; for Python versions before Python 3.6, other inputs may result in a different order.
In Python 3.6 the implementation for dictionaries changed and input order is now retained.
t = ['a', 'b', 'c', 'c', 'c', 'd', 'e', 'e']
print [a for a in t if t.count(a) == 1]
lst = ['a', 'b', 'c', 'c', 'c', 'd', 'e', 'e']
from collections import Counter
c = Counter(lst)
print([k for k,v in c.items() if v == 1 ])
collections.Counter will count the occurrences of each element, we keep the elements whose count/value is == 1 with if v == 1
#Padraic:
If your list is:
lst = ['a', 'b', 'c', 'c', 'c', 'd', 'e', 'e']
then
list(set(lst))
would return the following:
['a', 'c', 'b', 'e', 'd']
which is not the thing adhankar wants..
Filtering all duplicates completely can be easily done with a list comprehension:
[item for item in lst if lst.count(item) == 1]
The output of this would be:
['a', 'b', 'd']
item stands for every item in the list lst, but it is only appended to the new list if lst.count(item) equals 1, which ensures, that the item only exists once in the original list lst.
Look up List Comprehension for more information: Python list comprehension documentation
You could make a secondary empty list and only append items that aren't already in it.
oldList = ['a', 'b', 'c', 'c', 'c', 'd', 'e', 'e']
newList = []
for item in oldList:
if item not in newList:
newList.append(item)
print newList
I don't have an interpreter with me, but the logic seems sound.

how to return two list in two variables in python 3

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]

Resources