More efficient way to loop through tuple and obtain list of lists of transitions from high to lowAre tuples more efficient than lists in Python?What's the best way to break from nested loops in JavaScript?Efficient way to shift a list in pythonHow do I loop through a list by twos?What's the fastest way to loop through an array in JavaScript?What is the most efficient way to loop through dataframes with pandas?Python: How do I assign 2 values I return from a function with 1 input as values outside the function?Comparing data in a list in python in a loop until no more pairs can be madeIs there a more pythonic/more efficient way to loop through dictionary containing lists rather than using for loops?Looking for a more efficient/pythonic way to sum tuples in a list, and compute an average
Assigning function to function pointer, const argument correctness?
Does a (nice) centerless group always have a centerless profinite completion?
Breaking changes to eieio in Emacs 27?
Canada travel to US using Global Entry
Flight compensation with agent
Does putting salt first make it easier for attacker to bruteforce the hash?
Can there be absolute velocity?
How (un)safe is it to ride barefoot?
Print "N NE E SE S SW W NW"
Trying to get (more) accurate readings from thermistor (electronics, math, and code inside)
Can a human be transformed into a Mind Flayer?
What STL algorithm can determine if exactly one item in a container satisfies a predicate?
Three questions
How and why do references in academic papers work?
How to avoid typing 'git' at the begining of every Git command
Convert only certain words to lowercase
Why isn't Bash trap working if output is redirected to stdout?
Is Dumbledore a human lie detector?
Confused with atmospheric pressure equals plastic balloon’s inner pressure
Grep Match and extract
Do you have to have figures when playing D&D?
Can you make an identity from this product?
Why do radiation hardened IC packages often have long leads?
How to find a better angle and distance for clicking picture of a distorted artwork to achieve 3D effect?
More efficient way to loop through tuple and obtain list of lists of transitions from high to low
Are tuples more efficient than lists in Python?What's the best way to break from nested loops in JavaScript?Efficient way to shift a list in pythonHow do I loop through a list by twos?What's the fastest way to loop through an array in JavaScript?What is the most efficient way to loop through dataframes with pandas?Python: How do I assign 2 values I return from a function with 1 input as values outside the function?Comparing data in a list in python in a loop until no more pairs can be madeIs there a more pythonic/more efficient way to loop through dictionary containing lists rather than using for loops?Looking for a more efficient/pythonic way to sum tuples in a list, and compute an average
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a tuple of input integer values, and a value of high. I want to loop through and pull out pairs of the first instance of a value in the tuple that is >= high, and then first value that is < high. An example might help:
Keep only the first high or first low of each repeating high or low
For instance, if max == 100, and the input is (102, 109, 120, 80, 40, 30, 200, 90)
output should be [[102, 80], [200, 90]]
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
started = False # Used to ensure we start with high values first
currently_on_highs = True
first_val_high = True
first_val_low = True
transitions = []
inner_transition = []
for item in items:
if item >= high:
started = True
currently_on_highs = True
if first_val_high:
first_val_high = False
first_val_low = True
inner_transition.append(item)
else:
if started:
currently_on_highs = False
if first_val_low:
first_val_high = True
first_val_low = False
inner_transition.append(item)
transitions.append(inner_transition)
inner_transition = []
print(transitions)
Here's a much better result after the suggestion from @michael-butscher
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
in_high = False
transitions = []
inner_transition = []
for item in items:
if item >= high and not in_high:
in_high = True
inner_transition.append(item)
elif item < high and in_high:
in_high = False
inner_transition.append(item)
transitions.append(inner_transition)
inner_transition = []
print(transitions)
python list loops for-loop tuples
add a comment |
I have a tuple of input integer values, and a value of high. I want to loop through and pull out pairs of the first instance of a value in the tuple that is >= high, and then first value that is < high. An example might help:
Keep only the first high or first low of each repeating high or low
For instance, if max == 100, and the input is (102, 109, 120, 80, 40, 30, 200, 90)
output should be [[102, 80], [200, 90]]
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
started = False # Used to ensure we start with high values first
currently_on_highs = True
first_val_high = True
first_val_low = True
transitions = []
inner_transition = []
for item in items:
if item >= high:
started = True
currently_on_highs = True
if first_val_high:
first_val_high = False
first_val_low = True
inner_transition.append(item)
else:
if started:
currently_on_highs = False
if first_val_low:
first_val_high = True
first_val_low = False
inner_transition.append(item)
transitions.append(inner_transition)
inner_transition = []
print(transitions)
Here's a much better result after the suggestion from @michael-butscher
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
in_high = False
transitions = []
inner_transition = []
for item in items:
if item >= high and not in_high:
in_high = True
inner_transition.append(item)
elif item < high and in_high:
in_high = False
inner_transition.append(item)
transitions.append(inner_transition)
inner_transition = []
print(transitions)
python list loops for-loop tuples
2
You only need one boolean variable (e.g.in_high
). If you find a high value andin_high
is False, start new inner transition with the value, setin_high
True. Else if a value is low andin_high
is True, close inner transition and changein_high
. Other possible conditions aren't of interest.
– Michael Butscher
Mar 24 at 22:02
3
More efficient in what dimension? Lines of code? CPU time? Memory? You could probably subtract 100 from all values and just act on sign changes.
– schlenk
Mar 24 at 22:05
Thanks, @michael-butscher
– djangomachine
Mar 24 at 22:13
Thanks, @schlenk You gave me something good to mull over.
– djangomachine
Mar 24 at 22:13
@djangomachine My answer is still shorter.
– Artemis Fowl
Mar 24 at 22:17
add a comment |
I have a tuple of input integer values, and a value of high. I want to loop through and pull out pairs of the first instance of a value in the tuple that is >= high, and then first value that is < high. An example might help:
Keep only the first high or first low of each repeating high or low
For instance, if max == 100, and the input is (102, 109, 120, 80, 40, 30, 200, 90)
output should be [[102, 80], [200, 90]]
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
started = False # Used to ensure we start with high values first
currently_on_highs = True
first_val_high = True
first_val_low = True
transitions = []
inner_transition = []
for item in items:
if item >= high:
started = True
currently_on_highs = True
if first_val_high:
first_val_high = False
first_val_low = True
inner_transition.append(item)
else:
if started:
currently_on_highs = False
if first_val_low:
first_val_high = True
first_val_low = False
inner_transition.append(item)
transitions.append(inner_transition)
inner_transition = []
print(transitions)
Here's a much better result after the suggestion from @michael-butscher
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
in_high = False
transitions = []
inner_transition = []
for item in items:
if item >= high and not in_high:
in_high = True
inner_transition.append(item)
elif item < high and in_high:
in_high = False
inner_transition.append(item)
transitions.append(inner_transition)
inner_transition = []
print(transitions)
python list loops for-loop tuples
I have a tuple of input integer values, and a value of high. I want to loop through and pull out pairs of the first instance of a value in the tuple that is >= high, and then first value that is < high. An example might help:
Keep only the first high or first low of each repeating high or low
For instance, if max == 100, and the input is (102, 109, 120, 80, 40, 30, 200, 90)
output should be [[102, 80], [200, 90]]
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
started = False # Used to ensure we start with high values first
currently_on_highs = True
first_val_high = True
first_val_low = True
transitions = []
inner_transition = []
for item in items:
if item >= high:
started = True
currently_on_highs = True
if first_val_high:
first_val_high = False
first_val_low = True
inner_transition.append(item)
else:
if started:
currently_on_highs = False
if first_val_low:
first_val_high = True
first_val_low = False
inner_transition.append(item)
transitions.append(inner_transition)
inner_transition = []
print(transitions)
Here's a much better result after the suggestion from @michael-butscher
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
in_high = False
transitions = []
inner_transition = []
for item in items:
if item >= high and not in_high:
in_high = True
inner_transition.append(item)
elif item < high and in_high:
in_high = False
inner_transition.append(item)
transitions.append(inner_transition)
inner_transition = []
print(transitions)
python list loops for-loop tuples
python list loops for-loop tuples
edited Mar 24 at 22:11
djangomachine
asked Mar 24 at 21:48
djangomachinedjangomachine
1898
1898
2
You only need one boolean variable (e.g.in_high
). If you find a high value andin_high
is False, start new inner transition with the value, setin_high
True. Else if a value is low andin_high
is True, close inner transition and changein_high
. Other possible conditions aren't of interest.
– Michael Butscher
Mar 24 at 22:02
3
More efficient in what dimension? Lines of code? CPU time? Memory? You could probably subtract 100 from all values and just act on sign changes.
– schlenk
Mar 24 at 22:05
Thanks, @michael-butscher
– djangomachine
Mar 24 at 22:13
Thanks, @schlenk You gave me something good to mull over.
– djangomachine
Mar 24 at 22:13
@djangomachine My answer is still shorter.
– Artemis Fowl
Mar 24 at 22:17
add a comment |
2
You only need one boolean variable (e.g.in_high
). If you find a high value andin_high
is False, start new inner transition with the value, setin_high
True. Else if a value is low andin_high
is True, close inner transition and changein_high
. Other possible conditions aren't of interest.
– Michael Butscher
Mar 24 at 22:02
3
More efficient in what dimension? Lines of code? CPU time? Memory? You could probably subtract 100 from all values and just act on sign changes.
– schlenk
Mar 24 at 22:05
Thanks, @michael-butscher
– djangomachine
Mar 24 at 22:13
Thanks, @schlenk You gave me something good to mull over.
– djangomachine
Mar 24 at 22:13
@djangomachine My answer is still shorter.
– Artemis Fowl
Mar 24 at 22:17
2
2
You only need one boolean variable (e.g.
in_high
). If you find a high value and in_high
is False, start new inner transition with the value, set in_high
True. Else if a value is low and in_high
is True, close inner transition and changein_high
. Other possible conditions aren't of interest.– Michael Butscher
Mar 24 at 22:02
You only need one boolean variable (e.g.
in_high
). If you find a high value and in_high
is False, start new inner transition with the value, set in_high
True. Else if a value is low and in_high
is True, close inner transition and changein_high
. Other possible conditions aren't of interest.– Michael Butscher
Mar 24 at 22:02
3
3
More efficient in what dimension? Lines of code? CPU time? Memory? You could probably subtract 100 from all values and just act on sign changes.
– schlenk
Mar 24 at 22:05
More efficient in what dimension? Lines of code? CPU time? Memory? You could probably subtract 100 from all values and just act on sign changes.
– schlenk
Mar 24 at 22:05
Thanks, @michael-butscher
– djangomachine
Mar 24 at 22:13
Thanks, @michael-butscher
– djangomachine
Mar 24 at 22:13
Thanks, @schlenk You gave me something good to mull over.
– djangomachine
Mar 24 at 22:13
Thanks, @schlenk You gave me something good to mull over.
– djangomachine
Mar 24 at 22:13
@djangomachine My answer is still shorter.
– Artemis Fowl
Mar 24 at 22:17
@djangomachine My answer is still shorter.
– Artemis Fowl
Mar 24 at 22:17
add a comment |
3 Answers
3
active
oldest
votes
You can easily do this with zip by offsetting the elements by one entry to compare each of them with their predecessor:
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
bounds = [ num for prev,num in zip((high-1,)+items,items) if (num<high)^(prev<high) ]
result = list(zip(bounds[::2],bounds[1::2]))
print(result) # [(102, 80), (200, 90)]
add a comment |
Here you go:
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
ret = []
findh = True
for i in items:
if findh and i >= high:
findh = False
ret.append([i])
elif not findh and i < high:
findh = True
ret[-1].append(i)
print(ret)
Explanation:
ret = []
what we will add the pairs tofindh = True
whether or not we should look for a higher valueif findh and i >= high:
if we are looking higher and it is higerfindh = False
look lower in futureret.append([i])
add another list to our master list, with just this value in
elif not findh and i < high:
if we are looking lower and it is lowerfindh = True
look higher in futureret[-1].append(i)
add this value to the last list in the master list
add a comment |
A first step could be creating a list of all "switch" values:
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
res = []
last = high-1
for x in items:
if last < high <= x or x <= high < last:
res.append(x)
last = x
then you can build the pairs:
res = [(res[i], res[i+1]) for i in range(0, len(res), 2)]
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55328902%2fmore-efficient-way-to-loop-through-tuple-and-obtain-list-of-lists-of-transitions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can easily do this with zip by offsetting the elements by one entry to compare each of them with their predecessor:
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
bounds = [ num for prev,num in zip((high-1,)+items,items) if (num<high)^(prev<high) ]
result = list(zip(bounds[::2],bounds[1::2]))
print(result) # [(102, 80), (200, 90)]
add a comment |
You can easily do this with zip by offsetting the elements by one entry to compare each of them with their predecessor:
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
bounds = [ num for prev,num in zip((high-1,)+items,items) if (num<high)^(prev<high) ]
result = list(zip(bounds[::2],bounds[1::2]))
print(result) # [(102, 80), (200, 90)]
add a comment |
You can easily do this with zip by offsetting the elements by one entry to compare each of them with their predecessor:
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
bounds = [ num for prev,num in zip((high-1,)+items,items) if (num<high)^(prev<high) ]
result = list(zip(bounds[::2],bounds[1::2]))
print(result) # [(102, 80), (200, 90)]
You can easily do this with zip by offsetting the elements by one entry to compare each of them with their predecessor:
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
bounds = [ num for prev,num in zip((high-1,)+items,items) if (num<high)^(prev<high) ]
result = list(zip(bounds[::2],bounds[1::2]))
print(result) # [(102, 80), (200, 90)]
edited Mar 25 at 4:20
answered Mar 24 at 22:47
Alain T.Alain T.
10.2k11430
10.2k11430
add a comment |
add a comment |
Here you go:
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
ret = []
findh = True
for i in items:
if findh and i >= high:
findh = False
ret.append([i])
elif not findh and i < high:
findh = True
ret[-1].append(i)
print(ret)
Explanation:
ret = []
what we will add the pairs tofindh = True
whether or not we should look for a higher valueif findh and i >= high:
if we are looking higher and it is higerfindh = False
look lower in futureret.append([i])
add another list to our master list, with just this value in
elif not findh and i < high:
if we are looking lower and it is lowerfindh = True
look higher in futureret[-1].append(i)
add this value to the last list in the master list
add a comment |
Here you go:
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
ret = []
findh = True
for i in items:
if findh and i >= high:
findh = False
ret.append([i])
elif not findh and i < high:
findh = True
ret[-1].append(i)
print(ret)
Explanation:
ret = []
what we will add the pairs tofindh = True
whether or not we should look for a higher valueif findh and i >= high:
if we are looking higher and it is higerfindh = False
look lower in futureret.append([i])
add another list to our master list, with just this value in
elif not findh and i < high:
if we are looking lower and it is lowerfindh = True
look higher in futureret[-1].append(i)
add this value to the last list in the master list
add a comment |
Here you go:
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
ret = []
findh = True
for i in items:
if findh and i >= high:
findh = False
ret.append([i])
elif not findh and i < high:
findh = True
ret[-1].append(i)
print(ret)
Explanation:
ret = []
what we will add the pairs tofindh = True
whether or not we should look for a higher valueif findh and i >= high:
if we are looking higher and it is higerfindh = False
look lower in futureret.append([i])
add another list to our master list, with just this value in
elif not findh and i < high:
if we are looking lower and it is lowerfindh = True
look higher in futureret[-1].append(i)
add this value to the last list in the master list
Here you go:
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
ret = []
findh = True
for i in items:
if findh and i >= high:
findh = False
ret.append([i])
elif not findh and i < high:
findh = True
ret[-1].append(i)
print(ret)
Explanation:
ret = []
what we will add the pairs tofindh = True
whether or not we should look for a higher valueif findh and i >= high:
if we are looking higher and it is higerfindh = False
look lower in futureret.append([i])
add another list to our master list, with just this value in
elif not findh and i < high:
if we are looking lower and it is lowerfindh = True
look higher in futureret[-1].append(i)
add this value to the last list in the master list
edited Mar 24 at 22:12
answered Mar 24 at 22:05
Artemis FowlArtemis Fowl
1,67741328
1,67741328
add a comment |
add a comment |
A first step could be creating a list of all "switch" values:
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
res = []
last = high-1
for x in items:
if last < high <= x or x <= high < last:
res.append(x)
last = x
then you can build the pairs:
res = [(res[i], res[i+1]) for i in range(0, len(res), 2)]
add a comment |
A first step could be creating a list of all "switch" values:
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
res = []
last = high-1
for x in items:
if last < high <= x or x <= high < last:
res.append(x)
last = x
then you can build the pairs:
res = [(res[i], res[i+1]) for i in range(0, len(res), 2)]
add a comment |
A first step could be creating a list of all "switch" values:
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
res = []
last = high-1
for x in items:
if last < high <= x or x <= high < last:
res.append(x)
last = x
then you can build the pairs:
res = [(res[i], res[i+1]) for i in range(0, len(res), 2)]
A first step could be creating a list of all "switch" values:
items = (102, 109, 120, 80, 40, 30, 200, 90)
high = 100
res = []
last = high-1
for x in items:
if last < high <= x or x <= high < last:
res.append(x)
last = x
then you can build the pairs:
res = [(res[i], res[i+1]) for i in range(0, len(res), 2)]
answered Mar 24 at 22:57
65026502
89.2k13117219
89.2k13117219
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55328902%2fmore-efficient-way-to-loop-through-tuple-and-obtain-list-of-lists-of-transitions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You only need one boolean variable (e.g.
in_high
). If you find a high value andin_high
is False, start new inner transition with the value, setin_high
True. Else if a value is low andin_high
is True, close inner transition and changein_high
. Other possible conditions aren't of interest.– Michael Butscher
Mar 24 at 22:02
3
More efficient in what dimension? Lines of code? CPU time? Memory? You could probably subtract 100 from all values and just act on sign changes.
– schlenk
Mar 24 at 22:05
Thanks, @michael-butscher
– djangomachine
Mar 24 at 22:13
Thanks, @schlenk You gave me something good to mull over.
– djangomachine
Mar 24 at 22:13
@djangomachine My answer is still shorter.
– Artemis Fowl
Mar 24 at 22:17