-
-
Save dyspop/484f09eed4cee61f5d7901db0a621fb0 to your computer and use it in GitHub Desktop.
records = {('wendy', 'sanchez'): ['wunderkind','extraordinaire'], ('dan', 'black'): ['hacker','wannabe'], ('tim', 'black'): ['mad', 'genius', 'dontchaknow'], ('dan', 'garfield'): ['porg', 'rammer', 'snake charmer']} | |
sorted_last = ['black', 'black', 'garfield', 'sanchez'] | |
itered_records = [] | |
for last_name in sorted_last: | |
for key in records: | |
if key[1] == last_name: | |
if str([key + ('', records[key])]) not in itered_records: | |
itered_records.append(str([key + ('', records[key])])) | |
print key, records[key]` |
to get the sorted list of keys from records, what you have as sorted_last, it should be generated with code. hard coding data like that would probably be rejected as a solution because it's impossible to do for data sets of non-trivial size.
This is a microcosm of the real problem, so I just made a dummy set to illustrate the data
if you can't even say in English what the condition is checking, then you wrote it wrong.
if str([key + ('', records[key])]) not in itered_record]:
English: "If the stringified key value pair is not in the list of iterated records"
what is being checked in that conditional? checking if you've seen the record yet? I don't know why you need to make that check. what is the specification of the problem?
Yes, exactly that is what's being checked. The reason is because of the nested loop, the output has duplicates. We don't want duplicates.
it looks like its just some data munging
precisely.
Your solution works pretty well! 🙇
I knew it had bad code smell and we tried doing sorted and we tried list comprehension but just a lambda sorted didn't occur to us. Thanks!
some snippets and idioms that might help clean this up. it's not as bad you think it is though.
sorted_last
, it should be generated with code. hard coding data like that would probably be rejected as a solution because it's impossible to do for data sets of non-trivial size.if str([key + ('', records[key])]) not in itered_record]:
. i'm really not sure what you're trying to do there but here's the code smell: if you can't even say in English what the condition is checking, then you wrote it wrong. if you have to type cast something in order to get it to work, then you wrote it wrong. the general principle is that complicated tricky code is never correct under any circumstances. there are a vanishingly small number of good exceptions to that rule of thumb and nearly all of them are related to really twitchy optimization of low level code in languages that are close to the hardware. in a high level language like Python I've never once seen a good reason to except that rule of thumb.sorry if I missed the point of the exercise. I'm not entirely sure what you were supposed to be doing with this. it looks like its just some data munging though, massaging the contents of the records dictionary into a sorted list, right? if there's something more besides that let me know and I'll take another look.