Looping through a linked list containing arrays in python
i'm struggling to work out how to use a loop to run through a list which contains 4 array values being "a_value", "b_value", "c_value", "d_value". Each of my arrays in this list have 23 elements. the outcome im looking for is for a loop to run though the list adding all 23 elements of each array. E.G. total of "a_value" then total of "b_value" and so on. this is what i have so far.
Thank you guys.
class node():
def __init__(self, dataval = None):
self.dataval = dataval
self.nextval = None
class linked_list():
def __init__(self):
self.headval = None
def listprint(self):
printval = self.headval
while printval != None:
print (printval.dataval)
printval = printval.nextval
arraylist = linked_list()
arraylist.headval = node(a_value) # each '""_value' has 23 integer values
in them
e2 = node(b_value)
e3 = node(c_value)
e4 = node(d_value)
arraylist.headval.nextval = e2
e2.nextval = e3
e3.nextval = e4
arraylist.listprint()
i = 1
x = 1
total_a = 0
total_b = 0
total_c = 0
total_d = 0
for n in arraylist:
for z in range(len(n)-1)
value_a = int(a_value[i])
total_a+=value_a
i+= 1
x+=1
python arrays linked-list nested-loops
add a comment |
i'm struggling to work out how to use a loop to run through a list which contains 4 array values being "a_value", "b_value", "c_value", "d_value". Each of my arrays in this list have 23 elements. the outcome im looking for is for a loop to run though the list adding all 23 elements of each array. E.G. total of "a_value" then total of "b_value" and so on. this is what i have so far.
Thank you guys.
class node():
def __init__(self, dataval = None):
self.dataval = dataval
self.nextval = None
class linked_list():
def __init__(self):
self.headval = None
def listprint(self):
printval = self.headval
while printval != None:
print (printval.dataval)
printval = printval.nextval
arraylist = linked_list()
arraylist.headval = node(a_value) # each '""_value' has 23 integer values
in them
e2 = node(b_value)
e3 = node(c_value)
e4 = node(d_value)
arraylist.headval.nextval = e2
e2.nextval = e3
e3.nextval = e4
arraylist.listprint()
i = 1
x = 1
total_a = 0
total_b = 0
total_c = 0
total_d = 0
for n in arraylist:
for z in range(len(n)-1)
value_a = int(a_value[i])
total_a+=value_a
i+= 1
x+=1
python arrays linked-list nested-loops
What is your input and expected output?
– Alex
Nov 14 '18 at 14:15
Did it help you?
– NoorJafri
Nov 14 '18 at 14:36
add a comment |
i'm struggling to work out how to use a loop to run through a list which contains 4 array values being "a_value", "b_value", "c_value", "d_value". Each of my arrays in this list have 23 elements. the outcome im looking for is for a loop to run though the list adding all 23 elements of each array. E.G. total of "a_value" then total of "b_value" and so on. this is what i have so far.
Thank you guys.
class node():
def __init__(self, dataval = None):
self.dataval = dataval
self.nextval = None
class linked_list():
def __init__(self):
self.headval = None
def listprint(self):
printval = self.headval
while printval != None:
print (printval.dataval)
printval = printval.nextval
arraylist = linked_list()
arraylist.headval = node(a_value) # each '""_value' has 23 integer values
in them
e2 = node(b_value)
e3 = node(c_value)
e4 = node(d_value)
arraylist.headval.nextval = e2
e2.nextval = e3
e3.nextval = e4
arraylist.listprint()
i = 1
x = 1
total_a = 0
total_b = 0
total_c = 0
total_d = 0
for n in arraylist:
for z in range(len(n)-1)
value_a = int(a_value[i])
total_a+=value_a
i+= 1
x+=1
python arrays linked-list nested-loops
i'm struggling to work out how to use a loop to run through a list which contains 4 array values being "a_value", "b_value", "c_value", "d_value". Each of my arrays in this list have 23 elements. the outcome im looking for is for a loop to run though the list adding all 23 elements of each array. E.G. total of "a_value" then total of "b_value" and so on. this is what i have so far.
Thank you guys.
class node():
def __init__(self, dataval = None):
self.dataval = dataval
self.nextval = None
class linked_list():
def __init__(self):
self.headval = None
def listprint(self):
printval = self.headval
while printval != None:
print (printval.dataval)
printval = printval.nextval
arraylist = linked_list()
arraylist.headval = node(a_value) # each '""_value' has 23 integer values
in them
e2 = node(b_value)
e3 = node(c_value)
e4 = node(d_value)
arraylist.headval.nextval = e2
e2.nextval = e3
e3.nextval = e4
arraylist.listprint()
i = 1
x = 1
total_a = 0
total_b = 0
total_c = 0
total_d = 0
for n in arraylist:
for z in range(len(n)-1)
value_a = int(a_value[i])
total_a+=value_a
i+= 1
x+=1
python arrays linked-list nested-loops
python arrays linked-list nested-loops
edited Nov 14 '18 at 16:54
NoorJafri
583312
583312
asked Nov 14 '18 at 14:08
Chris DayChris Day
63
63
What is your input and expected output?
– Alex
Nov 14 '18 at 14:15
Did it help you?
– NoorJafri
Nov 14 '18 at 14:36
add a comment |
What is your input and expected output?
– Alex
Nov 14 '18 at 14:15
Did it help you?
– NoorJafri
Nov 14 '18 at 14:36
What is your input and expected output?
– Alex
Nov 14 '18 at 14:15
What is your input and expected output?
– Alex
Nov 14 '18 at 14:15
Did it help you?
– NoorJafri
Nov 14 '18 at 14:36
Did it help you?
– NoorJafri
Nov 14 '18 at 14:36
add a comment |
1 Answer
1
active
oldest
votes
you can try something like this:
a_value = [1,2,3]
b_value = [4,5,6]
c_value = [7,8,9]
d_value = [10,11,12]
class node():
def __init__(self, dataval = None):
self.dataval = dataval
self.nextval = None
class linked_list():
def __init__(self):
self.headval = None
def listprint(self):
printval = self.headval
while printval != None:
print (printval.dataval)
printval = printval.nextval
arraylist = linked_list()
arraylist.headval = node(a_value)
e2 = node(b_value)
e3 = node(c_value)
e4 = node(d_value)
arraylist.headval.nextval = e2
e2.nextval = e3
e3.nextval = e4
node = arraylist.headval # fetch head node
nextval = node.nextval # fetch next to node
list =
list.append(node.dataval) # enter all head list
while nextval: # this loop will break as nextval is none
node = node.nextval # node now becomes next and repeat
if hasattr(node, 'nextval'):
nextval = node.nextval
else:
nextval = None
list.append(node.dataval)
sum = 0
for x in list: # sum in the end or break into 23 for all separate
for y in x:
sum+=y
print(list, sum)
#OUTPUT [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] 78
Works fine :)
(nextval = node.nextval) is getting error " 'Nonetype' object has no attribute 'nextval' "
– Chris Day
Nov 14 '18 at 15:29
Just add a check before fetching the value.
– NoorJafri
Nov 14 '18 at 15:32
what kind of check would you use?
– Chris Day
Nov 14 '18 at 15:54
great! thank you buddy! just one last thing as im curious, is there a way to get it so it would print "[[6],[15],[24],[33]]" instead Thanks :)
– Chris Day
Nov 14 '18 at 18:44
You have to play around that :D learn guy and accept answer if it helped you :p
– NoorJafri
Nov 14 '18 at 19:23
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%2f53302144%2flooping-through-a-linked-list-containing-arrays-in-python%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 try something like this:
a_value = [1,2,3]
b_value = [4,5,6]
c_value = [7,8,9]
d_value = [10,11,12]
class node():
def __init__(self, dataval = None):
self.dataval = dataval
self.nextval = None
class linked_list():
def __init__(self):
self.headval = None
def listprint(self):
printval = self.headval
while printval != None:
print (printval.dataval)
printval = printval.nextval
arraylist = linked_list()
arraylist.headval = node(a_value)
e2 = node(b_value)
e3 = node(c_value)
e4 = node(d_value)
arraylist.headval.nextval = e2
e2.nextval = e3
e3.nextval = e4
node = arraylist.headval # fetch head node
nextval = node.nextval # fetch next to node
list =
list.append(node.dataval) # enter all head list
while nextval: # this loop will break as nextval is none
node = node.nextval # node now becomes next and repeat
if hasattr(node, 'nextval'):
nextval = node.nextval
else:
nextval = None
list.append(node.dataval)
sum = 0
for x in list: # sum in the end or break into 23 for all separate
for y in x:
sum+=y
print(list, sum)
#OUTPUT [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] 78
Works fine :)
(nextval = node.nextval) is getting error " 'Nonetype' object has no attribute 'nextval' "
– Chris Day
Nov 14 '18 at 15:29
Just add a check before fetching the value.
– NoorJafri
Nov 14 '18 at 15:32
what kind of check would you use?
– Chris Day
Nov 14 '18 at 15:54
great! thank you buddy! just one last thing as im curious, is there a way to get it so it would print "[[6],[15],[24],[33]]" instead Thanks :)
– Chris Day
Nov 14 '18 at 18:44
You have to play around that :D learn guy and accept answer if it helped you :p
– NoorJafri
Nov 14 '18 at 19:23
add a comment |
you can try something like this:
a_value = [1,2,3]
b_value = [4,5,6]
c_value = [7,8,9]
d_value = [10,11,12]
class node():
def __init__(self, dataval = None):
self.dataval = dataval
self.nextval = None
class linked_list():
def __init__(self):
self.headval = None
def listprint(self):
printval = self.headval
while printval != None:
print (printval.dataval)
printval = printval.nextval
arraylist = linked_list()
arraylist.headval = node(a_value)
e2 = node(b_value)
e3 = node(c_value)
e4 = node(d_value)
arraylist.headval.nextval = e2
e2.nextval = e3
e3.nextval = e4
node = arraylist.headval # fetch head node
nextval = node.nextval # fetch next to node
list =
list.append(node.dataval) # enter all head list
while nextval: # this loop will break as nextval is none
node = node.nextval # node now becomes next and repeat
if hasattr(node, 'nextval'):
nextval = node.nextval
else:
nextval = None
list.append(node.dataval)
sum = 0
for x in list: # sum in the end or break into 23 for all separate
for y in x:
sum+=y
print(list, sum)
#OUTPUT [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] 78
Works fine :)
(nextval = node.nextval) is getting error " 'Nonetype' object has no attribute 'nextval' "
– Chris Day
Nov 14 '18 at 15:29
Just add a check before fetching the value.
– NoorJafri
Nov 14 '18 at 15:32
what kind of check would you use?
– Chris Day
Nov 14 '18 at 15:54
great! thank you buddy! just one last thing as im curious, is there a way to get it so it would print "[[6],[15],[24],[33]]" instead Thanks :)
– Chris Day
Nov 14 '18 at 18:44
You have to play around that :D learn guy and accept answer if it helped you :p
– NoorJafri
Nov 14 '18 at 19:23
add a comment |
you can try something like this:
a_value = [1,2,3]
b_value = [4,5,6]
c_value = [7,8,9]
d_value = [10,11,12]
class node():
def __init__(self, dataval = None):
self.dataval = dataval
self.nextval = None
class linked_list():
def __init__(self):
self.headval = None
def listprint(self):
printval = self.headval
while printval != None:
print (printval.dataval)
printval = printval.nextval
arraylist = linked_list()
arraylist.headval = node(a_value)
e2 = node(b_value)
e3 = node(c_value)
e4 = node(d_value)
arraylist.headval.nextval = e2
e2.nextval = e3
e3.nextval = e4
node = arraylist.headval # fetch head node
nextval = node.nextval # fetch next to node
list =
list.append(node.dataval) # enter all head list
while nextval: # this loop will break as nextval is none
node = node.nextval # node now becomes next and repeat
if hasattr(node, 'nextval'):
nextval = node.nextval
else:
nextval = None
list.append(node.dataval)
sum = 0
for x in list: # sum in the end or break into 23 for all separate
for y in x:
sum+=y
print(list, sum)
#OUTPUT [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] 78
Works fine :)
you can try something like this:
a_value = [1,2,3]
b_value = [4,5,6]
c_value = [7,8,9]
d_value = [10,11,12]
class node():
def __init__(self, dataval = None):
self.dataval = dataval
self.nextval = None
class linked_list():
def __init__(self):
self.headval = None
def listprint(self):
printval = self.headval
while printval != None:
print (printval.dataval)
printval = printval.nextval
arraylist = linked_list()
arraylist.headval = node(a_value)
e2 = node(b_value)
e3 = node(c_value)
e4 = node(d_value)
arraylist.headval.nextval = e2
e2.nextval = e3
e3.nextval = e4
node = arraylist.headval # fetch head node
nextval = node.nextval # fetch next to node
list =
list.append(node.dataval) # enter all head list
while nextval: # this loop will break as nextval is none
node = node.nextval # node now becomes next and repeat
if hasattr(node, 'nextval'):
nextval = node.nextval
else:
nextval = None
list.append(node.dataval)
sum = 0
for x in list: # sum in the end or break into 23 for all separate
for y in x:
sum+=y
print(list, sum)
#OUTPUT [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] 78
Works fine :)
edited Nov 14 '18 at 16:44
answered Nov 14 '18 at 14:18
NoorJafriNoorJafri
583312
583312
(nextval = node.nextval) is getting error " 'Nonetype' object has no attribute 'nextval' "
– Chris Day
Nov 14 '18 at 15:29
Just add a check before fetching the value.
– NoorJafri
Nov 14 '18 at 15:32
what kind of check would you use?
– Chris Day
Nov 14 '18 at 15:54
great! thank you buddy! just one last thing as im curious, is there a way to get it so it would print "[[6],[15],[24],[33]]" instead Thanks :)
– Chris Day
Nov 14 '18 at 18:44
You have to play around that :D learn guy and accept answer if it helped you :p
– NoorJafri
Nov 14 '18 at 19:23
add a comment |
(nextval = node.nextval) is getting error " 'Nonetype' object has no attribute 'nextval' "
– Chris Day
Nov 14 '18 at 15:29
Just add a check before fetching the value.
– NoorJafri
Nov 14 '18 at 15:32
what kind of check would you use?
– Chris Day
Nov 14 '18 at 15:54
great! thank you buddy! just one last thing as im curious, is there a way to get it so it would print "[[6],[15],[24],[33]]" instead Thanks :)
– Chris Day
Nov 14 '18 at 18:44
You have to play around that :D learn guy and accept answer if it helped you :p
– NoorJafri
Nov 14 '18 at 19:23
(nextval = node.nextval) is getting error " 'Nonetype' object has no attribute 'nextval' "
– Chris Day
Nov 14 '18 at 15:29
(nextval = node.nextval) is getting error " 'Nonetype' object has no attribute 'nextval' "
– Chris Day
Nov 14 '18 at 15:29
Just add a check before fetching the value.
– NoorJafri
Nov 14 '18 at 15:32
Just add a check before fetching the value.
– NoorJafri
Nov 14 '18 at 15:32
what kind of check would you use?
– Chris Day
Nov 14 '18 at 15:54
what kind of check would you use?
– Chris Day
Nov 14 '18 at 15:54
great! thank you buddy! just one last thing as im curious, is there a way to get it so it would print "[[6],[15],[24],[33]]" instead Thanks :)
– Chris Day
Nov 14 '18 at 18:44
great! thank you buddy! just one last thing as im curious, is there a way to get it so it would print "[[6],[15],[24],[33]]" instead Thanks :)
– Chris Day
Nov 14 '18 at 18:44
You have to play around that :D learn guy and accept answer if it helped you :p
– NoorJafri
Nov 14 '18 at 19:23
You have to play around that :D learn guy and accept answer if it helped you :p
– NoorJafri
Nov 14 '18 at 19:23
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%2f53302144%2flooping-through-a-linked-list-containing-arrays-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
What is your input and expected output?
– Alex
Nov 14 '18 at 14:15
Did it help you?
– NoorJafri
Nov 14 '18 at 14:36