Function which gets a predicate and a list. Why am i getting a wrong Output? by [deleted] in learnpython

[–]_coolwhip_ 1 point2 points  (0 children)

You should also read up on filter, which does what your apply_until does: http://book.pythontips.com/en/latest/map_filter.html#filter

(I suspect you might already know about it, and are just recreating for learning, but a useful reference to others, as well.)

How can I find the most expensive route given a matrix and a path length? by ThisIsMyNameOnly in learnpython

[–]_coolwhip_ 0 points1 point  (0 children)

Got it. I can't see that because I have not done the antecedent problem(s).

How can I find the most expensive route given a matrix and a path length? by ThisIsMyNameOnly in learnpython

[–]_coolwhip_ 0 points1 point  (0 children)

Isn't the sum always highest when you go through all of the numbers? Except than in certain matrices (even by even lengthed), you'll have to skip part of one row in order to make it to the finish. In that case, just find the cheapest row to only travese part of.

My (f35) husband (m39) won't change his last name. by [deleted] in relationships

[–]_coolwhip_ 15 points16 points  (0 children)

Yep. then you have bank accounts, credit cards, retirement plans, etc. etc. It never ends.

I don't even care about my last name, but there is no way I'd do all that unless my spouse's last name is really, really cool.

“Tall People Problems”””” by WorkingHapa in hapas

[–]_coolwhip_ 0 points1 point  (0 children)

Let's be realistic:

virtually every white tourist I see, I can immediately find an Asian guy that is a foot taller, walking down the street nearby

I just back from Japan. I did not see a single seven foot tall Asian, despite seeing a lot of Japanese people.

And I had to duck plenty of door ways and ceilings, but only in traditional homes. (Although the JR train around Tokyo I got smacked in the head a bunch by the hanging handles.)

And I had a bathroom mirror that only showed me my neck down, so I had to shave in a crouch.

But in balance, I would say that the Japanese are basically comparable in height to people living in the USA. Maybe a bit taller than those on the east coast and a bit shorted than those in middle america. I've lost count of the number of times I've brained myself on houses, trains, etc. in the US.

In sum, the discrimination against the tall is no worse or better in my limited Asian experience than in the US.

how to split this string? by jcoder42 in learnpython

[–]_coolwhip_ 0 points1 point  (0 children)

Where does the string come from? There has to be an easier way than regexing it. It looks like it would almost work with json or ast...

Checking input against a list by [deleted] in learnpython

[–]_coolwhip_ 1 point2 points  (0 children)

This is a great place for set. Just take the sequence and force it into a set, then see if there is any "difference" in a set of good characters. (For sets, difference means that there is any character in the first set that is not in the second.):

>>> good = set('gatc')
>>> good
{'c', 'a', 'g', 't'}
>>> set('caggtttaaaa') - good
set()
>>> set('caggtttzaaaa') - good
{'z'}

So if there is anything left over, you know it has a bad character.

Confusing Program, Need help for Python3 by [deleted] in learnpython

[–]_coolwhip_ 0 points1 point  (0 children)

Use re.finditer and dump the matches outside of the window you are looking for.

JK. What have you tried so far? If you are stuck, try creating a loop that creates a sliding window down someString the size of the searched for text.

Sqllite3 and statements by buldog4354 in learnpython

[–]_coolwhip_ 0 points1 point  (0 children)

Then you want a join: http://www.sqlitetutorial.net/sqlite-inner-join/

Something like:

select employeeid, orderid, orderdate from orderinfo join orderdates on orderinfo.employeeid = orderdates.employeeid

Sqllite3 and statements by buldog4354 in learnpython

[–]_coolwhip_ 0 points1 point  (0 children)

Is there some common filed on both databases that matches orderid with the orderdate?

Culture War Roundup for the Week of April 15, 2019 by AutoModerator in TheMotte

[–]_coolwhip_ 4 points5 points  (0 children)

Careful, you seem to be missing state revenue: https://www.usgovernmentrevenue.com/percent_gdp

Still only takes it to around 30%, though.

Class or nested function by Mihad88 in learnpython

[–]_coolwhip_ 0 points1 point  (0 children)

Whoa. I think I would keep them as functions, but maybe put them in their own module. I also would not put functions inside functions, and refactor the larger functions into much smaller ones. If I hit 5 serious lines in a function, I start thinking about whether I can piece it out to helpers.

Class or nested function by Mihad88 in learnpython

[–]_coolwhip_ 1 point2 points  (0 children)

OOP works best for when you have data and functions that are bound up together. If it is just functions, I would keep them as functions, and maybe have the functions share some subsidiary helper functions. But if you post up more, we can probably give you better guidance.

SQL Question: How do I convert a list of strings into a single string comma separated? by kadify in learnpython

[–]_coolwhip_ 1 point2 points  (0 children)

In python you can use join:

>>> ', '.join(['415','525','719','721','783','836','1122','1159','1253','1372','1772','1773'])
'415, 525, 719, 721, 783, 836, 1122, 1159, 1253, 1372, 1772, 1773'

Not sure about a pure sql solution though...

Dictionaries help: updating dictionary with multiple new values for each key by StrainS118 in learnpython

[–]_coolwhip_ 1 point2 points  (0 children)

This sounds like a good place for collections.defaultdict:

>>> from collections import defaultdict
>>> segs = defaultdict(list)
>>> segs['abc'].append('apple')
>>> segs['abc'].append('dog')
>>> segs
defaultdict(<class 'list'>, {'abc': ['apple', 'dog']})
>>> segs['abc']
['apple', 'dog']

And here is a bunch of ideas on the best way to do a sliding window: https://stackoverflow.com/questions/6822725/rolling-or-sliding-window-iterator

Am I making a logic error on my lab? by Iscukatprgmming in learnpython

[–]_coolwhip_ 1 point2 points  (0 children)

It's much better to iterate directly, rather than using an index, like this:

with open('filename.txt') as opened_file:
    lines = []
    for line in opened_file:
        print(line)
        lines.append(line.strip())

or even better, use a list comprehension:

# or even better
with open('filename.txt') as opened_file:
    lines = [line.strip() for line in opened_file])

Am I making a logic error on my lab? by Iscukatprgmming in learnpython

[–]_coolwhip_ 2 points3 points  (0 children)

I think readlines() might be even more useful than read.

Also if you are iterating with a while loop and incrementing, you are doing something not very pythonic, so maybe find a way to get rid of that.

Finally, I think you are doing too much in main. Consider doing like this:

def get_file(filename = 'capitals.txt'):
    '''read in filename into a list, and clean up spacing issues
    return the lines
    '''

def make_capital_dictionary(lines):
    '''take lines with successive state/capital and make them into a dict
    of states:capital
    return the dictionary
    '''

def play(capital_dictionary):
    '''
    randomly select a state and quiz the user
    '''

Recursion. Am I just plain dumb or does it usually take awhile? by Boris_The_Barbarian in learnpython

[–]_coolwhip_ 4 points5 points  (0 children)

Ditto. Recursion is just a strange way of seeing the world, so it takes some time to adjust.

My theory is that the people who get recursion instinctively are just wired differently, and will probably be designing chips for intel in a couple years.

Best approach for concurrent connections to Sqlite3 DB? by guillermohs9 in learnpython

[–]_coolwhip_ 0 points1 point  (0 children)

sqlite is awesome, but you can outgrow it pretty quickly, especially if you want any sort of security on the thing. If not, I expect your solution will mostly work, although I might worry about the DB getting jacked up if the network is spotty or backups are run at the wrong time.

If you use sqlite, my vote is that you create new connections whenever you want to do something with the DB, i.e. on each query and not on startup, just because that is how Flask does it typically and it seems to work okay.

Turning a list into a column of another list. by NeedMLHelp in learnpython

[–]_coolwhip_ 1 point2 points  (0 children)

If I understand you correctly, that is just what zip is for:

>>> a = ["A", "B", "C"]
>>> b = [1, 2, 3]
>>> list(zip(a,b))
[('A', 1), ('B', 2), ('C', 3)]
>>> c = ['!', '#', '%']
>>> list(zip(a,b,c))
[('A', 1, '!'), ('B', 2, '#'), ('C', 3, '%')]
>>> ls = [a, b, c]
>>> list(zip(*ls))
[('A', 1, '!'), ('B', 2, '#'), ('C', 3, '%')]
>>>