![]() |
|
Re: ** Python Support Thread **
Quote:
Code:
filter(lambda x: type(x) is int, list)Code:
[x for x in list if type(x) is int] |
Re: ** Python Support Thread **
The general "pythonic" way is to use list comprehension. Lots of people that have not been exposed to functional programming don't know about things like filter, map, etc.
|
Re: ** Python Support Thread **
What about:
Quote:
Quote:
I mean, obviously list comprehensions are more flexible and should be used in lots of places. I'm just wondering specifically using list comprehensions for filtering. |
Re: ** Python Support Thread **
Quote:
Edit: I feel like I'm coming across kind of belligerent here. I'm not, just curious about people's opinions. |
Re: ** Python Support Thread **
i don't think of python as being particularly functional. buh-dum-chhh.
no but really, most of the python code i encounter is very procedural or OO. don't see a lot of functional programming (although your filter example is obviously more common than, i dunno, passing function pointers around like that C->Python guy up there was trying to do :p). edit: and fwiw i think sorrow's version is quite easy to read. i actually think i'd have more trouble writing it! |
Re: ** Python Support Thread **
I used to program in Lisp so I am well used to map, reduce, filter, lambda, but I know they are considered "unpythonic" because they were originally scheduled to be removed in Python 3. (In the end, only reduce was removed).
jjshabado and sorrow -- none of your examples work as I believe they are expected to. They all fail to cast strings or discard zeros. Try them on a test case like Code:
li = [1, 2, "123", "ad", 4.5, "b2", "3fdf", [1, 2], 0, "0"] |
Re: ** Python Support Thread **
Peter Norvig's spelling corrector is a nice demonstration of how to use list comprehensions -- or in my case, a demonstration of how weak and feeble my own code is relative to Peter Norvig's - http://norvig.com/spell-correct.html.
Code:
import re, collections |
Re: ** Python Support Thread **
Quote:
Quote:
|
Re: ** Python Support Thread **
A working hybrid version is
Code:
filter(lambda x: x is not None, [convertable(el) for el in li]) |
Re: ** Python Support Thread **
btw since i ignited this firestorm, here's the code i ended up committing. not as elegant as some of the other examples, but it's been running in production all the while you guys were trying to figure out the how to make it elegantest :):
Code:
# Attempt to cast all entries to integers. This both insures proper numeric |
Re: ** Python Support Thread **
Quote:
|
Re: ** Python Support Thread **
Posted in the wrong thread at first, crossposting from LC :o
Kind of a noob question, but I'm pretty stumped here although it is probably simple. I am trying to combine certain lines of xml files I have into one ordered document, and trying to search through the file with regular expressions. Lines I'm searching for look like this or similar: 000101101000101101 -> 000101101110101101 However, I'm always looking for the second binary string as a given, and just want to also find the first string. I store the second string I'm looking for in a string, so something like: string='000101101000101101 -> 000101101110101101' want='001001101110101101' What I then try to do is re.search(r'([01]+).->.'(want), string) but that isn't working. The problem here is that I want to search for that specific binary string that I would like to store in the variable, ánd that I want to store both strings in a tuple group so I can call them later for other stuff. Any idea how I can fix this? |
Re: ** Python Support Thread **
1. don't parse well-known formats with regular expressions. use a library that knows how to parse it. there are about a million and a half of them for xml.
this will seem like a pain because you have to learn the api and debug it and blah blah, but if you're doing anything other than reeeeeeeeally trivial operations, you will save yourself time and heartache by just doing it right the first time. 2. if i understand correctly, i think you're looking for re.match(). |
Re: ** Python Support Thread **
I was actually looking for re.findall, but the problem I was having was searching for a string and the string content of a variable at the same time. Each one on its own is working for me but I can't seem to combine them into one search?
And they are quite trivial operations, but it might be better to learn a library anyway if I ever have to do anything like this in the future. Recommendations, or is any of the 1.5M good? |
Re: ** Python Support Thread **
Quote:
Code:
import re |
Re: ** Python Support Thread **
That sort of is what I was looking for, and helped me on the way to fix it. Since the second string I wanted to find was set beforehand, I had a problem putting it directly into the regex. However, if I use your method I can fix that. Here's how I did it in the end:
Code:
string='000101101000101101 -> 000101101110101101' |
Re: ** Python Support Thread **
Quote:
Code:
import re |
Re: ** Python Support Thread **
Quote:
Maybe the lack of reduce is why Python 3 is having such a slow adoption rate... ;) |
Re: ** Python Support Thread **
Hi guys,
I decided to use pypoker-eval-138.0 found here: http://download.gna.org/pokersource/sources/ (very bottom of the page) I'm new to python. I downloaded it, opened IDLE, loaded the pokereval.py code in the script window, and pressed F5, but in the python shell I get an error: Traceback (most recent call last): File "C:\Users\[my name]\Desktop\pypoker-eval-138.0\pypoker-eval-138.0\pokereval.py", line 29, in <module> _pokereval = __import__('_pokereval_' + sys.version[0] + '_' + sys.version[2]) ImportError: No module named _pokereval_3_2 Thanks. |
Re: ** Python Support Thread **
If you're new to python then you'd best learn python before trying to understand a big lump of python sources. :)
|
Re: ** Python Support Thread **
Quote:
That being said, pypokereval is notoriously difficult to get working on Windows. I believe gimick from the fpdb project was able to get it working once. Sorrow is another good person to ask about this. GL |
Re: ** Python Support Thread **
Interesting. So in other words I shouldn't try getting it to work?
Where else can I get a poker simulator/calculator? Can I tell VBA or Java to run PokerStove, then I can parse the text from pokerstove.txt? I know I can get my calculations off pro poker tools, but I want around 50,000 calculations just for one of my calculations. Is it reasonable to have my program go to their site 50,000 times at once? I'm not even sure how long this would take. |
Re: ** Python Support Thread **
Quote:
|
Re: ** Python Support Thread **
So....
I'm working on the mitocw courses. Using spoilers because I don't want to give out the answer to googleBots: Spoiler:
|
Re: ** Python Support Thread **
Since test is already a boolean, you don't need to compare it to True. Python, like many scripting languages, is liberal with will evaluate to true in an if clause. However, I tend to only let booleans auto evaluate for the sake of readability.
Also, loops can have an else block that executes when the loop runs to completion. I've never actually used it, but it is exactly what you are testing for. |
| All times are GMT -4. The time now is 03:50 PM. |
|
Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Copyright © 2008-2020, Two Plus Two Interactive