|
NtotheTC posted:Does anyone know if pipenv can replicate the behaviour of vex? (https://pypi.python.org/pypi/vex) I've always found vex to be lightyears ahead of virtualenvwrapper for development (activating and deactivating virtualenvs manually is a horrible pattern) but I've never used pipenv and people seem very happy with it pipenv run python myscript.py
|
# ? Jan 22, 2018 04:16 |
|
|
# ? Jun 5, 2024 05:56 |
|
The code cops are busting me with an "invalid syntax" error for the crime of trying to combine comparison operations in an if statement (I think):code:
Maybe the problem is trying to combine arithmetic operations on a single line? If so, I guess I could do it like this: code:
|
# ? Jan 22, 2018 04:36 |
|
You can't be implicit in your comparison You need something like code:
code:
|
# ? Jan 22, 2018 04:44 |
|
Linear Zoetrope posted:However, your second version is preferred. Python (and most any programming language) will only evaluate the conditionals in order from first to last, so if it's testing if pr <= 2000 it's already executed and proven that it's not <= 999 I kind of suspected that was the case. Thanks!
|
# ? Jan 22, 2018 04:54 |
|
Seventh Arrow posted:The code cops are busting me with an "invalid syntax" error for the crime of trying to combine comparison operations in an if statement (I think): I’m not sure how “correct” it is but I see and use this all the time. If pr <= 999 then the condition is met and nothing else will evaluate. If pr was not <= 999 then it moves onto the next condition, which is now implicitly “999 < pr <= 2000.
|
# ? Jan 22, 2018 14:18 |
|
Boris Galerkin posted:I’m not sure how “correct” it is but I see and use this all the time. If pr <= 999 then the condition is met and nothing else will evaluate. If pr was not <= 999 then it moves onto the next condition, which is now implicitly “999 < pr <= 2000. Yes, they have a section devoted to this in Python Crash Course but I lent it to a friend; still, I had a feeling this would be the case but I wasn't sure. The next trick will be to see if it fits with the rest of the code.
|
# ? Jan 22, 2018 14:23 |
|
Ok I'm back and it seems like my if/elif/else loop is not quite getting along with the rest of the code. I started off trying to pull the latitude and longitude in a spreadsheet and put them as markers on a folium map. With some help from you guys, it worked with the following: code:
code:
code:
df.dtypes Post_id float64 Price float64 Bedroom float64 Bathroom float64 Sqft float64 Latitude float64 Longitude float64 Description object Latlng object Postal_code object dtype: object
|
# ? Jan 22, 2018 23:45 |
|
You're getting that error in particular, because [df['Price']] is a list containing one item, the pandas column. You want just df['Price']. However, that won't actually get the behavior you want, because just as you can't compare a list and an int, you can't compare a dataframe column and an int (well technically you can, but you won't get a True or False, you'll get a columns of True/False, which wont' work in that context). Notice that you're only setting this cl variable once, whereas you want it to take a different value for every row. Your options are either: 1. You could set the color inside the loop by putting all that if/elif stuff inside the loop, therefore checking for a new color every time. You will need to change a couple other things which I leave as an excercise. 2. You could set all the colors ahead of time with something like pd.cut(df['Price'], [0, 1000, 2000, 3000], labels=['green', 'yellow', 'orange', 'red']).
|
# ? Jan 23, 2018 00:22 |
|
The if/elif stuff isn't in the loop already? I thought that the if/elif stuff was the loop
|
# ? Jan 23, 2018 00:29 |
|
The loop is the part that starts with for and ends when the indentation returns to the previous level.
|
# ? Jan 23, 2018 00:30 |
|
Ah, I see what you mean. Thank you. I will take a swing at #1 and see how it goes.
|
# ? Jan 23, 2018 00:37 |
|
A loop... loops! It can go around multiple times, until some condition is met The if/elif stuff is just a conditional - you go through it once, and follow the first branch that matches, then you pop out the other side You can put a conditional inside a loop of course, which is pretty common - iterating over several things, doing something different based on the current situation each time around baka kaba fucked around with this message at 02:08 on Jan 23, 2018 |
# ? Jan 23, 2018 02:06 |
|
Ok I think I'm almost there. I've managed to twist its arm enough that it will post the map marker with a colour...once. So I think I'm in the ballpark, but I get a map with only one marker. Here's what I have so far:code:
A) The "dropna" and "iterrows" are working their magic with the "position" variable so that when the process gets around to "pr", almost all of the rows have been dropped. If this is true, then I don't know enough about python to properly address it. B) It could be something folium-specific, since jupyter displays the following: <folium.map.Marker at 0x7efe9fd758d0> Also, the "class color" thing is something I got off of Stack Overflow. If I don't put it in there, it says that "color is not defined."
|
# ? Jan 24, 2018 01:18 |
|
Seventh Arrow posted:Ok I think I'm almost there. I've managed to twist its arm enough that it will post the map marker with a colour...once. So I think I'm in the ballpark, but I get a map with only one marker. Here's what I have so far: You need to place the line folium.Marker(position, icon=folium.Icon(color=cl)).add_to(map_1) inside the for loop (so basically tab it over once). Right now the only thing being added to the map is the last location. EDIT: Also it looks like you chose to use the iterrows() solution that I proposed, but as pointed out that is probably the slowest way to do things, and you should probably use one of the other suggested methods. Jose Cuervo fucked around with this message at 02:20 on Jan 24, 2018 |
# ? Jan 24, 2018 02:17 |
|
Your line: code:
What you are doing is like this: code:
code:
|
# ? Jan 24, 2018 02:19 |
|
I remember telling myself to put that line back, too. Woops! Thanks for the help.
|
# ? Jan 24, 2018 02:45 |
|
Here is why it says “color” is undefined without that class:code:
You could simply remove this line (and the “class color” segment you added above) and everything would be fine again. If the reason you have this line here is to pre declare “cl” as a variable then that’s not needed in Python. Alternatively, you could change that line to code:
|
# ? Jan 24, 2018 06:26 |
|
Ok that does indeed make sense, thanks. But I think classes or their objects do need to be defined I think, right? That what "def __init__" is for? Actually, I think I'll just look up the python documentation and review some of this stuff since I'm not gonna get my book back any time soon.
|
# ? Jan 24, 2018 07:57 |
|
Seventh Arrow posted:Ok that does indeed make sense, thanks. But I think classes or their objects do need to be defined I think, right? That what "def __init__" is for? You don't have to pre-define cl, because it's definitely being defined in your if/elif/else block. You're setting cl to a string (eg "red", "blue", etc are all instances of the string class) in those blocks of code. This effectively means that the "cl = color" line does nothing and can just be deleted. An IDE like pycharm would point this out for you as well. If you want an instance of a color class, then you would indeed need to define a color class first. But it doesn't sound like that's what you actually want; the function you're accessing just wants a string with the name of the requested color
|
# ? Jan 24, 2018 10:23 |
|
If it helps, variables in python are basically just keys in a dictionary, so you can just go thing = whatever at any time and it just stores that key/value pair. So there's no need to pre-declare that variable like you would in other languages where you want to reuse the same reference - you're just overwriting the same entry in the dictionary
|
# ? Jan 24, 2018 11:38 |
|
How do I remove certain strings of text (e.g. any text in brackets) from a list of dictionaries? To explain further, I have a list that contains dictionaries of song lyrics (each dictionary has two items: the song title and the lyrics). However, some of the lyrics contain various junk I want to get rid of like: "[Chorus]", "[Verse 1]", and "[Talking]". So I need something that goes through all the lyrics and removes anything that's in brackets basically. Sorry if this is an easy/stupid question--I'm very new to Python. I'm using Python 3 if that matters.
|
# ? Jan 25, 2018 18:11 |
|
I'm not sure where in the list of dicts is your quoted strings are (keys? values?), but you could apply something like this to each string, in a loop or comprehension:Python code:
Dominoes fucked around with this message at 18:35 on Jan 25, 2018 |
# ? Jan 25, 2018 18:26 |
|
Here's the documentation for the re module itself. And a how to that's a bit more digestible.
|
# ? Jan 25, 2018 18:30 |
|
Sorry I can be more specific and post a concrete example. Here's an example of one of the dictionaries in the list:quote:{'title': 'Pale Green Things', 'lyrics': "\n\n[Verse 1]\nGot up before dawn\nWent down to the racetrack\nRiding with the windows down\nShortly after your first heart attack\nYou parked behind the paddock\nCracking asphalt underfoot\nComing up through the cracks\n\n[Chorus]\nPale green things\nPale green things\n\n[Verse 2]\nYou watched the horses run their workouts\nYou held your stopwatch in your left hand\nAnd a Racing Form beneath your arm\nCasting your gaze way out to no man's land\nSometimes I'll meet you out there\nLonely and frightened\nFlicking my tongue out at the wet leaves\n\n[Chorus]\nPale green things\nPale green things\n\n[Verse 3]\nMy sister called at 3 AM\nJust last December\nShe told me how you'd died at last\nAt last\nThat morning at the racetrack\nWas one thing that I remembered\nI turned it over in my mind\nLike a living Chinese finger trap\nSeaweed in Indiana sawgrass\n\n[Chorus]\nPale green things\nPale green things\n\n"} I want to remove all the bracketed text from the lyrics so that it in the end it looks like this: quote:{'title': 'Pale Green Things', 'lyrics': "\n\n\nGot up before dawn\nWent down to the racetrack\nRiding with the windows down\nShortly after your first heart attack\nYou parked behind the paddock\nCracking asphalt underfoot\nComing up through the cracks\n\n\nPale green things\nPale green things\n\n\nYou watched the horses run their workouts\nYou held your stopwatch in your left hand\nAnd a Racing Form beneath your arm\nCasting your gaze way out to no man's land\nSometimes I'll meet you out there\nLonely and frightened\nFlicking my tongue out at the wet leaves\n\n\nPale green things\nPale green things\n\n\nMy sister called at 3 AM\nJust last December\nShe told me how you'd died at last\nAt last\nThat morning at the racetrack\nWas one thing that I remembered\nI turned it over in my mind\nLike a living Chinese finger trap\nSeaweed in Indiana sawgrass\n\n\nPale green things\nPale green things\n\n"}
|
# ? Jan 25, 2018 18:37 |
|
Python code:
|
# ? Jan 25, 2018 18:53 |
|
That seems to have worked. Thank you!
|
# ? Jan 25, 2018 18:59 |
|
Pretty sure this would also work:Python code:
Dr Subterfuge fucked around with this message at 19:31 on Jan 25, 2018 |
# ? Jan 25, 2018 19:22 |
|
Hey, I'm trying to figure out how to end up with a csv file to do some basic data analysis. I've been building off some code that I shamelessly stole from someone else, which is scraping data from a prediction market and turning each security into a panda series with the date and price for the past year. I would like to take all the the securities on the market (about 1000), and have each security represented by a column in a csv file. Python code:
code:
|
# ? Jan 25, 2018 19:32 |
|
I have an interview with a Python shop coming up and my background is in Ruby and JavaScript. The company told me to be prepared to demonstrate understanding of "lists, maps, and queues" in the interview and I want to sanity check that I have the right terminology. Am I correct in thinking that in Python, a list is the data structure that looks like [foo, bar, baz], a map is the data structure that looks like {"foo": "bar", "baz":"qux"} and a queue is implemented with collections.deque?
|
# ? Jan 25, 2018 20:02 |
|
map is a function that applies another function to all items of an iterable (such as a list).
|
# ? Jan 25, 2018 20:06 |
|
Dominoes posted:map is a function that applies another function to all items of an iterable (such as a list). Okay, thanks. Is there anything more to it than meets the eye? It looks like I just do something like code:
|
# ? Jan 25, 2018 20:18 |
|
fantastic in plastic posted:I have an interview with a Python shop coming up and my background is in Ruby and JavaScript. The company told me to be prepared to demonstrate understanding of "lists, maps, and queues" in the interview and I want to sanity check that I have the right terminology. Am I correct in thinking that in Python, a list is the data structure that looks like [foo, bar, baz], a map is the data structure that looks like {"foo": "bar", "baz":"qux"} and a queue is implemented with collections.deque? Worth noting that they may not mean "be comfortable with lists maps and queues" in terms of their python implementations. They may just want you to write the code for a linked list or a queue or whatever.
|
# ? Jan 25, 2018 20:26 |
|
That's true, too. In the phone interview I did with an engineer there, we spoke a lot about queues, so I'm definitely expecting questions in that area. I just wanted to make sure I wasn't overthinking it.
|
# ? Jan 25, 2018 20:37 |
|
fantastic in plastic posted:Okay, thanks. Is there anything more to it than meets the eye? It looks like I just do something like It's not terribly uncommon for dictionaries to be referred to as "maps". It's not technically correct, but I'd lean towards them meaning dictionaries rather than the map function since they mentioned it in the context of two other data structures. And yes, a dictionary is one of these: Python code:
Python code:
|
# ? Jan 25, 2018 20:39 |
|
Baronash posted:Is there a good way to have the extra series added as columns, rather than additional rows? I've never actually used pandas, but I can tell you that you should be working with a DataFrame as your result. Series are one-dimensional arrays, which is why appending one to another only extends the length full_list.
|
# ? Jan 25, 2018 20:50 |
|
pd.concat(your list of series, axis=1) should do it.
|
# ? Jan 25, 2018 20:53 |
|
Here's a neat thing if you're one of the cool people like me who can mostly move most of their projects to keep up with the latest python versions. Coming in Python 3.7 are data classes! Data classes are kind of a hybrid between namedtuples and regular classes. They look like this: Python code:
Python code:
They're a lot more powerful than this, so click through the the above-linked PEP to read more about em. Being the typing fan that I am, I especially appreciate being able to restrict class vs instance variables, and also the ability to freeze instances.
|
# ? Jan 25, 2018 20:58 |
|
Love it. Mutable too; a reason that sometimes keeps me from using namedtuples.
|
# ? Jan 25, 2018 21:03 |
|
fantastic in plastic posted:Okay, thanks. Is there anything more to it than meets the eye? It looks like I just do something like
|
# ? Jan 25, 2018 21:05 |
|
|
# ? Jun 5, 2024 05:56 |
|
So I just started using python, and because I'm flailing around in the dark like an idiot, I could use some help. I'm trying to write a small script that runs some simulations and then saves the data out. Because I want to run many simulations with the same settings I would like to ideally give the functions the # of times to run and have it write out the data into a unique file. To do this, I want to make a filename string and tell it to write/append that file, however, when I run my script it's telling me that it can't take a tuple for the file name input and I don't understand why my data is a tuple to begin with. Everything else with the code works, and it's fine if I manually put in the filename into each open() call, but I want to automate as much as I can. Basically, I have this code:quote:filename=('HeteroFreq2.txt'), and I get this error quote:TypeError: expected str, bytes or os.PathLike object, not tuple What am I doing wrong with declaring the filename? Edit: I'm an idiot who missed that extra ',' at the end of the filename declaration. ZarathustraFollower fucked around with this message at 22:14 on Jan 25, 2018 |
# ? Jan 25, 2018 22:09 |