Related
I'm trying to split a list into groups based on index pairs from another list, given:
>>> l = list(range(10))
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> idx = [0, 5]
I need to break up the list resulting in:
>>> l[0:5]
[0, 1, 2, 3, 4]
>>> l[5:]
[5, 6, 7, 8, 9]
The list idx will at a minimum always be [0], but may be of size n; values inside idx will always be sorted ascending.
Currently I have:
>>> l = list(range(10))
>>> idx = [0, 5]
>>> idx.append(None)
>>> [l[idx[i]:idx[i + 1]] for i in range(len(idx) - 1)]
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]
Is there a way to accomplish this without explicitly appending Non and iterating over a range?
Edit: for another example...
Given:
>>> l = list(range(14))
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
>>> idx = [0, 5, 10]
Desired result:
[[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13]]
You could try about itertools.zip_longest:
from itertools import zip_longest
l = list(range(14))
idx = [0, 5, 10]
print([l[pre: next] for pre, next in zip_longest(idx,idx[1:])])
Result:
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13]]
With numpy you can use numpy.split()
import numpy as np
res =[list(x) for x in np.split(l, idx) if x.size != 0]
print(res)
Output:
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13]]
result = [l[curr_idx:curr_idx+idx[1]] for curr_idx in idx]
result
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13]]
If I have a list, I'm attempting to create a function that will go through the list and create arrays of size n.
For example:
list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] with n = 3
Running the function would generate [0, 1, 2], [1, 2, 3], [2, 3, 4] etc.
If you want to write it as a function, you have two inputs:
input_list and n
Then you should iterate over the list, and make sub-lists with length n, i've done this using list slices.
def print_list(input_list,n):
for i in range(len(input_list)):
if len(input_list[i:i+n])==n:
print(input_list[i:i+n])
Here you can find the output of this program with an example :
>>> print_list([1,2,3,4,5,6],3)
[1, 2, 3]
[2, 3, 4]
[3, 4, 5]
[4, 5, 6]
You can use zip with lists with incremented offsets:
lst = list(range(11))
n = 3
out = []
for sub in zip(*[lst[i:] for i in range(n)]):
out.append(list(sub))
Since zip stops at the end of the final list, you won’t have any empty values at the end. And in a function:
def func(list, n):
return [[*x] for x in zip(*[list[i:] for i in range(n)])]
See if this works for you:
x=[0,1,2,3,4,5,6,7,8,9,10,11]
def lst(lst, n):
for x in range(len(lst)-(n-1)):
print(lst[x:x+n])
lst(x,3)
Output:
[0, 1, 2]
[1, 2, 3]
[2, 3, 4]
[3, 4, 5]
[4, 5, 6]
[5, 6, 7]
[6, 7, 8]
[7, 8, 9]
[8, 9, 10]
[9, 10, 11]
Is there an elegant way how to pad the last sublist with zeroes while creating sublists from a list of integers?
So far I have this oneliner and need to fill the last sublist with 2 zeroes
[lst[x:x+3] for x in range(0, len(lst), 3)]
for example
lst =[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result should be:
[1,2,3][4,5,6][7,8,9][10,0,0]
With itertools.zip_longest, consuming the same iterator created off of the list, and fill in the missing values as 0 :
[[*i] for i in itertools.zip_longest(*[iter(lst)] * 3, fillvalue=0)]
Example:
In [1219]: lst =[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [1220]: [[*i] for i in itertools.zip_longest(*[iter(lst)] * 3, fillvalue=0)]
Out[1220]: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 0, 0]]
Without itertools:
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print([lst[x:x+3]+[0]*(x-len(lst)+3) for x in range(0, len(lst), 3)])
Prints:
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 0, 0]]
If I have pre-set list variables that are the same name except for they end in a different number, how can I create variables in a for loop that will call those lists?
My issue is that the variable I create in the for loop is staying as a string. I need it to recognize that it's simply a name of a variable.
Example:
list1 = [2, 4, 3, 7]
list2 = [4, 5, 6]
list3 = [9, 5, 7, 8, 3, 2, 1]
list4 = [4, 1]
for i in range(1,5):
print ("list%d" % (i))
This results in printing the strings:
list1
list2
list3
list4
I want it to print out the actual lists.
Thanks for any help!
You can achieve below as by using 'eval'.
list1 = [2, 4, 3, 7]
list2 = [4, 5, 6]
list3 = [9, 5, 7, 8, 3, 2, 1]
list4 = [4, 1]
for i in range(1,5):
print (eval('list%d'% (i)))
But as mentioned in comments, this is not recommended. You should rewrite by dict or list of list i.e.
my_list = [[2, 4, 3, 7],
[4, 5, 6],
[9, 5, 7, 8, 3, 2, 1],
[4, 1]]
for i in range(len(my_list)):
print (my_list[i])
Note that i change the name of variable list to my_list because the word list is a reserved word.
You are printing a string literal (i.e. plain text) not the actual variable of each list. One thing you can do is put those lists in a class or a dictionary.
Assuming it's a class:
class cls:
list1 = [2, 4, 3, 7]
list2 = [4, 5, 6]
list3 = [9, 5, 7, 8, 3, 2, 1]
list4 = [4, 1]
for i in range(1, 5):
print getattr(cls, 'list{}'.format(i))
Assuming it's a dictionary:
lists = {
'list1': [2, 4, 3, 7],
'list2': [4, 5, 6],
'list3': [9, 5, 7, 8, 3, 2, 1],
'list4': [4, 1],
}
for k, v in lists.items():
print '{0}={1}'.format(k, v)
As suggested in the comments you should consider some other data structue to use here.
If you still want this to work. You may try as below
for i in range(1,5):
print (eval("list"+str(i)))
the issue with your code that you print a string not the list variable
to loop through a list variable
list1 = [2, 4, 3, 7]
list2 = [4, 5, 6]
list3 = [9, 5, 7, 8, 3, 2, 1]
list4 = [4, 1]
for i in list1:
print (i)
or you can put all the lists in a list and loop through it
new_list=[[2, 4, 3, 7],[4, 5, 6],[9, 5, 7, 8, 3, 2, 1],[4, 1]]
for e in new_list:
print(e)
you can also put all of them in a dictionary structure {key,value}
dic_of_lists={"list1":[2, 4, 3, 7],"list2":[4, 5, 6]
,"list3":[9, 5, 7, 8, 3, 2, 1],"list4":[4, 1]}
#to loop through the dictionary
for key,value in dic_of_lists.items():
print(key+" : ")
print(value)
I have a lists of lists in variable lists something like this:
[7, 6, 1, 8, 3]
[1, 7, 2, 4, 2]
[5, 6, 4, 2, 3]
[0, 3, 3, 1, 6]
[3, 5, 2, 14, 3]
[3, 11, 9, 1, 1]
[1, 10, 2, 3, 1]
When I write lists[1] I get vertically:
6
7
6
3
5
11
10
but when I loop it:
for i in list:
print(i)
I get this horizontally.
7
6
1
8
3
etc...
So, how it works? How can I modify loop to go and give me all vertically?
Short answer:
for l in lists:
print l[1]
Lists of lists
list_of_lists = [ [1, 2, 3], [4, 5, 6], [7, 8, 9]]
for list in list_of_lists:
for x in list:
print x
Here is how you would print out the list of lists columns.
lists = [[7, 6, 1, 8, 3],
[1, 7, 2, 4, 2],
[5, 6, 4, 2, 3],
[0, 3, 3, 1, 6],
[3, 5, 2, 14, 3],
[3, 11, 9, 1, 1],
[1, 10, 2, 3, 1]]
for i in range(0, len(lists[1])):
for j in range(0, len(lists)):
print lists[j][i],
print "\n"