[0] * 10
is okay.
But,
initializing using
[ [0] ] * 10
So you get [ [0] , [0], ... [0] ]
is NOT okay - you've built a list of copies of the SAME list!!!
You wouldn't guess would you? This is where Larry Wall would have dumped out a nice warning. Why can't python marry with Perl?
Hard-to-find tips on otherwise easy-to-do tasks involving everyday technology, with some advanced insight on history and culture thrown in. Brought to you by a master dabbler. T-S T-S's mission is to boost your competitiveness with every visit. This blog is committed to the elimination of the rat from the tree of evolution and the crust of the earth.
Thursday, May 31, 2018
Tuesday, May 29, 2018
Contrasting Styles - Inverting an Adjacency Graph
Mine :
def inverse_graph(graph):
invG = []
for i,row in enumerate(graph) :
sieve = [1] * len( row )
sieve[i] = 0
invG.append( [xor(x[0], x[1]) for x in zip(row, sieve)] )
return invG
Instructor :
def inverse_graph(graph):
n = len( graph )
invG = []
for i in range( n ) :
invG.append( [] )
for j in range( n ) :
if i != j :
invG[i].append( 1 - graph[i][j] )
else :
invG[i].append( 0 )
return invG
Which do you feel is more idiomatic?
Here's a handy hack - if you had a list of ints, which is a subset of the first n non-negative ints (0,1,2,.. n-1), how do you build yourself a list of ones and zeros such that the entry is a one when the ordinal number representing that entry is in the list of ints? ( [0,1,4], 5 ) --> [ 1, 1, 0 , 0 , 1] )
sieve = list(map(lambda x: 1 if x in cover else 0, range(len(graph))))
Like?
def inverse_graph(graph):
invG = []
for i,row in enumerate(graph) :
sieve = [1] * len( row )
sieve[i] = 0
invG.append( [xor(x[0], x[1]) for x in zip(row, sieve)] )
return invG
Instructor :
def inverse_graph(graph):
n = len( graph )
invG = []
for i in range( n ) :
invG.append( [] )
for j in range( n ) :
if i != j :
invG[i].append( 1 - graph[i][j] )
else :
invG[i].append( 0 )
return invG
Which do you feel is more idiomatic?
Here's a handy hack - if you had a list of ints, which is a subset of the first n non-negative ints (0,1,2,.. n-1), how do you build yourself a list of ones and zeros such that the entry is a one when the ordinal number representing that entry is in the list of ints? ( [0,1,4], 5 ) --> [ 1, 1, 0 , 0 , 1] )
sieve = list(map(lambda x: 1 if x in cover else 0, range(len(graph))))
Like?
Wednesday, May 16, 2018
Visual Studio Code Setup for Simple Build In Local Directory No Frills
Assuming :
You're okay with a.out, then :
{
"tasks": [
{
"type": "shell",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Generated task by Debugger"
}
],
"version": "2.0.0"
}
Tuesday, May 15, 2018
Python : Access Your Own Docstring
You have a function in a file and you want to view the docstring. You're in the python shell. What's the quickest way?
> Cheat - assume you're in the same directory :)
>>> from basename import * # file is basename.py
>>> func_name.__doc__ # will show it to you
Saturday, May 12, 2018
Python Assign Characters of a String to a List
Or, convert a string to a list :
type( a )
<class 'str'>
b = list( a )
# is all you need :)
type( a )
<class 'str'>
b = list( a )
# is all you need :)
Monday, May 07, 2018
Thursday, May 03, 2018
Python Sorting Two Lists Together
I don't get it either, but it works..
https://codereview.stackexchange.com/questions/138702/sorting-two-lists
s = sorted( zip( a, b) ) # a is master, b is slave
a,b = map( list, zip(*s) )
# and that's it! The genius of Guido Van Rossum
https://codereview.stackexchange.com/questions/138702/sorting-two-lists
s = sorted( zip( a, b) ) # a is master, b is slave
a,b = map( list, zip(*s) )
# and that's it! The genius of Guido Van Rossum
Highlight Selected Cell or Row in Excel - Aus Tom Urtis
It's literally this easy, if you know how.
To highlight the selected cell (automatically that is.. not doing something each time you select something :) :
Get to the Visual Basic editor (might have to enable the Developer tab and then hit View Code -- google this for the way, it's not here)
Then, paste (thanks TU) :
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Application.ScreenUpdating = False ' Clear the color of all the cells Cells.Interior.ColorIndex = 0 ' Highlight the active cell Target.Interior.ColorIndex = 8 Application.ScreenUpdating = True End Sub
That's it. You just paste this and File > Close and Return to Excel and MAGIC happens! Sufficiently advanced technology!!
And for highlighting the entire row AND column (tweak the code as necessary if you want only..) :
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.Cells.Count > 1 Then Exit Sub Application.ScreenUpdating = False ' Clear the color of all the cells Cells.Interior.ColorIndex = 0 With Target ' Highlight the entire row and column that contain the active cell .EntireRow.Interior.ColorIndex = 8 .EntireColumn.Interior.ColorIndex = 8 End With Application.ScreenUpdating = True End Sub
And how do you know what number to use for the ColorIndex?
http://dmcritchie.mvps.org/excel/colors.htm
If you want his book :
To highlight the selected cell (automatically that is.. not doing something each time you select something :) :
Get to the Visual Basic editor (might have to enable the Developer tab and then hit View Code -- google this for the way, it's not here)
Then, paste (thanks TU) :
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Application.ScreenUpdating = False ' Clear the color of all the cells Cells.Interior.ColorIndex = 0 ' Highlight the active cell Target.Interior.ColorIndex = 8 Application.ScreenUpdating = True End Sub
That's it. You just paste this and File > Close and Return to Excel and MAGIC happens! Sufficiently advanced technology!!
And for highlighting the entire row AND column (tweak the code as necessary if you want only..) :
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.Cells.Count > 1 Then Exit Sub Application.ScreenUpdating = False ' Clear the color of all the cells Cells.Interior.ColorIndex = 0 With Target ' Highlight the entire row and column that contain the active cell .EntireRow.Interior.ColorIndex = 8 .EntireColumn.Interior.ColorIndex = 8 End With Application.ScreenUpdating = True End Sub
And how do you know what number to use for the ColorIndex?
http://dmcritchie.mvps.org/excel/colors.htm
If you want his book :
Subscribe to:
Posts (Atom)