We have nested list create dictionary key as word list and the number of times they occur as valuesHow do I sort a list of dictionaries by a value of the dictionary?Getting key with maximum value in dictionary?Create a dictionary with list comprehension in PythonGet key by value in dictionaryPython - stringsHow to return dictionary keys as a list in Python?Codeeval Challenge not returning correct output. (Python)Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?Why is [] faster than list()?ValueError: I/O operation on closed file.
Adding empty element to declared container without declaring type of element
Could solar power be utilized and substitute coal in the 19th century?
How to prevent YouTube from showing already watched videos?
What should I use for Mishna study?
Simulating a probability of 1 of 2^N with less than N random bits
How to deal with or prevent idle in the test team?
Should my PhD thesis be submitted under my legal name?
Resetting two CD4017 counters simultaneously, only one resets
Books on the History of math research at European universities
Invariance of results when scaling explanatory variables in logistic regression, is there a proof?
Are taller landing gear bad for aircraft, particulary large airliners?
A social experiment. What is the worst that can happen?
Simple recursive Sudoku solver
Can I rely on these GitHub repository files?
Freedom of speech and where it applies
Can I use my Chinese passport to enter China after I acquired another citizenship?
Is it okay / does it make sense for another player to join a running game of Munchkin?
Can I Retrieve Email Addresses from BCC?
Java - What do constructor type arguments mean when placed *before* the type?
Can a malicious addon access internet history and such in chrome/firefox?
My boss asked me to take a one-day class, then signs it up as a day off
A workplace installs custom certificates on personal devices, can this be used to decrypt HTTPS traffic?
Have I saved too much for retirement so far?
Can a Bard use an arcane focus?
We have nested list create dictionary key as word list and the number of times they occur as values
How do I sort a list of dictionaries by a value of the dictionary?Getting key with maximum value in dictionary?Create a dictionary with list comprehension in PythonGet key by value in dictionaryPython - stringsHow to return dictionary keys as a list in Python?Codeeval Challenge not returning correct output. (Python)Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?Why is [] faster than list()?ValueError: I/O operation on closed file.
Actual Code is :
big_list = [[['one', 'two'], ['seven', 'eight']], [['nine', 'four'], ['three', 'one']], [['two', 'eight'], ['seven', 'four']], [['five', 'one'], ['four', 'two']], [['six', 'eight'], ['two', 'seven']], [['three', 'five'], ['one', 'six']], [['nine', 'eight'], ['five', 'four']], [['six', 'three'], ['four', 'seven']]]
word_counts =
for n in big_list:
for i in big_list:
if n == i:
word_counts[i] = word_counts[i] + 1
print(word_counts)
Error is :
unhashable type: 'list' on line 8
Expected result:
['one':3 , 'two': 5, 'three': 4 ,.....] like that
So please help me find correct solution
python python-2.7
add a comment |
Actual Code is :
big_list = [[['one', 'two'], ['seven', 'eight']], [['nine', 'four'], ['three', 'one']], [['two', 'eight'], ['seven', 'four']], [['five', 'one'], ['four', 'two']], [['six', 'eight'], ['two', 'seven']], [['three', 'five'], ['one', 'six']], [['nine', 'eight'], ['five', 'four']], [['six', 'three'], ['four', 'seven']]]
word_counts =
for n in big_list:
for i in big_list:
if n == i:
word_counts[i] = word_counts[i] + 1
print(word_counts)
Error is :
unhashable type: 'list' on line 8
Expected result:
['one':3 , 'two': 5, 'three': 4 ,.....] like that
So please help me find correct solution
python python-2.7
add a comment |
Actual Code is :
big_list = [[['one', 'two'], ['seven', 'eight']], [['nine', 'four'], ['three', 'one']], [['two', 'eight'], ['seven', 'four']], [['five', 'one'], ['four', 'two']], [['six', 'eight'], ['two', 'seven']], [['three', 'five'], ['one', 'six']], [['nine', 'eight'], ['five', 'four']], [['six', 'three'], ['four', 'seven']]]
word_counts =
for n in big_list:
for i in big_list:
if n == i:
word_counts[i] = word_counts[i] + 1
print(word_counts)
Error is :
unhashable type: 'list' on line 8
Expected result:
['one':3 , 'two': 5, 'three': 4 ,.....] like that
So please help me find correct solution
python python-2.7
Actual Code is :
big_list = [[['one', 'two'], ['seven', 'eight']], [['nine', 'four'], ['three', 'one']], [['two', 'eight'], ['seven', 'four']], [['five', 'one'], ['four', 'two']], [['six', 'eight'], ['two', 'seven']], [['three', 'five'], ['one', 'six']], [['nine', 'eight'], ['five', 'four']], [['six', 'three'], ['four', 'seven']]]
word_counts =
for n in big_list:
for i in big_list:
if n == i:
word_counts[i] = word_counts[i] + 1
print(word_counts)
Error is :
unhashable type: 'list' on line 8
Expected result:
['one':3 , 'two': 5, 'three': 4 ,.....] like that
So please help me find correct solution
python python-2.7
python python-2.7
edited Mar 7 at 10:36
DilbertFan
12017
12017
asked Mar 7 at 10:12
Mahendra SeerviMahendra Seervi
66
66
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can use itertools.chain in order to flatten the nested list and collections.Counter to count how many times each element occurs:
from collections import Counter
from itertools import chain
Counter(chain(*chain(*big_list)))
Output
Counter('four': 5, 'one': 4, 'two': 4, 'seven': 4, 'eight': 4, 'three': 3,
'five': 3, 'six': 3, 'nine': 2)
For a solution without imports you can do something like:
d =
for i in big_list:
for j in i:
for k in j:
if not d.get(k):
d[k] = 1
else:
d[k] += 1
print(d)
# 'one': 4, 'two': 4, 'seven': 4, 'eight': 4, 'nine': 2,
# 'four': 5, 'three': 3, 'five': 3, 'six': 3
can we do without using module/libaray??
– Mahendra Seervi
Mar 7 at 10:35
And thanks for that
– Mahendra Seervi
Mar 7 at 10:35
Let me post another alternative
– yatu
Mar 7 at 10:38
thanks @yatu done
– Mahendra Seervi
Mar 7 at 10:49
Don't forget to accept @MahendraSeervi, thanks!
– yatu
Mar 7 at 10:49
|
show 3 more comments
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%2f55041177%2fwe-have-nested-list-create-dictionary-key-as-word-list-and-the-number-of-times-t%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
You can use itertools.chain in order to flatten the nested list and collections.Counter to count how many times each element occurs:
from collections import Counter
from itertools import chain
Counter(chain(*chain(*big_list)))
Output
Counter('four': 5, 'one': 4, 'two': 4, 'seven': 4, 'eight': 4, 'three': 3,
'five': 3, 'six': 3, 'nine': 2)
For a solution without imports you can do something like:
d =
for i in big_list:
for j in i:
for k in j:
if not d.get(k):
d[k] = 1
else:
d[k] += 1
print(d)
# 'one': 4, 'two': 4, 'seven': 4, 'eight': 4, 'nine': 2,
# 'four': 5, 'three': 3, 'five': 3, 'six': 3
can we do without using module/libaray??
– Mahendra Seervi
Mar 7 at 10:35
And thanks for that
– Mahendra Seervi
Mar 7 at 10:35
Let me post another alternative
– yatu
Mar 7 at 10:38
thanks @yatu done
– Mahendra Seervi
Mar 7 at 10:49
Don't forget to accept @MahendraSeervi, thanks!
– yatu
Mar 7 at 10:49
|
show 3 more comments
You can use itertools.chain in order to flatten the nested list and collections.Counter to count how many times each element occurs:
from collections import Counter
from itertools import chain
Counter(chain(*chain(*big_list)))
Output
Counter('four': 5, 'one': 4, 'two': 4, 'seven': 4, 'eight': 4, 'three': 3,
'five': 3, 'six': 3, 'nine': 2)
For a solution without imports you can do something like:
d =
for i in big_list:
for j in i:
for k in j:
if not d.get(k):
d[k] = 1
else:
d[k] += 1
print(d)
# 'one': 4, 'two': 4, 'seven': 4, 'eight': 4, 'nine': 2,
# 'four': 5, 'three': 3, 'five': 3, 'six': 3
can we do without using module/libaray??
– Mahendra Seervi
Mar 7 at 10:35
And thanks for that
– Mahendra Seervi
Mar 7 at 10:35
Let me post another alternative
– yatu
Mar 7 at 10:38
thanks @yatu done
– Mahendra Seervi
Mar 7 at 10:49
Don't forget to accept @MahendraSeervi, thanks!
– yatu
Mar 7 at 10:49
|
show 3 more comments
You can use itertools.chain in order to flatten the nested list and collections.Counter to count how many times each element occurs:
from collections import Counter
from itertools import chain
Counter(chain(*chain(*big_list)))
Output
Counter('four': 5, 'one': 4, 'two': 4, 'seven': 4, 'eight': 4, 'three': 3,
'five': 3, 'six': 3, 'nine': 2)
For a solution without imports you can do something like:
d =
for i in big_list:
for j in i:
for k in j:
if not d.get(k):
d[k] = 1
else:
d[k] += 1
print(d)
# 'one': 4, 'two': 4, 'seven': 4, 'eight': 4, 'nine': 2,
# 'four': 5, 'three': 3, 'five': 3, 'six': 3
You can use itertools.chain in order to flatten the nested list and collections.Counter to count how many times each element occurs:
from collections import Counter
from itertools import chain
Counter(chain(*chain(*big_list)))
Output
Counter('four': 5, 'one': 4, 'two': 4, 'seven': 4, 'eight': 4, 'three': 3,
'five': 3, 'six': 3, 'nine': 2)
For a solution without imports you can do something like:
d =
for i in big_list:
for j in i:
for k in j:
if not d.get(k):
d[k] = 1
else:
d[k] += 1
print(d)
# 'one': 4, 'two': 4, 'seven': 4, 'eight': 4, 'nine': 2,
# 'four': 5, 'three': 3, 'five': 3, 'six': 3
edited Mar 7 at 10:44
answered Mar 7 at 10:15
yatuyatu
14.6k41542
14.6k41542
can we do without using module/libaray??
– Mahendra Seervi
Mar 7 at 10:35
And thanks for that
– Mahendra Seervi
Mar 7 at 10:35
Let me post another alternative
– yatu
Mar 7 at 10:38
thanks @yatu done
– Mahendra Seervi
Mar 7 at 10:49
Don't forget to accept @MahendraSeervi, thanks!
– yatu
Mar 7 at 10:49
|
show 3 more comments
can we do without using module/libaray??
– Mahendra Seervi
Mar 7 at 10:35
And thanks for that
– Mahendra Seervi
Mar 7 at 10:35
Let me post another alternative
– yatu
Mar 7 at 10:38
thanks @yatu done
– Mahendra Seervi
Mar 7 at 10:49
Don't forget to accept @MahendraSeervi, thanks!
– yatu
Mar 7 at 10:49
can we do without using module/libaray??
– Mahendra Seervi
Mar 7 at 10:35
can we do without using module/libaray??
– Mahendra Seervi
Mar 7 at 10:35
And thanks for that
– Mahendra Seervi
Mar 7 at 10:35
And thanks for that
– Mahendra Seervi
Mar 7 at 10:35
Let me post another alternative
– yatu
Mar 7 at 10:38
Let me post another alternative
– yatu
Mar 7 at 10:38
thanks @yatu done
– Mahendra Seervi
Mar 7 at 10:49
thanks @yatu done
– Mahendra Seervi
Mar 7 at 10:49
Don't forget to accept @MahendraSeervi, thanks!
– yatu
Mar 7 at 10:49
Don't forget to accept @MahendraSeervi, thanks!
– yatu
Mar 7 at 10:49
|
show 3 more comments
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%2f55041177%2fwe-have-nested-list-create-dictionary-key-as-word-list-and-the-number-of-times-t%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