There is no counter in inline python?
When I'm trying to put counter in inline loop of Python, it tells me the syntax error. Apparently here it expects me to assign a value to i
not k
.
Could anyone help with rewriting the inline loop?
aa = [2, 2, 1]
k = 0
b = [k += 1 if i != 2 for i in aa ]
print(b)
python loops inline
add a comment |
When I'm trying to put counter in inline loop of Python, it tells me the syntax error. Apparently here it expects me to assign a value to i
not k
.
Could anyone help with rewriting the inline loop?
aa = [2, 2, 1]
k = 0
b = [k += 1 if i != 2 for i in aa ]
print(b)
python loops inline
7
That is not an "inline loop". List comprehensions are not a nifty way to write arbitraryfor
loops; they are for building lists.
– user2357112
Nov 13 '18 at 20:11
count = sum([i != 2 for i in aa ] )
– Joran Beasley
Nov 13 '18 at 20:11
1
What were you even expectingb
to be after that?
– user2357112
Nov 13 '18 at 20:11
Hi @user2357112, ideally I'm expecting to get [2,1]. To count how many times "2" shows up in the list and keep the rest of the list as it was.
– Bridget Huang
Nov 13 '18 at 20:42
add a comment |
When I'm trying to put counter in inline loop of Python, it tells me the syntax error. Apparently here it expects me to assign a value to i
not k
.
Could anyone help with rewriting the inline loop?
aa = [2, 2, 1]
k = 0
b = [k += 1 if i != 2 for i in aa ]
print(b)
python loops inline
When I'm trying to put counter in inline loop of Python, it tells me the syntax error. Apparently here it expects me to assign a value to i
not k
.
Could anyone help with rewriting the inline loop?
aa = [2, 2, 1]
k = 0
b = [k += 1 if i != 2 for i in aa ]
print(b)
python loops inline
python loops inline
edited Nov 13 '18 at 20:36
Alois Mahdal
5,24244062
5,24244062
asked Nov 13 '18 at 20:09
Bridget HuangBridget Huang
33
33
7
That is not an "inline loop". List comprehensions are not a nifty way to write arbitraryfor
loops; they are for building lists.
– user2357112
Nov 13 '18 at 20:11
count = sum([i != 2 for i in aa ] )
– Joran Beasley
Nov 13 '18 at 20:11
1
What were you even expectingb
to be after that?
– user2357112
Nov 13 '18 at 20:11
Hi @user2357112, ideally I'm expecting to get [2,1]. To count how many times "2" shows up in the list and keep the rest of the list as it was.
– Bridget Huang
Nov 13 '18 at 20:42
add a comment |
7
That is not an "inline loop". List comprehensions are not a nifty way to write arbitraryfor
loops; they are for building lists.
– user2357112
Nov 13 '18 at 20:11
count = sum([i != 2 for i in aa ] )
– Joran Beasley
Nov 13 '18 at 20:11
1
What were you even expectingb
to be after that?
– user2357112
Nov 13 '18 at 20:11
Hi @user2357112, ideally I'm expecting to get [2,1]. To count how many times "2" shows up in the list and keep the rest of the list as it was.
– Bridget Huang
Nov 13 '18 at 20:42
7
7
That is not an "inline loop". List comprehensions are not a nifty way to write arbitrary
for
loops; they are for building lists.– user2357112
Nov 13 '18 at 20:11
That is not an "inline loop". List comprehensions are not a nifty way to write arbitrary
for
loops; they are for building lists.– user2357112
Nov 13 '18 at 20:11
count = sum([i != 2 for i in aa ] )
– Joran Beasley
Nov 13 '18 at 20:11
count = sum([i != 2 for i in aa ] )
– Joran Beasley
Nov 13 '18 at 20:11
1
1
What were you even expecting
b
to be after that?– user2357112
Nov 13 '18 at 20:11
What were you even expecting
b
to be after that?– user2357112
Nov 13 '18 at 20:11
Hi @user2357112, ideally I'm expecting to get [2,1]. To count how many times "2" shows up in the list and keep the rest of the list as it was.
– Bridget Huang
Nov 13 '18 at 20:42
Hi @user2357112, ideally I'm expecting to get [2,1]. To count how many times "2" shows up in the list and keep the rest of the list as it was.
– Bridget Huang
Nov 13 '18 at 20:42
add a comment |
2 Answers
2
active
oldest
votes
You seem to misunderstand what you're doing. This:
[x for y in z]
is not an "inline for loop". A for
loop can do anything, iterating on any iterable object. One of the things a for
loop can do is create a list of items:
my_list =
for i in other_list:
if condition_is_met:
my_list.append(i)
A list comprehension covers only this use case of a for
loop:
my_list = [i for i in other_list if condition_is_met]
That's why it's called a "list comprehension" and not an "inline for loop" - because it only creates lists. The other things you might use a for
loop for, like iterating a number, you can't directly use a list comprehension to do.
For your particular problem, you're trying to use k += 1
in a list comprehension. This operation doesn't return anything - it just modifies the variable k
- so when python tries to assign that to a list item, the operation fails. If you want to count up with k
, you should either just use a regular for
loop:
for i in aa:
if i != 2:
k += 1
or use the list comprehension to indirectly measure what you want:
k += len([i for i in aa if i != 2])
Here, we use a list comprehension to construct a list of every element i
in aa
such that i != 2
, then we take the number of elements in that list and add it to k
. Since this operation actually produces a list of its own, the code will not crash, and it will have the same overall effect. This solution isn't always doable if you have more complicated things you'd like to do in a for
loop - and it's slightly less efficient as well, because this solution requires actually creating the new list which isn't necessary for what you're trying to achieve.
Very comprehensive answer! Much appreciated. May I ask a further question? Is it possible to realize "list comprehension" and iteration at the same time? E.g. To convert [True, False, False, False, True] to [0,3,4]. "0", and "4" represent the position, while "3" is the count of "False"
– Bridget Huang
Nov 13 '18 at 20:52
I don't think you'd be able to do that with a list comprehension. They're a useful tool, but it's not likefor
loops are to be avoided or anything, and for a task like that I would just use afor
loop (basically do a finite-state-machine thing or something)
– Green Cloak Guy
Nov 14 '18 at 2:47
Thanks for your answer!
– Bridget Huang
Nov 14 '18 at 16:21
add a comment |
you can use len()
like so
print(len([i for i in a if i != 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%2f53288732%2fthere-is-no-counter-in-inline-python%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
You seem to misunderstand what you're doing. This:
[x for y in z]
is not an "inline for loop". A for
loop can do anything, iterating on any iterable object. One of the things a for
loop can do is create a list of items:
my_list =
for i in other_list:
if condition_is_met:
my_list.append(i)
A list comprehension covers only this use case of a for
loop:
my_list = [i for i in other_list if condition_is_met]
That's why it's called a "list comprehension" and not an "inline for loop" - because it only creates lists. The other things you might use a for
loop for, like iterating a number, you can't directly use a list comprehension to do.
For your particular problem, you're trying to use k += 1
in a list comprehension. This operation doesn't return anything - it just modifies the variable k
- so when python tries to assign that to a list item, the operation fails. If you want to count up with k
, you should either just use a regular for
loop:
for i in aa:
if i != 2:
k += 1
or use the list comprehension to indirectly measure what you want:
k += len([i for i in aa if i != 2])
Here, we use a list comprehension to construct a list of every element i
in aa
such that i != 2
, then we take the number of elements in that list and add it to k
. Since this operation actually produces a list of its own, the code will not crash, and it will have the same overall effect. This solution isn't always doable if you have more complicated things you'd like to do in a for
loop - and it's slightly less efficient as well, because this solution requires actually creating the new list which isn't necessary for what you're trying to achieve.
Very comprehensive answer! Much appreciated. May I ask a further question? Is it possible to realize "list comprehension" and iteration at the same time? E.g. To convert [True, False, False, False, True] to [0,3,4]. "0", and "4" represent the position, while "3" is the count of "False"
– Bridget Huang
Nov 13 '18 at 20:52
I don't think you'd be able to do that with a list comprehension. They're a useful tool, but it's not likefor
loops are to be avoided or anything, and for a task like that I would just use afor
loop (basically do a finite-state-machine thing or something)
– Green Cloak Guy
Nov 14 '18 at 2:47
Thanks for your answer!
– Bridget Huang
Nov 14 '18 at 16:21
add a comment |
You seem to misunderstand what you're doing. This:
[x for y in z]
is not an "inline for loop". A for
loop can do anything, iterating on any iterable object. One of the things a for
loop can do is create a list of items:
my_list =
for i in other_list:
if condition_is_met:
my_list.append(i)
A list comprehension covers only this use case of a for
loop:
my_list = [i for i in other_list if condition_is_met]
That's why it's called a "list comprehension" and not an "inline for loop" - because it only creates lists. The other things you might use a for
loop for, like iterating a number, you can't directly use a list comprehension to do.
For your particular problem, you're trying to use k += 1
in a list comprehension. This operation doesn't return anything - it just modifies the variable k
- so when python tries to assign that to a list item, the operation fails. If you want to count up with k
, you should either just use a regular for
loop:
for i in aa:
if i != 2:
k += 1
or use the list comprehension to indirectly measure what you want:
k += len([i for i in aa if i != 2])
Here, we use a list comprehension to construct a list of every element i
in aa
such that i != 2
, then we take the number of elements in that list and add it to k
. Since this operation actually produces a list of its own, the code will not crash, and it will have the same overall effect. This solution isn't always doable if you have more complicated things you'd like to do in a for
loop - and it's slightly less efficient as well, because this solution requires actually creating the new list which isn't necessary for what you're trying to achieve.
Very comprehensive answer! Much appreciated. May I ask a further question? Is it possible to realize "list comprehension" and iteration at the same time? E.g. To convert [True, False, False, False, True] to [0,3,4]. "0", and "4" represent the position, while "3" is the count of "False"
– Bridget Huang
Nov 13 '18 at 20:52
I don't think you'd be able to do that with a list comprehension. They're a useful tool, but it's not likefor
loops are to be avoided or anything, and for a task like that I would just use afor
loop (basically do a finite-state-machine thing or something)
– Green Cloak Guy
Nov 14 '18 at 2:47
Thanks for your answer!
– Bridget Huang
Nov 14 '18 at 16:21
add a comment |
You seem to misunderstand what you're doing. This:
[x for y in z]
is not an "inline for loop". A for
loop can do anything, iterating on any iterable object. One of the things a for
loop can do is create a list of items:
my_list =
for i in other_list:
if condition_is_met:
my_list.append(i)
A list comprehension covers only this use case of a for
loop:
my_list = [i for i in other_list if condition_is_met]
That's why it's called a "list comprehension" and not an "inline for loop" - because it only creates lists. The other things you might use a for
loop for, like iterating a number, you can't directly use a list comprehension to do.
For your particular problem, you're trying to use k += 1
in a list comprehension. This operation doesn't return anything - it just modifies the variable k
- so when python tries to assign that to a list item, the operation fails. If you want to count up with k
, you should either just use a regular for
loop:
for i in aa:
if i != 2:
k += 1
or use the list comprehension to indirectly measure what you want:
k += len([i for i in aa if i != 2])
Here, we use a list comprehension to construct a list of every element i
in aa
such that i != 2
, then we take the number of elements in that list and add it to k
. Since this operation actually produces a list of its own, the code will not crash, and it will have the same overall effect. This solution isn't always doable if you have more complicated things you'd like to do in a for
loop - and it's slightly less efficient as well, because this solution requires actually creating the new list which isn't necessary for what you're trying to achieve.
You seem to misunderstand what you're doing. This:
[x for y in z]
is not an "inline for loop". A for
loop can do anything, iterating on any iterable object. One of the things a for
loop can do is create a list of items:
my_list =
for i in other_list:
if condition_is_met:
my_list.append(i)
A list comprehension covers only this use case of a for
loop:
my_list = [i for i in other_list if condition_is_met]
That's why it's called a "list comprehension" and not an "inline for loop" - because it only creates lists. The other things you might use a for
loop for, like iterating a number, you can't directly use a list comprehension to do.
For your particular problem, you're trying to use k += 1
in a list comprehension. This operation doesn't return anything - it just modifies the variable k
- so when python tries to assign that to a list item, the operation fails. If you want to count up with k
, you should either just use a regular for
loop:
for i in aa:
if i != 2:
k += 1
or use the list comprehension to indirectly measure what you want:
k += len([i for i in aa if i != 2])
Here, we use a list comprehension to construct a list of every element i
in aa
such that i != 2
, then we take the number of elements in that list and add it to k
. Since this operation actually produces a list of its own, the code will not crash, and it will have the same overall effect. This solution isn't always doable if you have more complicated things you'd like to do in a for
loop - and it's slightly less efficient as well, because this solution requires actually creating the new list which isn't necessary for what you're trying to achieve.
answered Nov 13 '18 at 20:18
Green Cloak GuyGreen Cloak Guy
2,5031720
2,5031720
Very comprehensive answer! Much appreciated. May I ask a further question? Is it possible to realize "list comprehension" and iteration at the same time? E.g. To convert [True, False, False, False, True] to [0,3,4]. "0", and "4" represent the position, while "3" is the count of "False"
– Bridget Huang
Nov 13 '18 at 20:52
I don't think you'd be able to do that with a list comprehension. They're a useful tool, but it's not likefor
loops are to be avoided or anything, and for a task like that I would just use afor
loop (basically do a finite-state-machine thing or something)
– Green Cloak Guy
Nov 14 '18 at 2:47
Thanks for your answer!
– Bridget Huang
Nov 14 '18 at 16:21
add a comment |
Very comprehensive answer! Much appreciated. May I ask a further question? Is it possible to realize "list comprehension" and iteration at the same time? E.g. To convert [True, False, False, False, True] to [0,3,4]. "0", and "4" represent the position, while "3" is the count of "False"
– Bridget Huang
Nov 13 '18 at 20:52
I don't think you'd be able to do that with a list comprehension. They're a useful tool, but it's not likefor
loops are to be avoided or anything, and for a task like that I would just use afor
loop (basically do a finite-state-machine thing or something)
– Green Cloak Guy
Nov 14 '18 at 2:47
Thanks for your answer!
– Bridget Huang
Nov 14 '18 at 16:21
Very comprehensive answer! Much appreciated. May I ask a further question? Is it possible to realize "list comprehension" and iteration at the same time? E.g. To convert [True, False, False, False, True] to [0,3,4]. "0", and "4" represent the position, while "3" is the count of "False"
– Bridget Huang
Nov 13 '18 at 20:52
Very comprehensive answer! Much appreciated. May I ask a further question? Is it possible to realize "list comprehension" and iteration at the same time? E.g. To convert [True, False, False, False, True] to [0,3,4]. "0", and "4" represent the position, while "3" is the count of "False"
– Bridget Huang
Nov 13 '18 at 20:52
I don't think you'd be able to do that with a list comprehension. They're a useful tool, but it's not like
for
loops are to be avoided or anything, and for a task like that I would just use a for
loop (basically do a finite-state-machine thing or something)– Green Cloak Guy
Nov 14 '18 at 2:47
I don't think you'd be able to do that with a list comprehension. They're a useful tool, but it's not like
for
loops are to be avoided or anything, and for a task like that I would just use a for
loop (basically do a finite-state-machine thing or something)– Green Cloak Guy
Nov 14 '18 at 2:47
Thanks for your answer!
– Bridget Huang
Nov 14 '18 at 16:21
Thanks for your answer!
– Bridget Huang
Nov 14 '18 at 16:21
add a comment |
you can use len()
like so
print(len([i for i in a if i != 2]))
add a comment |
you can use len()
like so
print(len([i for i in a if i != 2]))
add a comment |
you can use len()
like so
print(len([i for i in a if i != 2]))
you can use len()
like so
print(len([i for i in a if i != 2]))
answered Nov 13 '18 at 20:11
vencaslacvencaslac
1,002217
1,002217
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%2f53288732%2fthere-is-no-counter-in-inline-python%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
7
That is not an "inline loop". List comprehensions are not a nifty way to write arbitrary
for
loops; they are for building lists.– user2357112
Nov 13 '18 at 20:11
count = sum([i != 2 for i in aa ] )
– Joran Beasley
Nov 13 '18 at 20:11
1
What were you even expecting
b
to be after that?– user2357112
Nov 13 '18 at 20:11
Hi @user2357112, ideally I'm expecting to get [2,1]. To count how many times "2" shows up in the list and keep the rest of the list as it was.
– Bridget Huang
Nov 13 '18 at 20:42