![]() |
|
Re: ** Python Support Thread **
I'm looking for a good way of sorting an array of dicts by two dict elements, and nothing i've written so far looks maintainable or reusable for similar data.
Code:
leaguegames = LeagueGame.objects.filter(Q(division__exact = division)){ "played":2, "won": 2, "lost":0, "For": 32, "against": 28, "percent":53.3 , "points":4 } I'd like to sort by "points" then "percent". Ideas? |
Re: ** Python Support Thread **
Quote:
I'm thinking it might be down to one of the following: 1. It's assuming tied pots are tied n ways instead of being tied between 2 and n ways. (This would then give slightly too little equity to each.) 2. It's a rounding error internal to the library. Either way my currently solution is just to normalise the equity, so if I get told equity is 499 497 then I just convert multiply each equity by 1000/996. It worries me though that either the library has this bug, or I'm not fully understanding the results it is returning. |
Quote:
|
Re: ** Python Support Thread **
ups. Obviously _dict should be ladder in my post above.
sorted(ladder, lambda x: (x["points"], x["percent"])) |
Re: ** Python Support Thread **
Hey guys,
I've written the below code that simulates randomly drawing a card from a 52-card deck (with replacement) and tells the user how many cards and face cards he was dealt in order to get a total of four aces. Here's the thing: I want to modify my code so that it stops only once I've drawn the same card twice in a row. I know I have to use a while loop but it gets messy, b/c I *think* I'll have to introduce other variables like rank2 and suit2. But I still want it to simulate drawing one card each time. Sorry for the rambly post, hopefully it made sense. Thanks in advance for the help! import random number_card = 1 number_aces = 0 number_face_cards = 0 while number_aces < 4: rank = random.randint(1,13) if rank in range(2,10): print("Card #" + str(number_card) + ": " + str(rank) + " of ", end="") elif rank == 1: print("Card #" + str(number_card) + ": Ace of ", end="") number_aces = number_aces + 1 elif rank == 11: print("Card #" + str(number_card) + ": Jack of " , end="") elif rank == 12: print("Card #" + str(number_card) + ": Queen of " , end="") else: print("Card #" + str(number_card) + ": King of " , end="") if rank in range(11,13): number_face_cards = number_face_cards + 1 suit = random.randint(1,4) if suit == 1: print("Clubs") elif suit == 2: print("Diamonds") elif suit == 3: print("Hearts") else: print("Spades") number_card = number_card + 1 print("") print("It took " + str(number_card) + " cards to get 4 Aces!") print("") print("In total, you were dealt " + str(number_face_cards) + " face cards!") |
Re: ** Python Support Thread **
Use code tags for large samples
|
Re: ** Python Support Thread **
How do I do that?
|
Re: ** Python Support Thread **
Code:
def foo(bar): |
Quote:
Code:
# inside while |
Re: ** Python Support Thread **
Gotcha, thanks for the help.
Quick question about ranges. When I write: if x in range(2,10) This means that the condition is met if x = any integer between 2 and 9 inclusive (so it doesn't include 10), correct? Whereas: if x in (2,10) includes 10, similar to random.randint(2,10) includes 10, yeah? |
Re: ** Python Support Thread **
Quote:
I can never remember if range is inclusive or exclusive, type range(2,10) into the python shell and check out the list that it produces. "if x in (2,10)" will only be true if x is 2 or 10. This is because 2 and 10 are the only elements in this tuple. |
Re: ** Python Support Thread **
range is left-inclusive, right-exclusive
It sounded dumb to me at first until I had it explained to me that this means that you can write something like range(x, x+k) and it will contain k elements. e.g. "range(1, 1+8)" is the integers 1 to 8. |
Re: ** Python Support Thread **
I like that explaination a lot, thx.
|
Re: ** Python Support Thread **
Ahh v. helpful. Thanks for the clarification guys.
|
Re: ** Python Support Thread **
Thanks again for the help guys. I think I've got this one most of the way there; I'm trying to get it so that, if you roll doubles, your turn's over and if not, the program asks you if you want to roll again.
Issue is that, when someone inputs "1", my program doesn't roll again...I thought it would go to the beginning and check if p == 1 and, given that it does, roll again... Where am I going wrong? Code:
import random |
Re: ** Python Support Thread **
Firstly, try raw_input(), secondly try forcing conversion to int, so actually use int(raw_input())
|
Re: ** Python Support Thread **
Quote:
You might also find this interesting: http://www.cs.utexas.edu/users/EWD/t...xx/EWD831.html |
Re: ** Python Support Thread **
Quote:
Pretty close to what I ended up with: Code:
ladder = sorted(ladder, key=lambda d: (-d['points'], -d['percent'])) |
Re: ** Python Support Thread **
Hey guys. So sometimes I find that I want to use a function in a loop where it'll act on the arg only if it exists. Like for example do a loop on a list and keep going only until it finds out that the next index of that list doesn't exist. Or maybe the function operates on a list of various class objects, of which only some of those objects share a certain method. So if that object has that attribute, I want the function to be carried out, but if not, do nothing and move on to the next item in the list.
And the easiest way I can think of to do this is to use a try except. But I'm not sure if that's considered bad form, or if they're only supposed to be used to catch errors. |
Re: ** Python Support Thread **
A lot of people in the python use try/catch for the type of situation you're describing. Personally I'm not a huge fan of it. I'd rather do something like:
Code:
for obj in objects:Code:
for obj in objects: |
Re: ** Python Support Thread **
+1 neko's answer
two other techniques that seem applicable: - design your class structure to get rid of this problem: Code:
class Parent(object):Code:
attribute = getattr(klass, 'attribute') |
Re: ** Python Support Thread **
Trying to find the smallest number divisible by each of the numbers between 1 and 20. Here's my attempt:
x = 1 y = 1 while y < 21: if x % y == 0: y = y + 1 else: x = x + 1 y = 1 print(str(x)) Issue is, when I run it, Python doesn't print anything...not sure why. Ideas? |
Re: ** Python Support Thread **
use the [ code ] tags.
try putting a print inside your while loop so you can see what x and y are doing. |
Re: ** Python Support Thread **
Quote:
|
Re: ** Python Support Thread **
Should have used the code tags but have sorted out the issue, thanks guys.
On a dif not, why do the vowels below need quotes around them. I would have done it without... Code:
secret = input("Type something amusing: ")Thanks again, you guys have been rly helpful! -Mariogs |
| All times are GMT -4. The time now is 06:18 PM. |
|
Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Copyright © 2008-2020, Two Plus Two Interactive