Not able to append elements in python list
I am writing a program, to find the character that occurs maximum number of odd times, in a given string using python. However i am not able to append characters to a list, if two or more characters occur maximum number of odd times.
input used : AAAbbccc
Error i am getting :
Traceback (most recent call last):
File "./prog.py", line 18, in
AttributeError: 'str' object has no attribute 'append'
inputString = input()
dict = {}
for i in inputString:
if i in dict:
dict[i] += 1
else:
dict[i] = 1
print(dict)
max = -1
lst =
for i in dict:
if(dict[i]%2!=0 and max<=dict[i]):
if(max == dict[i]):
lst.append(i)
else:
max = dict[i]
lst = i
print(lst)
python string python-3.x list counter
add a comment |
I am writing a program, to find the character that occurs maximum number of odd times, in a given string using python. However i am not able to append characters to a list, if two or more characters occur maximum number of odd times.
input used : AAAbbccc
Error i am getting :
Traceback (most recent call last):
File "./prog.py", line 18, in
AttributeError: 'str' object has no attribute 'append'
inputString = input()
dict = {}
for i in inputString:
if i in dict:
dict[i] += 1
else:
dict[i] = 1
print(dict)
max = -1
lst =
for i in dict:
if(dict[i]%2!=0 and max<=dict[i]):
if(max == dict[i]):
lst.append(i)
else:
max = dict[i]
lst = i
print(lst)
python string python-3.x list counter
2
In line 21 you have an assignment 'lst' to 'i' which is not an array, then when when line 18 occurs lst is not an array anymore so it doesn't have an append method
– szogoon
Nov 14 '18 at 11:57
Thanks but that's not the issue that's working completely fine i'm getting error for line " lst.append(i) " where i am trying to append character into the list but i'm getting the above mentioned error
– Anand
Nov 14 '18 at 12:01
add a comment |
I am writing a program, to find the character that occurs maximum number of odd times, in a given string using python. However i am not able to append characters to a list, if two or more characters occur maximum number of odd times.
input used : AAAbbccc
Error i am getting :
Traceback (most recent call last):
File "./prog.py", line 18, in
AttributeError: 'str' object has no attribute 'append'
inputString = input()
dict = {}
for i in inputString:
if i in dict:
dict[i] += 1
else:
dict[i] = 1
print(dict)
max = -1
lst =
for i in dict:
if(dict[i]%2!=0 and max<=dict[i]):
if(max == dict[i]):
lst.append(i)
else:
max = dict[i]
lst = i
print(lst)
python string python-3.x list counter
I am writing a program, to find the character that occurs maximum number of odd times, in a given string using python. However i am not able to append characters to a list, if two or more characters occur maximum number of odd times.
input used : AAAbbccc
Error i am getting :
Traceback (most recent call last):
File "./prog.py", line 18, in
AttributeError: 'str' object has no attribute 'append'
inputString = input()
dict = {}
for i in inputString:
if i in dict:
dict[i] += 1
else:
dict[i] = 1
print(dict)
max = -1
lst =
for i in dict:
if(dict[i]%2!=0 and max<=dict[i]):
if(max == dict[i]):
lst.append(i)
else:
max = dict[i]
lst = i
print(lst)
python string python-3.x list counter
python string python-3.x list counter
edited Nov 14 '18 at 12:39
ctrl-alt-delor
4,24632444
4,24632444
asked Nov 14 '18 at 11:52
AnandAnand
226
226
2
In line 21 you have an assignment 'lst' to 'i' which is not an array, then when when line 18 occurs lst is not an array anymore so it doesn't have an append method
– szogoon
Nov 14 '18 at 11:57
Thanks but that's not the issue that's working completely fine i'm getting error for line " lst.append(i) " where i am trying to append character into the list but i'm getting the above mentioned error
– Anand
Nov 14 '18 at 12:01
add a comment |
2
In line 21 you have an assignment 'lst' to 'i' which is not an array, then when when line 18 occurs lst is not an array anymore so it doesn't have an append method
– szogoon
Nov 14 '18 at 11:57
Thanks but that's not the issue that's working completely fine i'm getting error for line " lst.append(i) " where i am trying to append character into the list but i'm getting the above mentioned error
– Anand
Nov 14 '18 at 12:01
2
2
In line 21 you have an assignment 'lst' to 'i' which is not an array, then when when line 18 occurs lst is not an array anymore so it doesn't have an append method
– szogoon
Nov 14 '18 at 11:57
In line 21 you have an assignment 'lst' to 'i' which is not an array, then when when line 18 occurs lst is not an array anymore so it doesn't have an append method
– szogoon
Nov 14 '18 at 11:57
Thanks but that's not the issue that's working completely fine i'm getting error for line " lst.append(i) " where i am trying to append character into the list but i'm getting the above mentioned error
– Anand
Nov 14 '18 at 12:01
Thanks but that's not the issue that's working completely fine i'm getting error for line " lst.append(i) " where i am trying to append character into the list but i'm getting the above mentioned error
– Anand
Nov 14 '18 at 12:01
add a comment |
1 Answer
1
active
oldest
votes
There are some issues with your code:
- Don't name variables after built-ins (even as an example). Use
d
ordict_
instead ofdict
. Dittomax
. - Your
max
(fixed at-1
) will always be<= dict[i]
, since counts are always>= 1
. - You define
lst
as a list, then assign a string to it.
Much simpler, use collections.Counter
, calculate the maximum value, then use max
with a custom function:
from collections import Counter
inputString = input()
c = Counter(inputString)
print(c)
maxval = max(c.values())
def max_logic(x):
cond1 = x[1] % 2
cond2 = x[1] - maxval
return cond1, cond2
key, val = max(c.items(), key=max_logic)
Example run:
print(key, val)
thisisateststring
Counter({'t': 4, 's': 4, 'i': 3, 'h': 1, 'a': 1, 'e': 1, 'r': 1, 'n': 1, 'g': 1})
i 3
The solution assumes a valid odd count does exist in your string. If it doesn't and you need to apply special treatment, you'll need to add additional logic. I leave that as an exercise.
1
Thanks a lot...
– Anand
Nov 14 '18 at 12:16
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%2f53299632%2fnot-able-to-append-elements-in-python-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are some issues with your code:
- Don't name variables after built-ins (even as an example). Use
d
ordict_
instead ofdict
. Dittomax
. - Your
max
(fixed at-1
) will always be<= dict[i]
, since counts are always>= 1
. - You define
lst
as a list, then assign a string to it.
Much simpler, use collections.Counter
, calculate the maximum value, then use max
with a custom function:
from collections import Counter
inputString = input()
c = Counter(inputString)
print(c)
maxval = max(c.values())
def max_logic(x):
cond1 = x[1] % 2
cond2 = x[1] - maxval
return cond1, cond2
key, val = max(c.items(), key=max_logic)
Example run:
print(key, val)
thisisateststring
Counter({'t': 4, 's': 4, 'i': 3, 'h': 1, 'a': 1, 'e': 1, 'r': 1, 'n': 1, 'g': 1})
i 3
The solution assumes a valid odd count does exist in your string. If it doesn't and you need to apply special treatment, you'll need to add additional logic. I leave that as an exercise.
1
Thanks a lot...
– Anand
Nov 14 '18 at 12:16
add a comment |
There are some issues with your code:
- Don't name variables after built-ins (even as an example). Use
d
ordict_
instead ofdict
. Dittomax
. - Your
max
(fixed at-1
) will always be<= dict[i]
, since counts are always>= 1
. - You define
lst
as a list, then assign a string to it.
Much simpler, use collections.Counter
, calculate the maximum value, then use max
with a custom function:
from collections import Counter
inputString = input()
c = Counter(inputString)
print(c)
maxval = max(c.values())
def max_logic(x):
cond1 = x[1] % 2
cond2 = x[1] - maxval
return cond1, cond2
key, val = max(c.items(), key=max_logic)
Example run:
print(key, val)
thisisateststring
Counter({'t': 4, 's': 4, 'i': 3, 'h': 1, 'a': 1, 'e': 1, 'r': 1, 'n': 1, 'g': 1})
i 3
The solution assumes a valid odd count does exist in your string. If it doesn't and you need to apply special treatment, you'll need to add additional logic. I leave that as an exercise.
1
Thanks a lot...
– Anand
Nov 14 '18 at 12:16
add a comment |
There are some issues with your code:
- Don't name variables after built-ins (even as an example). Use
d
ordict_
instead ofdict
. Dittomax
. - Your
max
(fixed at-1
) will always be<= dict[i]
, since counts are always>= 1
. - You define
lst
as a list, then assign a string to it.
Much simpler, use collections.Counter
, calculate the maximum value, then use max
with a custom function:
from collections import Counter
inputString = input()
c = Counter(inputString)
print(c)
maxval = max(c.values())
def max_logic(x):
cond1 = x[1] % 2
cond2 = x[1] - maxval
return cond1, cond2
key, val = max(c.items(), key=max_logic)
Example run:
print(key, val)
thisisateststring
Counter({'t': 4, 's': 4, 'i': 3, 'h': 1, 'a': 1, 'e': 1, 'r': 1, 'n': 1, 'g': 1})
i 3
The solution assumes a valid odd count does exist in your string. If it doesn't and you need to apply special treatment, you'll need to add additional logic. I leave that as an exercise.
There are some issues with your code:
- Don't name variables after built-ins (even as an example). Use
d
ordict_
instead ofdict
. Dittomax
. - Your
max
(fixed at-1
) will always be<= dict[i]
, since counts are always>= 1
. - You define
lst
as a list, then assign a string to it.
Much simpler, use collections.Counter
, calculate the maximum value, then use max
with a custom function:
from collections import Counter
inputString = input()
c = Counter(inputString)
print(c)
maxval = max(c.values())
def max_logic(x):
cond1 = x[1] % 2
cond2 = x[1] - maxval
return cond1, cond2
key, val = max(c.items(), key=max_logic)
Example run:
print(key, val)
thisisateststring
Counter({'t': 4, 's': 4, 'i': 3, 'h': 1, 'a': 1, 'e': 1, 'r': 1, 'n': 1, 'g': 1})
i 3
The solution assumes a valid odd count does exist in your string. If it doesn't and you need to apply special treatment, you'll need to add additional logic. I leave that as an exercise.
edited Nov 14 '18 at 12:17
answered Nov 14 '18 at 12:11
jppjpp
100k2162111
100k2162111
1
Thanks a lot...
– Anand
Nov 14 '18 at 12:16
add a comment |
1
Thanks a lot...
– Anand
Nov 14 '18 at 12:16
1
1
Thanks a lot...
– Anand
Nov 14 '18 at 12:16
Thanks a lot...
– Anand
Nov 14 '18 at 12:16
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%2f53299632%2fnot-able-to-append-elements-in-python-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
2
In line 21 you have an assignment 'lst' to 'i' which is not an array, then when when line 18 occurs lst is not an array anymore so it doesn't have an append method
– szogoon
Nov 14 '18 at 11:57
Thanks but that's not the issue that's working completely fine i'm getting error for line " lst.append(i) " where i am trying to append character into the list but i'm getting the above mentioned error
– Anand
Nov 14 '18 at 12:01