Find number of times a number is followed by a larger number in a list
I have this function to find the number of times a number is followed by a larger number in a list. Is there another more "pythonic" way this could be done? I am using Python 3.7.0.
Thanks in advance.
def find_greater_numbers(arr):
count = 0
i = 0
j = 1
while i < len(arr):
while j < len(arr):
if arr[j] > arr[i]:
count += 1
j+=1
j = i+1
i+=1
return count
find_greater_numbers([6,1,2,7]]) # returns 4
python python-3.7
add a comment |
I have this function to find the number of times a number is followed by a larger number in a list. Is there another more "pythonic" way this could be done? I am using Python 3.7.0.
Thanks in advance.
def find_greater_numbers(arr):
count = 0
i = 0
j = 1
while i < len(arr):
while j < len(arr):
if arr[j] > arr[i]:
count += 1
j+=1
j = i+1
i+=1
return count
find_greater_numbers([6,1,2,7]]) # returns 4
python python-3.7
add a comment |
I have this function to find the number of times a number is followed by a larger number in a list. Is there another more "pythonic" way this could be done? I am using Python 3.7.0.
Thanks in advance.
def find_greater_numbers(arr):
count = 0
i = 0
j = 1
while i < len(arr):
while j < len(arr):
if arr[j] > arr[i]:
count += 1
j+=1
j = i+1
i+=1
return count
find_greater_numbers([6,1,2,7]]) # returns 4
python python-3.7
I have this function to find the number of times a number is followed by a larger number in a list. Is there another more "pythonic" way this could be done? I am using Python 3.7.0.
Thanks in advance.
def find_greater_numbers(arr):
count = 0
i = 0
j = 1
while i < len(arr):
while j < len(arr):
if arr[j] > arr[i]:
count += 1
j+=1
j = i+1
i+=1
return count
find_greater_numbers([6,1,2,7]]) # returns 4
python python-3.7
python python-3.7
edited Nov 13 '18 at 3:58
Julien
7,60331537
7,60331537
asked Nov 13 '18 at 3:43
johnsmithoptionaljohnsmithoptional
82
82
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
It's a bit unclear if you mean immediately followed, or followed at any later index
In the first case, this one liner:
sum(x < y for x,y in zip(arr[:-1],arr[1:])) # answer is 2
In the second, this one:
sum(any(x < y for y in arr[i:]) for i,x in enumerate(arr)) # answer is 3
And if you want to count the number of exact such pairs (like what your actual code seems to be doing):
sum(x < y for i,x in enumerate(arr) for y in arr[i:]) # answer is 4
Hello. I meant followed by a greater number at any later index. So in the example list [6,1,2,7], 6 is followed 1 time by a greater number 1 is followed 2 times by a greater number , 2 is followed 1 time by a greater number (1+2+1=4). Thanks.
– johnsmithoptional
Nov 13 '18 at 4:24
add a comment |
Use map to get the list of True/False for each pair of number in the list depending on the condition if x < y. This list is being generated using the for loop. After getting the list, filter the number of 'True' in the list. Then create a single list using itertools.chain.from_iterable() function and find its length.
import itertools
arr = [6, 1, 2, 7]
num = len(list(itertools.chain.from_iterable(list(filter(lambda x: x , map(lambda x, y: x<y, arr[:-i], arr[i:]))) for i, x in enumerate(arr))))
print(num)
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%2f53273467%2ffind-number-of-times-a-number-is-followed-by-a-larger-number-in-a-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's a bit unclear if you mean immediately followed, or followed at any later index
In the first case, this one liner:
sum(x < y for x,y in zip(arr[:-1],arr[1:])) # answer is 2
In the second, this one:
sum(any(x < y for y in arr[i:]) for i,x in enumerate(arr)) # answer is 3
And if you want to count the number of exact such pairs (like what your actual code seems to be doing):
sum(x < y for i,x in enumerate(arr) for y in arr[i:]) # answer is 4
Hello. I meant followed by a greater number at any later index. So in the example list [6,1,2,7], 6 is followed 1 time by a greater number 1 is followed 2 times by a greater number , 2 is followed 1 time by a greater number (1+2+1=4). Thanks.
– johnsmithoptional
Nov 13 '18 at 4:24
add a comment |
It's a bit unclear if you mean immediately followed, or followed at any later index
In the first case, this one liner:
sum(x < y for x,y in zip(arr[:-1],arr[1:])) # answer is 2
In the second, this one:
sum(any(x < y for y in arr[i:]) for i,x in enumerate(arr)) # answer is 3
And if you want to count the number of exact such pairs (like what your actual code seems to be doing):
sum(x < y for i,x in enumerate(arr) for y in arr[i:]) # answer is 4
Hello. I meant followed by a greater number at any later index. So in the example list [6,1,2,7], 6 is followed 1 time by a greater number 1 is followed 2 times by a greater number , 2 is followed 1 time by a greater number (1+2+1=4). Thanks.
– johnsmithoptional
Nov 13 '18 at 4:24
add a comment |
It's a bit unclear if you mean immediately followed, or followed at any later index
In the first case, this one liner:
sum(x < y for x,y in zip(arr[:-1],arr[1:])) # answer is 2
In the second, this one:
sum(any(x < y for y in arr[i:]) for i,x in enumerate(arr)) # answer is 3
And if you want to count the number of exact such pairs (like what your actual code seems to be doing):
sum(x < y for i,x in enumerate(arr) for y in arr[i:]) # answer is 4
It's a bit unclear if you mean immediately followed, or followed at any later index
In the first case, this one liner:
sum(x < y for x,y in zip(arr[:-1],arr[1:])) # answer is 2
In the second, this one:
sum(any(x < y for y in arr[i:]) for i,x in enumerate(arr)) # answer is 3
And if you want to count the number of exact such pairs (like what your actual code seems to be doing):
sum(x < y for i,x in enumerate(arr) for y in arr[i:]) # answer is 4
edited Nov 13 '18 at 4:07
answered Nov 13 '18 at 3:53
JulienJulien
7,60331537
7,60331537
Hello. I meant followed by a greater number at any later index. So in the example list [6,1,2,7], 6 is followed 1 time by a greater number 1 is followed 2 times by a greater number , 2 is followed 1 time by a greater number (1+2+1=4). Thanks.
– johnsmithoptional
Nov 13 '18 at 4:24
add a comment |
Hello. I meant followed by a greater number at any later index. So in the example list [6,1,2,7], 6 is followed 1 time by a greater number 1 is followed 2 times by a greater number , 2 is followed 1 time by a greater number (1+2+1=4). Thanks.
– johnsmithoptional
Nov 13 '18 at 4:24
Hello. I meant followed by a greater number at any later index. So in the example list [6,1,2,7], 6 is followed 1 time by a greater number 1 is followed 2 times by a greater number , 2 is followed 1 time by a greater number (1+2+1=4). Thanks.
– johnsmithoptional
Nov 13 '18 at 4:24
Hello. I meant followed by a greater number at any later index. So in the example list [6,1,2,7], 6 is followed 1 time by a greater number 1 is followed 2 times by a greater number , 2 is followed 1 time by a greater number (1+2+1=4). Thanks.
– johnsmithoptional
Nov 13 '18 at 4:24
add a comment |
Use map to get the list of True/False for each pair of number in the list depending on the condition if x < y. This list is being generated using the for loop. After getting the list, filter the number of 'True' in the list. Then create a single list using itertools.chain.from_iterable() function and find its length.
import itertools
arr = [6, 1, 2, 7]
num = len(list(itertools.chain.from_iterable(list(filter(lambda x: x , map(lambda x, y: x<y, arr[:-i], arr[i:]))) for i, x in enumerate(arr))))
print(num)
add a comment |
Use map to get the list of True/False for each pair of number in the list depending on the condition if x < y. This list is being generated using the for loop. After getting the list, filter the number of 'True' in the list. Then create a single list using itertools.chain.from_iterable() function and find its length.
import itertools
arr = [6, 1, 2, 7]
num = len(list(itertools.chain.from_iterable(list(filter(lambda x: x , map(lambda x, y: x<y, arr[:-i], arr[i:]))) for i, x in enumerate(arr))))
print(num)
add a comment |
Use map to get the list of True/False for each pair of number in the list depending on the condition if x < y. This list is being generated using the for loop. After getting the list, filter the number of 'True' in the list. Then create a single list using itertools.chain.from_iterable() function and find its length.
import itertools
arr = [6, 1, 2, 7]
num = len(list(itertools.chain.from_iterable(list(filter(lambda x: x , map(lambda x, y: x<y, arr[:-i], arr[i:]))) for i, x in enumerate(arr))))
print(num)
Use map to get the list of True/False for each pair of number in the list depending on the condition if x < y. This list is being generated using the for loop. After getting the list, filter the number of 'True' in the list. Then create a single list using itertools.chain.from_iterable() function and find its length.
import itertools
arr = [6, 1, 2, 7]
num = len(list(itertools.chain.from_iterable(list(filter(lambda x: x , map(lambda x, y: x<y, arr[:-i], arr[i:]))) for i, x in enumerate(arr))))
print(num)
answered Nov 13 '18 at 4:33
Rishabh MishraRishabh Mishra
378310
378310
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%2f53273467%2ffind-number-of-times-a-number-is-followed-by-a-larger-number-in-a-list%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