Create a 'dynamic list' during iteration in python
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Background
Let there be a set of integers
trialinteg = [231,355,112,1432,2434,5235,7896,7776,27421,42342]
Then it is possible to classify them into different equivalence classes modulo 6
Problem
Could we create an algorithm to classify all these integers into their respective equivalence class and store the results in a dictionary in python?
For example
d = "class0": [112,1432,..], "class1": [231,...], ...
More importantly, can we make d changes its size and names of the keys as the integer by which we define equivalence class (in this example, 6) changes?
Progress
It is possible to store all integers of equivalence class 0 modulo 6 in a list. But it is not clear how one can create a 'dynamic' dictionary that adjusts its size and names of the key when the integer in question changes (for example from 6 to 121).
moduloclasszero=
for num in trialinteg:
while num % 6 != 0:
print(f"num is not of class 0")
print(f"But num is of class num % 6")
print("now proceed to restore it to 0")
num = num + (6-(num % 6))
else:
print(f"num is of class 0")
moduloclasszero.append(num)
python integer modulo
add a comment |
Background
Let there be a set of integers
trialinteg = [231,355,112,1432,2434,5235,7896,7776,27421,42342]
Then it is possible to classify them into different equivalence classes modulo 6
Problem
Could we create an algorithm to classify all these integers into their respective equivalence class and store the results in a dictionary in python?
For example
d = "class0": [112,1432,..], "class1": [231,...], ...
More importantly, can we make d changes its size and names of the keys as the integer by which we define equivalence class (in this example, 6) changes?
Progress
It is possible to store all integers of equivalence class 0 modulo 6 in a list. But it is not clear how one can create a 'dynamic' dictionary that adjusts its size and names of the key when the integer in question changes (for example from 6 to 121).
moduloclasszero=
for num in trialinteg:
while num % 6 != 0:
print(f"num is not of class 0")
print(f"But num is of class num % 6")
print("now proceed to restore it to 0")
num = num + (6-(num % 6))
else:
print(f"num is of class 0")
moduloclasszero.append(num)
python integer modulo
add a comment |
Background
Let there be a set of integers
trialinteg = [231,355,112,1432,2434,5235,7896,7776,27421,42342]
Then it is possible to classify them into different equivalence classes modulo 6
Problem
Could we create an algorithm to classify all these integers into their respective equivalence class and store the results in a dictionary in python?
For example
d = "class0": [112,1432,..], "class1": [231,...], ...
More importantly, can we make d changes its size and names of the keys as the integer by which we define equivalence class (in this example, 6) changes?
Progress
It is possible to store all integers of equivalence class 0 modulo 6 in a list. But it is not clear how one can create a 'dynamic' dictionary that adjusts its size and names of the key when the integer in question changes (for example from 6 to 121).
moduloclasszero=
for num in trialinteg:
while num % 6 != 0:
print(f"num is not of class 0")
print(f"But num is of class num % 6")
print("now proceed to restore it to 0")
num = num + (6-(num % 6))
else:
print(f"num is of class 0")
moduloclasszero.append(num)
python integer modulo
Background
Let there be a set of integers
trialinteg = [231,355,112,1432,2434,5235,7896,7776,27421,42342]
Then it is possible to classify them into different equivalence classes modulo 6
Problem
Could we create an algorithm to classify all these integers into their respective equivalence class and store the results in a dictionary in python?
For example
d = "class0": [112,1432,..], "class1": [231,...], ...
More importantly, can we make d changes its size and names of the keys as the integer by which we define equivalence class (in this example, 6) changes?
Progress
It is possible to store all integers of equivalence class 0 modulo 6 in a list. But it is not clear how one can create a 'dynamic' dictionary that adjusts its size and names of the key when the integer in question changes (for example from 6 to 121).
moduloclasszero=
for num in trialinteg:
while num % 6 != 0:
print(f"num is not of class 0")
print(f"But num is of class num % 6")
print("now proceed to restore it to 0")
num = num + (6-(num % 6))
else:
print(f"num is of class 0")
moduloclasszero.append(num)
python integer modulo
python integer modulo
asked Nov 15 '18 at 17:20
hephaeshephaes
324
324
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You could use collections.defaultdict
:
from collections import defaultdict
trialinteg = [231,355,112,1432,2434,5235,7896,7776,27421,42342]
d = defaultdict(list)
for x in trialinteg:
d[f'classx % 6'].append(x)
print(d)
# defaultdict(<class 'list'>, 'class3': [231, 5235], 'class1': [355, 27421], 'class4': [112, 1432, 2434], 'class0': [7896, 7776, 42342])
add a comment |
Use the class value itself for your dictionary key.
my_mod = 6
for num in trialinteg:
d[num % my_mod].append(num)
I'll assume that you can already handle initializing the dict; if not, look at supporting questions on this site.
A dict comprehension can do this in a single assignment statement:
trial = [231,355,112,1432,2434,5235,7896,7776,27421,42342]
d = equi: [i for i in trial if i%my_mod == equi]
for equi in range(my_mod)
Resulting value of d:
0: [7896, 7776, 42342],
1: [355, 27421],
2: ,
3: [231, 5235],
4: [112, 1432, 2434],
5:
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%2f53324803%2fcreate-a-dynamic-list-during-iteration-in-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 could use collections.defaultdict
:
from collections import defaultdict
trialinteg = [231,355,112,1432,2434,5235,7896,7776,27421,42342]
d = defaultdict(list)
for x in trialinteg:
d[f'classx % 6'].append(x)
print(d)
# defaultdict(<class 'list'>, 'class3': [231, 5235], 'class1': [355, 27421], 'class4': [112, 1432, 2434], 'class0': [7896, 7776, 42342])
add a comment |
You could use collections.defaultdict
:
from collections import defaultdict
trialinteg = [231,355,112,1432,2434,5235,7896,7776,27421,42342]
d = defaultdict(list)
for x in trialinteg:
d[f'classx % 6'].append(x)
print(d)
# defaultdict(<class 'list'>, 'class3': [231, 5235], 'class1': [355, 27421], 'class4': [112, 1432, 2434], 'class0': [7896, 7776, 42342])
add a comment |
You could use collections.defaultdict
:
from collections import defaultdict
trialinteg = [231,355,112,1432,2434,5235,7896,7776,27421,42342]
d = defaultdict(list)
for x in trialinteg:
d[f'classx % 6'].append(x)
print(d)
# defaultdict(<class 'list'>, 'class3': [231, 5235], 'class1': [355, 27421], 'class4': [112, 1432, 2434], 'class0': [7896, 7776, 42342])
You could use collections.defaultdict
:
from collections import defaultdict
trialinteg = [231,355,112,1432,2434,5235,7896,7776,27421,42342]
d = defaultdict(list)
for x in trialinteg:
d[f'classx % 6'].append(x)
print(d)
# defaultdict(<class 'list'>, 'class3': [231, 5235], 'class1': [355, 27421], 'class4': [112, 1432, 2434], 'class0': [7896, 7776, 42342])
answered Nov 15 '18 at 17:26
AustinAustin
13.5k31031
13.5k31031
add a comment |
add a comment |
Use the class value itself for your dictionary key.
my_mod = 6
for num in trialinteg:
d[num % my_mod].append(num)
I'll assume that you can already handle initializing the dict; if not, look at supporting questions on this site.
A dict comprehension can do this in a single assignment statement:
trial = [231,355,112,1432,2434,5235,7896,7776,27421,42342]
d = equi: [i for i in trial if i%my_mod == equi]
for equi in range(my_mod)
Resulting value of d:
0: [7896, 7776, 42342],
1: [355, 27421],
2: ,
3: [231, 5235],
4: [112, 1432, 2434],
5:
add a comment |
Use the class value itself for your dictionary key.
my_mod = 6
for num in trialinteg:
d[num % my_mod].append(num)
I'll assume that you can already handle initializing the dict; if not, look at supporting questions on this site.
A dict comprehension can do this in a single assignment statement:
trial = [231,355,112,1432,2434,5235,7896,7776,27421,42342]
d = equi: [i for i in trial if i%my_mod == equi]
for equi in range(my_mod)
Resulting value of d:
0: [7896, 7776, 42342],
1: [355, 27421],
2: ,
3: [231, 5235],
4: [112, 1432, 2434],
5:
add a comment |
Use the class value itself for your dictionary key.
my_mod = 6
for num in trialinteg:
d[num % my_mod].append(num)
I'll assume that you can already handle initializing the dict; if not, look at supporting questions on this site.
A dict comprehension can do this in a single assignment statement:
trial = [231,355,112,1432,2434,5235,7896,7776,27421,42342]
d = equi: [i for i in trial if i%my_mod == equi]
for equi in range(my_mod)
Resulting value of d:
0: [7896, 7776, 42342],
1: [355, 27421],
2: ,
3: [231, 5235],
4: [112, 1432, 2434],
5:
Use the class value itself for your dictionary key.
my_mod = 6
for num in trialinteg:
d[num % my_mod].append(num)
I'll assume that you can already handle initializing the dict; if not, look at supporting questions on this site.
A dict comprehension can do this in a single assignment statement:
trial = [231,355,112,1432,2434,5235,7896,7776,27421,42342]
d = equi: [i for i in trial if i%my_mod == equi]
for equi in range(my_mod)
Resulting value of d:
0: [7896, 7776, 42342],
1: [355, 27421],
2: ,
3: [231, 5235],
4: [112, 1432, 2434],
5:
edited Nov 15 '18 at 17:34
answered Nov 15 '18 at 17:25
PrunePrune
46.2k143759
46.2k143759
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%2f53324803%2fcreate-a-dynamic-list-during-iteration-in-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