Suppose I have a list of numbers, how do I construct a new list such that every element of the new list is the sum of every two numbers in the old list?
ie given a list of number [1,2,3,4,5,6], I would like to return the list [1+2, 3+4,5+6]=[3,7,11]
I have attempted to write my code in the following way. However, when I applied it to my list, I am getting the error saying list index out of range.
def mod_list(mylist):
newlist=[]
counter = 0
for numbers in mylist:
counter += 1
if counter % 2 != 0:
newlist.append(numbers + mylist[counter + 1])
return newlist
[2, 0, 0, 1, 3, 0, 0, 1, 2, 2, 0, 1, 0, 0, 3, 2, 2, 0, 1, 2, 0, 4, 1, 0, 0, 0, 2, 2, 0, 1, 2, 3, 2, 1, 0, 2, 0, 0, 1, 0, 1, 1, 0, 2, 4, 1, 5, 2, 1, 4, 2, 2, 3, 1, 0, 1, 0, 5, 4, 0, 3, 1, 2, 0, 1, 1, 1, 1, 4, 1, 3, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 2, 0, 5, 1, 1, 2, 0, 1, 1, 1, 0, 2, 0, 0, 1, 0, 1, 1, 2, 4, 1, 0, 1, 2, 1, 0, 1, 0, 2, 6, 3, 2, 5, 4, 2, 3, 0, 1, 0, 5, 4, 2, 2, 4, 6, 1, 2, 2, 2, 0, 2, 2, 2, 2, 3, 1, 0, 0, 1, 1, 2, 1, 1, 2, 1, 4, 2, 1, 0, 7, 0, 0, 0, 2, 1, 2, 0, 0, 1, 2, 1, 0, 0, 3, 3, 4, 0, 2, 2, 2, 2, 1, 1, 5, 0, 0, 0, 3, 2, 6, 1, 0, 2, 2, 0, 0, 1, 5, 0, 0, 0, 0, 3, 1, 4, 6, 2, 3, 0, 2, 1, 6, 2, 1, 0, 4, 1, 4, 82, 17, 4, 0, 10, 8, 7, 9, 4, 2, 2, 0, 1, 1, 3, 0, 1, 3, 6, 5, 3, 1, 1, 2, 0, 0, 1, 3, 2, 1, 1, 2, 2, 0, 2, 1, 1, 0, 2, 5, 5, 1, 3, 7, 5, 3, 3, 2, 8, 4, 3, 6, 3, 2, 1, 2, 1, 3, 2, 2, 0, 2, 2, 0, 1, 0, 1, 1, 0, 0, 1, 3, 4, 3, 1, 2, 0, 0, 3, 1, 2, 2, 1, 4, 2, 0, 0, 1, 3, 1, 5, 2, 2, 2, 2, 3, 2, 2, 6, 0, 3, 3, 1, 0, 2, 1, 0, 0, 2, 0, 1, 6, 2, 1, 0, 3, 2, 1, 1, 4, 1, 6, 2, 0, 2, 2, 3, 3, 1, 6, 0, 1, 0, 0, 2, 0, 1, 5, 0, 0, 2, 1, 3, 5, 2]
Traceback (most recent call last):
File "C:\Textbook\Physics_180A\temp.py", line 131, in <module>
main()
File "C:\Textbook\Physics_180A\temp.py", line 13, in main
new_quake_days = mod_list(quake_days)
File "C:\Textbook\Physics_180A\temp.py", line 99, in mod_list
newlist.append(numbers + mylist[counter + 1])
IndexError: list index out of range
How can I fix this?
zip
can help here.
We have the following data.
>>> data = [1, 2, 3, 4, 5, 6]
Now we zip the data with itself.
>>> list(zip(data, data))
[(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]
Not exactly what we want, but it is a start. Now we use an offset of 1
for the second parameter.
>>> list(zip(data, data[1:]))
[(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
Looks better, but we need a step of 2
. This leads us to the following code.
>>> list(zip(data[::2], data[1::2]))
[(1, 2), (3, 4), (5, 6)]
Now we get a list with three entries. Each entry is a tuple with the numbers we want to add. Now all that's left is iterating over the resulting list/iterable, do the addition and add the result to a new list.
>>> result = []
>>> for x, y in zip(data[::2], data[1::2]):
result.append(x + y)
>>> print(result)
[3, 7, 11]
This could be written as a list comprehension and that leaves us with:
>>> result = [x + y for x, y in zip(data[::2], data[1::2])]
>>> print(result)
[3, 7, 11]
You can use this:
x = [1,2,3,4,5,6]
res = []
for i in range(0, len(x), 2):
res.append(x[i] + x[i+1])
Note: This assumes that the original list has even number of elements.
my_list = [1,2,3,4,5]
sum_list = []
for index, element in enumerate(my_list):
if index+1 < len(my_list):
sum_list.append(element+my_list[index+1])
else:
continue
This works for all size lists:
list = [1, 2, 3, 4, 5]
result = []
for i in range(0, len(list), 2):
if i == len(list) - 1:
result.append(list[i])
else:
result.append(list[i] + list[i + 1])
print (result)
As you group items by pair, it is simpler to process lists having an even number of elements. A trick for odd number of elements is to append a 0
: the algo will work fine, and you will get the same result:
def addlist(l):
if len(l) % 2 != 0:
l = l + [0] # does not change the list in the caller
return [l[i] + l[i+1] for i in range(0, len(l), 2)]
Demo:
>>> lst = [1, 2, 3, 4, 5, 6]
>>> addlist(lst)
[3, 7, 11]
>>> lst
[1, 2, 3, 4, 5, 6]
>>> lst = [1, 2, 3, 4, 5, 6, 7]
>>> addlist(lst)
[3, 7, 11, 7]
>>> lst
[1, 2, 3, 4, 5, 6, 7]
Considering the assumption that sum is calculated for even length lists, while for odd length lists, the last element is appended to the new list as it is.
What I mean is,
If the original list is [1, 2, 3, 4, 5, 6]
, the output list will be [3, 7, 11]
, while if the original list is [1, 2, 3, 4, 5]
, the output list will be [3, 7, 5]
.
For this assumption, the code is
lis = [1, 2, 3, 4, 5, 6]
newLis = []
for i in range(0, len(lis), 2):
if i < len(lis) and i + 1 < len(lis):
newLis.append(lis[i] + lis[i + 1])
else:
newLis.append(lis[i])
For odd-length list, if the last element is not required, the else part maybe removed.
How about making an iterable and extracting two elements from it in each iteration? Giving next
a default value of 0 deals with odd cases.
As a bonus, you do not need to create intermediate lists or use an explicit if-else.
def mod_list(mylist):
list_iter = iter(mylist)
return [x + next(list_iter, 0) for x in list_iter]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.