I have the following prettytable headings:
row[0] = ('k','No', 'A', 'I', 'P', 'lambda', 'rho','Wq', 'Est. Cost', 'Time (s)')
t = PrettyTable(row[0])
In the above table, I want to write lambda, rho, and W^q in a math way. Is it possible here in the pretty table? Thanks!
Related
I have such query
qty_used_annotation = Case(
When(scope='ticket', then=BookedTicket.objects.filter(
global_discount_code__code=F('code'), booking__status__in=['i', 'c', 'g', 'r', 's']
).count()),
When(scope='booking', then=Booking.objects.filter(
bookedticket__global_discount_code__code=F('code'),
status__in=['i', 'c', 'g', 'r', 's']
).count()),
output_field=IntegerField()
)
And it is not working. The error is Cannot resolve keyword 'code' into field.
Can somebody explain how to fix it or why it is not working.
Thanks
In subqueries you need to use OuterRef instead of F to access field from outer query:
from django.db.models import OuterRef, Subquery, Count
qty_used_annotation = Case(
When(scope='ticket', then=Subquery(BookedTicket.objects.filter(
global_discount_code__code=OuterRef('code'), booking__status__in=['i', 'c', 'g', 'r', 's']
).annotate(count=Count('id')).values('count'))),
When(scope='booking', then=Subquery(Booking.objects.filter(
bookedticket__global_discount_code__code=OuterRef('code'),
status__in=['i', 'c', 'g', 'r', 's']
).annotate(count=Count('id')).values('count'))),
output_field=IntegerField()
)
Lets say I have 2 dictionaries:
dict_a = {'01A': 'X', '02B': 'V', '03C': 'AE'}
dict_b = {'01A': 'V', '02B': 'D', '03C': 'X'}
They essentially have the same keys. What I want is this:
dict_c = {'01A': ['X', 'V'], '02B': ['V', 'D'], '03C': ['AE', 'X']}
What is the proper way to do this?
There are many ways to achieve that, the one could be using defaultdict from collections something like this.
from collections import defaultdict
dict_a = {'01A': 'X', '02B': 'V', '03C': 'AE'}
dict_b = {'01A': 'V', '02B': 'D', '03C': 'X'}
d = defaultdict(list)
for d1, d2 in dict_a.items() + dict_b.items():
d[d1].append(d2)
print(dict(d))
I have generated a CSV file from excel.
I am trying to read this CSV file using python CSV. However after each row I get /n. How to remove this /n.
Here is my code:
with open('/users/ps/downloads/test.csv','rU') as csvfile
spamreader = csv.reader(csvfile,dialect=csv.excel_tab)
a = []
for row in csvfile:
a.append(row)
print a
I get result like this:
['HEADER\n', 'a\n', 'b\n', 'c\n', 'd\n', 'e']
I want to have results like this:
['HEADER', 'a', 'b', 'c', 'd', 'e']
you could try a replace
a.replace('\n','')
edit:
working verison- a.append(row.replace('\n',''))
You can use strip
x = ['HEADER\n', 'a\n', 'b\n', 'c\n', 'd\n', 'e']
In [6]: def f(word):
...: return word.strip()
...:
In [7]: map(f, x)
Out[7]: ['HEADER', 'a', 'b', 'c', 'd', 'e']
In [8]:
Someone, asked how my input looks like:
The input is an ouput from preceeding function.
And when I do
print(H1_dict)
The following information is printed to the screen:
defaultdict(<class 'list'>, {2480: ['A', 'C', 'C'], 2651: ['T', 'A', 'G']})
which means the data type is defaultdict with (keys, values) as (class, list)
So something like this:
H1dict = {2480: ['A', 'C', 'C'], 2651: ['T', 'A', 'G'].....}
H2dict = {2480: ['C', 'T', 'T'], 2651: ['C', 'C', 'A'].....}
H1_p1_values = {2480: ['0.25', '0.1', '0.083'], 2651: ['0.43', '0.11', '0.23']....}
H1_p2_values = {2480: ['0.15', '0.15', '0.6'], 2651: ['0.26', '0.083', '0.23']....}
H2_p1_values = {2480: ['0.3', '0.19', '0.5'], 2651: ['0.43', '0.17', '0.083']....}
H2_p2_values = {2480: ['0.3', '0.3', '0.1'], 2651: ['0.39', '0.26', '0.21']....}
I want to merge this dictionaries as:
merged_dict (class, list) or (key, values)= {2480: h1['A', 'C', 'C'], h2 ['C', 'T', 'T'], h1_p1['0.25', '0.1', '0.083'], h1_p2['0.15', '0.15', '0.6'], h2_p1['0.3', '0.19', '0.5'], h2_p2['0.3', '0.3', '0.1'], 2651: h1['T', 'A', 'G'], h2['C', 'C', 'A']....}
So, I want to merge several dictionaries using key values but maintain the order in which different dictionary are supplied.
For merging the dictionary I am able to do it partially using:
merged = [haplotype_A, haplotype_B, hapA_freq_My, hapB_freq_My....]
merged_dict = {}
for k in haplotype_A.__iter__():
merged_dict[k] = tuple(merged_dict[k] for merged_dict in merged)
But, I want to add next level of keys infront of each list, so I can access specific items in a large file when needed.
Downstream I want to access the values inside this merged dictionary using keys each time with for-loop. Something like:
for k, v in merged_dict:
h1_p1sum = sum(float(x) for float in v[index] or v[h1_p1])
h1_p1_prod = mul(float(x) for float in v[index] or v[h1_p1])
h1_string = "-".join(str(x) for x in v[h1_index_level]
and the ability to print or write it to the file line by line
print (h1_string)
print (h1_p1_sum)
I am read several examples from defaultdict and other dict but not able to wrap my head around the process. I have been able to do simple operation but something like this seems a little complicated. I would really appreciate any explanation that you may add to the each step of the process.
Thank you in advance !
If I understand you correctly, you want this:
merged = {'h1': haplotype_A, 'h2': haplotype_B, 'h3': hapA_freq_My, ...}
merged_dict = defaultdict(dict)
for var_name in merged:
for k in merged[var_name]:
merged_dict[k][var_name] = merged[var_name][k]
This should give you an output of:
>>>merged_dict
{'2480': {'h1': ['A', 'C', 'C'], 'h2': ['C', 'T', 'T'], ..}, '2651': {...}}
given of course, the variables are the same as your example data given.
You can access them via nested for loops:
for k in merged_dict:
for sub_key in merged_dict[k]:
print(merged_dict[k][sub_key]) # print entire list
for item in merged[k][sub_key]:
print(item) # prints item in list
I am a Python newbie and I've been trying to find the way to generate each possible combination of members from two lists:
left = ['a', 'b', 'c', 'd', 'e']
right = ['f', 'g', 'h', 'i', 'j']
The resulting list should be something like:
af ag ah ai aj bf bg bh bi bj cf cg ch ci cj etc...
I made several experiments with loops but I can't get it right:
The zip function but it wasn't useful since it just pairs 1 to 1 members:
for x in zip(left,right):
print x
and looping one list for the other just returns the members of one list repeated as many times as the number of members of the second list :(
Any help will be appreciated. Thanks in advance.
You can use for example list comprehension:
left = ['a', 'b', 'c', 'd', 'e']
right = ['f', 'g', 'h', 'i', 'j']
result = [lc + rc for lc in left for rc in right]
print result
The result will look like:
['af', 'ag', 'ah', 'ai', 'aj', 'bf', 'bg', 'bh', 'bi', 'bj', 'cf', 'cg', 'ch', 'ci', 'cj', 'df', 'dg', 'dh', 'di', 'dj', 'ef', 'eg', 'eh', 'ei', 'ej']