Posts: 5
Threads: 3
Joined: Aug 2019
Hello,
How can I simplify below two coding to make the code clean?
coding 1:
Case1,Case2,Case3,Case4,Case5=0,0,0,0,0,0
if rule = 1:
Case1+=1
elif rule = 2:
Case2+=1
elif rule = 3:
Case3+=1
elif rule = 4:
Case4+=1
elif rule = 5:
Case5+=1 coding 2:
if condition:
break
elif condition:
x,y,z = none,none,none
a,b,c = 1,2,3
else:
x,y,z = 10,11,12
a,b,c = 1,2,3 Thanks!
Posts: 5,150
Threads: 396
Joined: Sep 2016
the first code section can be converted to a dictionary
Recommended Tutorials:
Posts: 1,936
Threads: 8
Joined: Jun 2018
Sep-19-2019, 12:02 PM
(This post was last modified: Sep-19-2019, 12:02 PM by perfringo.)
It seems to be counting so defaultdict could be an option:
>>> from collections import defaultdict
>>> c = defaultdict(int)
>>> rules = [1, 2, 3, 4, 2, 3, 5, 4]
>>> for rule in rules:
... c[rule] += 1
...
>>> c
defaultdict(int, {1: 1, 2: 2, 3: 2, 4: 2, 5: 1})
>>> c[3]
2 Or Counter:
>>> from collections import Counter
>>> c = Counter(rules)
>>> c
Counter({1: 1, 2: 2, 3: 2, 4: 2, 5: 1})
>>> c[5] += 1
>>> c[5]
2 EDIT: regarding 2 code: elif will be never executed therefore redundant. First if catches all True conditions and there is nothing left for elif not to mention that somewhere must be loop from which to break out (not provided in code sample).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 5
Threads: 3
Joined: Aug 2019
(Sep-19-2019, 12:02 PM)perfringo Wrote: It seems to be counting so defaultdict could be an option:
>>> from collections import defaultdict
>>> c = defaultdict(int)
>>> rules = [1, 2, 3, 4, 2, 3, 5, 4]
>>> for rule in rules:
... c[rule] += 1
...
>>> c
defaultdict(int, {1: 1, 2: 2, 3: 2, 4: 2, 5: 1})
>>> c[3]
2 Or Counter:
>>> from collections import Counter
>>> c = Counter(rules)
>>> c
Counter({1: 1, 2: 2, 3: 2, 4: 2, 5: 1})
>>> c[5] += 1
>>> c[5]
2 EDIT: regarding 2 code: elif will be never executed therefore redundant. First if catches all True conditions and there is nothing left for elif not to mention that somewhere must be loop from which to break out (not provided in code sample).
Thanks for your answer,
for code 2, I wish I can actually simplify without the break like:
if condition:
x,y,z = none,none,none
a,b,c = 1,2,3
else:
x,y,z = 10,11,12
a,b,c = 1,2,3
Posts: 1,936
Threads: 8
Joined: Jun 2018
With code above you will get NameError (if you haven't somewhere in your code defined none, which would be bad practice).
a, b, c are the same in both cases, so no need to write those two times or put into conditionals. If you like 'clever' one-liners then you can do following regarding x, y, z:
>>> condition = ''
>>> x, y, z = ([x, None][bool(condition)] for x in range(10, 13))
>>> x, y, z
(10, 11, 12)
>>> condition = 'abc'
>>> x, y, z = ([x, None][bool(condition)] for x in range(10, 13))
>>> x, y, z
(None, None, None)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 2,955
Threads: 48
Joined: Sep 2016
In your initial code all the if conditions are with a = sing which is an assignment operator. The comparison operator is ==
Posts: 5
Threads: 3
Joined: Aug 2019
(Sep-20-2019, 06:59 AM)perfringo Wrote: With code above you will get NameError (if you haven't somewhere in your code defined none, which would be bad practice).
a, b, c are the same in both cases, so no need to write those two times or put into conditionals. If you like 'clever' one-liners then you can do following regarding x, y, z:
>>> condition = ''
>>> x, y, z = ([x, None][bool(condition)] for x in range(10, 13))
>>> x, y, z
(10, 11, 12)
>>> condition = 'abc'
>>> x, y, z = ([x, None][bool(condition)] for x in range(10, 13))
>>> x, y, z
(None, None, None)
Thanks for answering,
Sorry I am not quite understand the expression you wrote in one line,
It would be very appreciated to explain a bit.
Thanks!
Posts: 101
Threads: 5
Joined: Jul 2019
Sep-24-2019, 12:06 PM
(This post was last modified: Sep-24-2019, 12:06 PM by Malt.)
Same here.. I'm not understanding what @ perfringo wrote in single line..
Please explain the code what it is doing
Posts: 1,936
Threads: 8
Joined: Jun 2018
Let's explain what different parts are doing and then put them all together:
bool(condition) evaluates whether condition is True or False:
>>> condition = '' # empty string (list, tuple, dict etc) is False
>>> bool(condition)
False
>>> condition = 42
>>> bool(condition)
True In Python True and False are 1 and 0:
>>> False == 0
True
>>> True == 1
True So 'bool(condition)' evaluates and returns False (0) or True (1). This result is used as index:
>>> condition = 42
>>> ['first', 'second'][bool(condition)]
'second' # True is 1 and therefore item at index 1 is returned
>>> condition = ''
['first', 'second'][bool(condition)] # False is 0 and item at index 0 is returned
'first' Alternatively it can be expressed this way:
>>> x = 7 < 42
>>> ['first', 'second'][x]
'second'
>>> x = 7 > 42
>>> ['first', 'second'][x]
'first' This enables to return either first or second item in list depending whether condition is true or false.
As it was required to have values 10..12 or None depending on condition truthiness list comprehension is used to emit required values, evaluate condition and return corresponding item: 'give me number or None based on whether condition is true or not for every number in range 10..12'
>>> condition = ''
>>> [[number, None][bool(condition] for number in range(10, 13)]
>>> [10, 11, 12] Returned list is unpacked into variables:
>>> first, second, third = [[number, None][bool(condition)] for number in range(10, 13)]
>>> first
10
>>> second
11
>>> third
12
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
|