![]() |
|
Re: ** Python Support Thread **
going back to the "don't mess with an iterable while you are iterating through it",
it is perfectly OK (read: safe and not alarming) to do this, right? Code:
for key, value in somedict.iteritems():Code:
for key, value in somedict.iteritems(): |
Re: ** Python Support Thread **
At first, I was doing something similar to:
Code:
Not sure how you program without the else blocks. Are you saying you don't specify else or you really don't use it at all? I'm sort of in the habit of using indenting to execute the else opposed to actually writing else. But there are times it is handy. The next two projects are 'brute force algorithms,' which require some else blocks to execute I think, if for nothing else but to return Null values to the main functions. But, eh, I haven't put any effort into much more than a rudimentary start. Hopefully, this one won't take a week to do. |
Re: ** Python Support Thread **
Spoilers won't hide the answer from bots by the way. It doesn't do a live call toget the spoiler contents. It just hides or shows a div that's already there.
|
Re: ** Python Support Thread **
Quote:
|
Re: ** Python Support Thread **
Yeah that should be fine.
But you don't want to be adding or deleting. That's typically going to be undefined or weird. |
Re: ** Python Support Thread **
This code does exactly what I want it to do, but I wonder if there is a simpler solution to this problem. I'm also wondering if there is a different solution to this problem that I didn't catch.
The idea is to find all the values of n via n = 6a + 9b + 20c. I created an ascending list with no repeating values. The program runs pretty slow, but I guess that isn't too surprising since I am running 3 simultaneous loops, checking vs an if, creating a list, sorting the list, then minimizing the list. Considering the problem set, I could use a smaller value than 150. I ran it with 50 as well, but it is still slow. Code:
ldo, math ftl? Code:
|
Re: ** Python Support Thread **
why are you sorting and uniq'ing during every iteration?
|
Re: ** Python Support Thread **
Quote:
[0, 6, 9, 12, 15, 18, 20, 21, 24, 26, 27, 29, 30, 32, 33, 35, 36, 38, 39, 40, 41, 42, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69] The plan (for now) is to check all numbers in the list against n, n + 1,... n+5 (which probably creates another for loop somewhere else), then print out 37. |
Re: ** Python Support Thread **
that's all gobbledegook to me but even if you really, really need to call
Code:
nList = list(set(nList)) |
Re: ** Python Support Thread **
here is the problem set that explains what I am doing.
http://ocw.mit.edu/courses/electrica...ents/pset2.pdf |
Re: ** Python Support Thread **
Quote:
But, yes, this is good. Thanks. |
Re: ** Python Support Thread **
Quote:
I solved this one pretty easily just brute-forcing the possible combinations and using if/else statements to set the appropriate flags and keep track of a counting variable until I found 6 valid combos in a row. The code is in a spoiler below in case you don't want to see before you solve it for yourself : Spoiler:
|
Re: ** Python Support Thread **
I have a solution, though it probably isn't as correct as yours. This is something I threw together over about 4 hours (which is extremely fast for me). I got a little to married to the list idea, but I'll work on a different solution at a later date.
Spoiler:
|
Re: ** Python Support Thread **
What's this for:
Quote:
In fact: I think you can just leave them as sets and then call max(yourSet) to get the largest number. Edit: The reason I'm pointing that out is that learning to use the right data structure and avoiding lots of unnecessary conversions between data structures is pretty important. Much more important than trying to get code down to a small size or to figure out the very best algorithm to use. |
Re: ** Python Support Thread **
Been trying to learn python for a few months on and off in the free time that I have (very little). This week it finally paid off as I could write a script that eases some of the work I was doing.
Even though it is probably a horrible piece of code from a programmers' view, I was still kinda proud. It's 8 times a similar thing after each other, but each one is dependent on the step before it, so I split it into 8 codeblocks. Is there a way to do that easier? Rough example: I have x, out of that comes x1 x2 and x3, then I do roughly the same with those and get y11 y12 y13, y21 y22 and y23, and y31/y32/y33. Is that possible to do in one codeblock without using so many for and if blocks that I tab out of the right side of my screen? Attached the script btw, some variable names might be weird but they make sense if you know what the subject is about. It basically gets the relevant information out of (in this case) 8 documents and pastes it exactly in the format I want in a new file. Also, first time dabbling in regexes so it took me a while to figure out all those to get exactly what I wanted. Spoiler:
|
Re: ** Python Support Thread **
Quote:
Slick Strawberry: The reason we were using spoilers is so we don't show off the answers to the homework we are doing. |
Re: ** Python Support Thread **
I'm using a spoiler because a spoiler gets a scrollbar if the script is too long, don't want my posts to be extremely long and I don't really know a good other way to show off my code.
|
Re: ** Python Support Thread **
Use the [ code ] tag.
|
Re: ** Python Support Thread **
Didn't know the code tag also added scrollbars to the code, now I have a code tag in a spoiler tag...
|
Re: ** Python Support Thread **
You can also use the php tag to get some syntax highlighting. It isn't perfect because it as it is geared towards php, but it does a decent job with any c-style language.
|
Re: ** Python Support Thread **
What's the best way for iterating over a list while filtering out values?
For example assume I have the following: Code:
l = [1,None,3]Code:
l = [1,None,3]Any better ideas? |
Re: ** Python Support Thread **
first one looks fine to me.
You can also use Code:
for x in filter(None, l): |
Re: ** Python Support Thread **
in Python 3 I understand you could use
Code:
[print(x) for x i l if x] |
Re: ** Python Support Thread **
I was wondering how you'd do it without the use of filter (since some people are against that). :)
Your second one won't work for me since my print statement is just standing in for about half a dozen statements and I hate putting a lot of logic inside of a list comprehension. |
Re: ** Python Support Thread **
May seem totally out of place, but the only thing I am against is using 'l' and '1' in the same program. People like to use various fonts and some of those fonts aren't going to show the difference between 'l' and '1'. Even with a decent font, it is still easy to mistaken the stuff.
|
| 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