I have a word length_of_word | repetitions dictionary and I want to make a histogram of that like the one in link below using only python built in functions no numpy or anything like it.
http://dev.collabshot.com/show/723400/
Please help me out at least with some pointers.
I guess you must have a dict that looks like this one, right ?
>>> d = {1:1, 2:10, 3:10, 4:6, 5:5, 6:4, 7:2, 8:1}
>>> d
{1: 1, 2: 10, 3: 10, 4: 6, 5: 5, 6: 4, 7: 2, 8: 1}
If so, I have a function that does the trick:
>>> def histo(dict_words):
# Get max values, plus delta to ease display
x_max = max(dict_words.keys()) + 2
y_max = max(dict_words.values()) + 2
# print line per line
print '^'
for j in range(y_max, 0, -1):
s = '|'
for i in range(1, x_max):
if i in dict_words.keys() and dict_words[i] >= j:
s += '***'
else:
s += ' '
print s
# print x axis
s = '+'
for i in range(1, x_max):
s += '---'
s += '>'
print s
# print indexes
s = ' '
for i in range(1, x_max):
s += ' %d ' % i
print s
>>> histo(d)
^
|
|
| ******
| ******
| ******
| ******
| *********
| ************
| ***************
| ***************
| ******************
|************************
+--------------------------->
1 2 3 4 5 6 7 8 9
>>>
Ok, there's a little more work to display values on the left and to format correctly numbers greater than 10 not to have shift in indexes, but I think it's a good start :-)
Related
I want to do
df[(df['col']==50) | (df['col']==150) | etc ..]
"etc" is size changing from 1 to many
so I do a loop
result is like
str= "(df['col']==50) | (df['col']==150) | (df['col']==100)"
then I do this
df[str]
but this does not work
How can I make it work ?
A simple solution:
list_of_numbers = [50,150]
df[df["col"].isin(list_of_numbers)]
Where list_of_numbers are the numbers you want to include in the condition. I'm assuming here your condition is always or.
Use query to filter a dataframe from a string
df = pd.DataFrame({'col': range(25, 225, 25)})
l = [50, 100, 150]
q = ' | '.join([f"col == {i}" for i in l])
out = df.query(f)
>>> q
'col == 50 | col == 100 | col == 150'
>>> out
col
1 50
3 100
5 150
I want to print a Python dictionary with keys in one row and values in another row.
Example:
d = {'a':'long val', 'b':None, 'long key':3, 'd':4}
I want to print it as
a | b | long key | d
long val | | 3 | 4
How can I do this?
I'd say that either you iterate twice or you build the two output lines first and then display them:
d = {'a': 'long val', 'b': None, 'long key': 3, 'd': 4}
headers = ""
row = ""
for k, v in d.items():
if row != "":
row += " | "
headers += " | "
max_len = max(len(str(k)), len(str(v)) if v else 1)
headers += str(k).center(max_len)
row += str(v).center(max_len) if v else " "
print(headers)
print(row)
But it looks like a lot of hassle to avoid a double iteration over a simple dict.
Ok, here is the updated version, which is scalable
print(*["{!s:^16}|".format(item) for item in d.keys()] )
print(*["{!s:^16}|".format(item) for item in d.values()])
output:
a | b | long key | d
long val | None | 3 | 4
Lets say I want to print out
Item 1 | Item 2
A Third item | #4
Which can be done without the | by doing
print('%s20 %20s' % (value1,value2))
How would I go about placing the | character so that it is evenly justified between the two formatted values?
I suppose I could manually could the length of the formatted string without the | character and then insert the | in the middle but I am hoping for a more elegant solution!
Thank you very much for your time.
Edit: Here is a solution that I suggested would be possible
def PrintDoubleColumn(value1, value2):
initial = '%s20 %20s' % (value1,value2)
lenOfInitial = len(initial)
print(initial[:lenOfInitial/2] +'|' + initial[lenOfInitial/2+1:])
There is a good source for string format operations: https://pyformat.info/#string_pad_align
x = [1, 2, 3, 4, 5]
y = ['a', 'b', 'c', 'd', 'e']
for i in range(0, 5):
print "{0:<20}|{1:>20}".format(x[i], y[i])
Result:
1 | a
2 | b
3 | c
4 | d
5 | e
I have a list of data that I'm trying to find the max value from with python. My current code will loop over the data and render all possible combinations of the data, however I can't figure out how to render the max from the results.
Below is my current setup:
street = %sql SELECT * FROM streets
for i in range(len(flight)):
for j in range(len(flight)):
for k in range(len(flight)):
A = flight[i][2]
B = flight[k][2]
num = flight[i][4] , flight[j][4] , flight[k][4]
numsum = sum(num)
print A, B, numsum
Printing flight will render the below
+----+-----------+----------------------+----------------------+---+
| id | flight | Start | End |dis|
+----+-----------+----------------------+----------------------+---+
| 0 | w | SFO | DEN | 4 |
| 1 | e | DEN | NYC | 7 |
| 1 | e | DEN | ORD | 7 |
However the max with throw the below error.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-283-770cd29ebd83> in <module>()
8 num = street[i][4] , street[j][4] , street[k][4]
9 numsum = sum(num)
---> 10 print A, B, max(numsum)
11
TypeError: 'int' object is not iterable
If I remove the max from the last line everything in the database will print. For example:
SFO ORD 35
DEN JFK 12
SFO JFK 94
LAX DEN 54
...
Can someone help me figure out how to get the max value in numsum so the result prints like this:
SFO JFK 94
Thanks in advance!
You're not doing what you're trying to do. Your algorithm isn't well thought-out. look at it this way:
for each_item in whatever_iterator:
a = each_item[some_element]
b = each_item[another_element]
num = some, other, numbers
sumnum = sum(num) # hey that was easy!
print a, b, sumnum # every time through, let's print it!
Nowhere does this find the LARGEST. In order to do that, you'd want to iterate through and save current_max = max(current_max, new_value)
Looks like what you're looking to do is:
max_sumnum = (0, 0, 0)
for i, j, k in itertools.product(len(x), repeat=3):
num = x[i][4], x[j][4], x[???][1][k][4]
cur_sumnum = x[i][2], x[k][2], sum(num)
max_sumnum = max(max_numsum, cur_sumnum, key=lambda tup: tup[2])
print max_sumnum
I use itertools.product here because it's a great shortcut for nested for loops.
for i in range(3):
for j in range(5):
for k in range(100):
for m in range(2):
foo(i, j, k, m)
# equivalent to....
for i, j, k, m in itertools.product(range(3), range(5),
range(100), range(2)):
And I use the repeat keyword since you're doing a bunch of the same loops.
for i in range(3):
for j in range(3):
for k in range(3):
# equivalent to....
for i, j, k in itertools.product(range(3), repeat=3):
in book composing program chapter 2.3, there is a function print_part() makes me confused (whole code here):
>>> def print_parts(tree, partition=[]):
if is_leaf(tree):
if root(tree):
print(' + '.join(partition))
else:
left, right = branches(tree)
m = str(root(tree))
print_parts(left, partition + [m])
print_parts(right, partition)
>>> print_parts(partition_tree(6, 4))
4 + 2
4 + 1 + 1
3 + 3
3 + 2 + 1
3 + 1 + 1 + 1
2 + 2 + 2
2 + 2 + 1 + 1
2 + 1 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1 + 1
this function print all the ways using parts up to 4 to partition 6. I've understand how the partition algorithm works in partition_tree() and have no problem to understand a partition tree:
4
_________________________
| |
4 3
_______ _____
| | | |
.. ... ... ...
but I still don't know how to print partition ways from a partition tree. especially these lines:
print(' + '.join(partition))
print_parts(left, partition + [m])
print_parts(right, partition)
# why call twice? and the recursion here didn't looks like the
# way the print_parts() called in the beginning.
update:
recursion here didn't looks like the way the print_parts() called in the beginning.
take an simpler args as example to illustrate my confusion:
>>> print_parts(partition_tree(3, 2))
2 + 1
1 + 1 + 1
the partition tree is:
2
--------------------------------
2 1
---------------- ------------------------
F 1 1 F
------ ------------
T F 1 F
-------
T F
or
[2, [2, [False], [1, [True], [False]]], [1, [1, [1, [True], [False]],[False]],[False]]]
the above list is first pass into the func print_parts() as trees's value.
when going to this line:
print_parts(left, partition + [m])
the left value is
[[2, [False], [1, [True], [False]]], [1, [1, [1, [True], [False]],[False]],[False]]]
it's no longer a tree because in the definition the tree should have a structure like: [node, [branch],[branch]]. if so, the recursion cannot works.
It sounds like you understand the algorithm but maybe not the Python code.
print(' + '.join(partition)) is just formatting a list into the output format that you see. For example, it turns the list [3, 2, 1] into the string '3 + 2 + 1'.
Then the reason the way print_parts can be called in a way that "doesn't look the same" as the original call is that the method definition allows for an optional second argument. If the second option is not explicitly provided, it is by default set to an empty list. That's what partition=[] in the function definition does.
As for why it's being called "twice", it's just that at each step of the recursion, we're branching into two branches (left and right), and then recursively processing both the left side and the right side.
Update (reply to your update):
The confusion stems from this line where Python can assign multiple variables at once:
left, right = branches(tree)
Since branches(tree) is a list of length 2, the 2 elements get assigned to left and right respectively.
So it is not true what you said:
the left value is
[[2, [False], [1, [True], [False]]], [1, [1, [1, [True], [False]],[False]],[False]]]
But instead:
left, right = branches(partition_tree(3, 2))
print left
> [2, [False], [1, [True], [False]]]
print right
> [1, [1, [1, [True], [False]], [False]], [False]]