This is list of tile placements. Each integer stands for an id of a tile. Each time an integer is added to a new list it means that a new tile is placed. When a tile is removed, the last integer is removed from a new list. I want that every time a tile is placed the list to be unique. A list dont have to be unique, when a tile is removed. The code is placing these tiles in a for loop. So for this example, the last list of the lists is wrong, because it wasn't unique when a tile was placed. Is there a way to exclude numbers which will make the new list not unique. So for this example is there a way to exclude the id 18, before it adds to the list.
I know this is a very vague question, but I am new to python and can't make the code of this assignment easier. I hope someone could help me with this vague question
[[1, 2, 3, 13, 4, 5, 6, 7, 17],
[1, 2, 3, 13, 4, 5, 6, 7, 17, 8],
[1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15],
[1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15, 9],
[1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15, 9, 10],
[1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15, 9, 10, 18],
[1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15, 9, 11],
[1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15, 9, 11, 18],
[1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15, 9, 10],
[1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15, 9, 10, 18]]
The lists must be in this order. So for example i have had these lists:
[[1, 2, 3, 13, 4, 5, 6, 7, 17],
[1, 2, 3, 13, 4, 5, 6, 7],
[1, 2, 3, 13, 4, 5, 6, 7, 8],
[1, 2, 3, 13, 4, 5, 6, 7],
[1, 2, 3, 13, 4, 5, 6, 7, 19],
[1, 2, 3, 13, 4, 5, 6, 7]]
I want to exlude the ids 17,8,19
So for [1, 2, 3, 13, 4, 5, 6, 7] the output must look like this ( id ont care if the output is a list or integers)
[17,8,19]
But when i have this list [1, 2, 3, 13, 4, 5, 6] in lists
[[1, 2, 3, 13, 4, 5, 6, 7, 17],
[1, 2, 3, 13, 4, 5, 6],
[1, 2, 3, 13, 4, 5, 6, 7, 8],
[1, 2, 3, 13, 4, 5, 6, 7],
[1, 2, 3, 13, 4, 5, 6, 7, 19],
[1, 2, 3, 13, 4, 5, 6, 7]]
The output is this:
[7]
I hope this will make it more clear.
I tried with itertools and collections- pass a list, a list element index and to be added value to the adder function if uniquness is kept the adder will add that passed value otherwise return intact list.compare_func return TRUE if list is unique using all.
import collections,itertools
compare_func = lambda x, y: collections.Counter(x) != collections.Counter(y)
lst = [[1, 2, 3],[1, 2, 3,4]]
def adder(mylist,indx,val):
mylist[indx].append(val)
if all([compare_func(*i) for i in list(itertools.combinations(lst,2))]):
print "Added item"
else:
print "Did not add item"
mylist[indx].pop()
return mylist
Now run print adder(lst,0,4)
Output-
Did not add item
[[1, 2, 3], [1, 2, 3, 4]]
But if run
print adder(lst,1,4)
Output-
Added item
[[1, 2, 3], [1, 2, 3, 4, 4]]
EDIT
After OP cleared question i added this portion-
Try using set as below-
import collections,itertools
data = [[1, 2, 3, 13, 4, 5, 6, 7, 17],
[1, 2, 3, 13, 4, 5, 6, 7],
[1, 2, 3, 13, 4, 5, 6, 7, 8],
[1, 2, 3, 13, 4, 5, 6, 7],
[1, 2, 3, 13, 4, 5, 6, 7, 19],
[1, 2, 3, 13, 4, 5, 6, 7]]
interscntion = set.intersection(*map(set,data))
d = collections.Counter([i for j in data for i in j if i not in list(interscntion)])
if len(set(it[1] for it in d.most_common()))>1:
print [max(d.most_common(),key=lambda x:x[1])[0]]
else:
print [j[0] for j in d.most_common()]
Output-
[8, 17, 19]
Here you go:
def tiles(arrOfArrs):
final = []
for x in arrOfArrs:
final += x
return list(set(final))
Keep another list of lists and at each position add the tiles that appear at that index in any of the primary lists.
Related
I am trying to create an output that will be an array that contains 5 "sub-arrays". Every array should include 10 random numbers between 0 and 10.
I have this code:
def count_tweets():
big_array = []
for i in range(5):
array = []
for p in range(10):
array.append(random.randint(0,10))
big_array.append(array)
print(big_array)
I get a result like:
[[4, 2, 7, 1, 3, 2, 6, 9, 3, 10]]
[[4, 2, 7, 1, 3, 2, 6, 9, 3, 10], [5, 10, 7, 10, 7, 2, 1, 4, 8, 3]]
[[4, 2, 7, 1, 3, 2, 6, 9, 3, 10], [5, 10, 7, 10, 7, 2, 1, 4, 8, 3], [2, 7, 1, 3, 8, 5, 7, 6, 0, 0]]
[[4, 2, 7, 1, 3, 2, 6, 9, 3, 10], [5, 10, 7, 10, 7, 2, 1, 4, 8, 3], [2, 7, 1, 3, 8, 5, 7, 6, 0, 0], [0, 1, 9, 9, 4, 2, 10, 4, 3, 8]]
[[4, 2, 7, 1, 3, 2, 6, 9, 3, 10], [5, 10, 7, 10, 7, 2, 1, 4, 8, 3], [2, 7, 1, 3, 8, 5, 7, 6, 0, 0], [0, 1, 9, 9, 4, 2, 10, 4, 3, 8], [3, 7, 3, 5, 4, 0, 2, 8, 6, 2]]
But instead it should be like:
[[0,2,6,7,9,4,6,1,10,5],[1,3,5,9,8,7,6,9,0,10],[3,5,1,7,9,4,7,2,7,9],[10,2,8,5,6,9,2,3,5,9],[4,5,2,9,8,7,5,1,3,5]]
I cannot seem to get the indentation correct. How do I fix the code?
So what you did was put the print() statement inside a loop, which will print each time it runs.
import random
def count_tweets():
big_array = []
for i in range(5):
array = []
for p in range(10):
array.append(random.randint(0,10))
big_array.append(array)
print(big_array)
count_tweets()
Hope this helps :)
You got it right, just slide the print out of the for loop.(delete four spaces before print())
I need to simulate a certain scenario.
So I'm defining a variable which generates a loop of a random number of integers.
I get for example:
list = [2, 35, 4, 8, 56, 10]
Then, I'm generating this random list 50 times through another loop and I store the data into a dictionary to visualize a Pandas Dataframe.
data_dict = {'random_numers': list}
data_dict_pd = pd.DataFrame(data_dict)
So I get for example this:
[1, 16, 6, 6, 1, 10]
[3, 8, 4, 4, 1, 20, 7, 25, 12]
[14, 8, 16, 4, 11, 18, 5, 15, 24, 2, 15, 5]
[7, 24, 1, 14]
[5, 14, 19, 24, 1]
... 50 times.
Now, I need to create another column enumerating each number in each list of elements, to get the following, based on the previous results:
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
...50 times.
Actually, came up with the following but it's wrong:
new_list = []
for index in enumerate(list)
new_list.append(index)
Any better idea?
I would strongly suggest changing the name of your list, as list is used for Python lists.
Assuming you change it to l, I would use:
l = [2, 35, 4, 8, 56, 10]
new_list = []
for i in range(1, len(l) + 1):
new_list.append(i)
print(new_list)
Output:
[1, 2, 3, 4, 5, 6]
If what you need is to iterate a list of lists, and incorporating #deceze suggestions (provided that you don't rename list):
lists = [
[1, 16, 6, 6, 1, 10],
[3, 8, 4, 4, 1, 20, 7, 25, 12],
[14, 8, 16, 4, 11, 18, 5, 15, 24, 2, 15, 5],
[7, 24, 1, 14],
[5, 14, 19, 24, 1]
]
new_lists = [list(range(1, len(lst) + 1)) for lst in lists]
print(new_lists)
Output:
[[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [1, 2, 3, 4], [1, 2, 3, 4, 5]]
new_list = []
for index in range(1,len(list)+1)
new_list.append(index)
should be used in place of enumerator,as it will going to return the pair.
Thank you,
I have a list(T) of 6500 images(arrays) that I am using for image classification, and I would like to see how increasing the data affects the accuracy.
So, starting from n=2000 images, I am thinking of having a loop that will add 500(n+=500) images at each iteration till it reaches 6500 and therefore compare the accuracy between 2000, 2500, 3000, ... 6500. I have simplified the problem below by having a list of 20 elements.
lst = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]
My second list (slist) contains the first 9 elements of the first list (lst).
I am trying to add 2 values to slist at each iteration, starting from lst[9:]. I know rather than using append, extend should be used to add multiple values at once. However, I couldn't find a way to do it.
In the following code, one element is added to slist (from lst) at each loop.
lst = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]
slist = lst[:9]
for i in lst[9:]:
slist.append(i)
How can I add 2 or 3 elements simultaneously at each loop? An example output would be:
[1,2,3,4,5,6,7,8,9,0,1]
[1,2,3,4,5,6,7,8,9,0,1,2,3]
[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5]
You could try using extend:
l = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]
slist = l[:9]
for i in l[9:][::2]:
if i == l[9]:
slist.extend(l[9+i: 9+i+1])
else:
slist.extend(l[9+i-1: 9+i+1])
print(slist)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0]
lst=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
16,17,18,19,20]
iter = 9
while True:
print(lst[:iter])
iter+=2
if len(lst) <= iter:
print(lst[:iter])
break
This code does the job
lst=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
16,17,18,19,21]
slist= lst[:9]
s,f=0,2
while True:
slist.extend(lst[9:][s:f])
print(slist)
s+=2
f+=2
if len(slist) >= len(lst):
break
It prints out:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
I have two iterators, which consists of a "list" that looks something like this:
[[1, 2, 3, 4, 5, 6],
[2, 4, 6, 8, 10, 12],
[3, 5, 8, 6, 1, 19],
[5, 9, 1, 9, 4, 6]]
Or, that is what it will look like if I just ran a for loop over them.
The reason for the iterator and not a list per se is due to memory. The true lists/arrays are way larger, this is just an example.
What I need to do is take one list and sum the columns of each index inside the list for all "outside" indices and then add them together for both lists like sum(list1) + sum(list2).
So basically:
list1: list2:
[[1, 2, 3, 4, 5, 6], [[5, 4, 3, 2, 1, 9],
[2, 4, 6, 8, 10, 12], [6, 3, 8, 1, 1, 6],
[3, 5, 8, 6, 1, 19], [1, 3, 2, 8, 2, 3],
[5, 9, 1, 9, 4, 6]] [5, 2, 9, 4, 2, 5]]
=> =>
[11, 20, 18, 20, 43] [17, 12, 22, 15, 23]
=>
[28, 32, 40, 35, 66]
So I iterate over the two lists, and for each list I need to sum the columns, and then in the end at the columns of the final two lists into one combined list.
I know how to do this if it were just regular lists, but since this is iterators/generators (don't know the correct term) I am really not sure how it is done.
You can use this to sum each one without loading everything into memory:
def sumIter(iter):
result = [0, 0, 0, 0, 0, 0] #Assuming there are always 6 items in each sub-list
for list in iter:
result = [(result[i] + list[i]) for i in range(6)]
And then:
sum1 = sumIter(iter1)
sum2 = sumIter(iter2)
result = [(sum1[i] + sum2[i]) for i in range(6)]
Using zip
Ex:
l1 = [
[1, 2, 3, 4, 5, 6],
[2, 4, 6, 8, 10, 12],
[3, 5, 8, 6, 1, 19],
[5, 9, 1, 9, 4, 6]
]
l2 = [
[5, 4, 3, 2, 1, 9],
[6, 3, 8, 1, 1, 6],
[1, 3, 2, 8, 2, 3],
[5, 2, 9, 4, 2, 5]
]
l1 = (sum(i) for i in zip(*l1))
l2 = (sum(i) for i in zip(*l2))
print( [sum(i) for i in zip(l1, l2)] )
Output:
[28, 32, 40, 42, 26, 66]
Using reduce since row can be added in numpy array.
reduce is an build-in function in python2
import numpy as np
from functools import reduce # only in python3
def sumup(one_row, another_row):
return one_row + another_row
test_list = np.array([[1, 2, 3, 4, 5, 6],
[2, 4, 6, 8, 10, 12],
[3, 5, 8, 6, 1, 19],
[5, 9, 1, 9, 4, 6]])
reduce(sumup, test_list)
Output
array([11, 20, 18, 27, 20, 43])
using numpy.sum
import numpy as np
l1 = np.sum([[1, 2, 3, 4, 5, 6], [2, 4, 6, 8, 10, 12], [3, 5, 8, 6, 1, 19], [5, 9, 1, 9, 4, 6]], axis=0)
l2 = np.sum([[5, 4, 3, 2, 1, 9],[6, 3, 8, 1, 1, 6], [1, 3, 2, 8, 2, 3],[5, 2, 9, 4, 2, 5]], axis=0)
print(l1 + l2)
Output
[28 32 40 42 26 66]
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm a newbie to coding and am trying to code something in Python. Below is the requirement.
I have an array with 15 elements.
A = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
I want to split into to overlapping smaller arrays of length 7. That is
l1 = [1,2,3,4,5,6,7]
l2 = [2,3,4,5,6,7,8]
l3 = [3,4,5,6,7,8,9]
etc till
l9 = [9,10,11,12,13,14,15]
How can this be done?
You could slice the list in a for loop:
>>> A = list(range(1, 16))
>>> for i in range(len(A) - 6):
... print(A[i:i+7])
...
[1, 2, 3, 4, 5, 6, 7]
[2, 3, 4, 5, 6, 7, 8]
[3, 4, 5, 6, 7, 8, 9]
[4, 5, 6, 7, 8, 9, 10]
[5, 6, 7, 8, 9, 10, 11]
[6, 7, 8, 9, 10, 11, 12]
[7, 8, 9, 10, 11, 12, 13]
[8, 9, 10, 11, 12, 13, 14]
[9, 10, 11, 12, 13, 14, 15]
If you want to assign the sublists to individual variables, it's better to use a list comprehension:
L1, L2, ..., L9 = [A[i:i+7] for i in range(len(A) - 6)]
This little function should do exactly what you want and it handles also cases in which len(array) is not an exact multiple of the lengths of the smaller arrays. Of course, you can input the split-size and the extension of the overlap.
def split_overlap(array,size,overlap):
result = []
while True:
if len(array) <= size:
result.append(array)
return result
else:
result.append(array[:size])
array = array[size-overlap:]
How to use it:
array = list(range(10))
print split_overlap(array,4,2)
[[0, 1, 2, 3], [2, 3, 4, 5], [4, 5, 6, 7], [6, 7, 8, 9]]
array = list(range(11))
print split_overlap(array,4,2)
[[0, 1, 2, 3], [2, 3, 4, 5], [4, 5, 6, 7], [6, 7, 8, 9], [8, 9, 10]]
Your specific case:
A = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
split_overlap(A,7,6)
[[1, 2, 3, 4, 5, 6, 7],
[2, 3, 4, 5, 6, 7, 8],
[3, 4, 5, 6, 7, 8, 9],
[4, 5, 6, 7, 8, 9, 10],
[5, 6, 7, 8, 9, 10, 11],
[6, 7, 8, 9, 10, 11, 12],
[7, 8, 9, 10, 11, 12, 13],
[8, 9, 10, 11, 12, 13, 14],
[9, 10, 11, 12, 13, 14, 15]]
You could probably use this code:
A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
start = range(len(A) + 1)
stop = start[7:]
L = [A[i:j] for i, j in zip(start, stop)]
Demo:
In [340]: L
Out[340]:
[[1, 2, 3, 4, 5, 6, 7],
[2, 3, 4, 5, 6, 7, 8],
[3, 4, 5, 6, 7, 8, 9],
[4, 5, 6, 7, 8, 9, 10],
[5, 6, 7, 8, 9, 10, 11],
[6, 7, 8, 9, 10, 11, 12],
[7, 8, 9, 10, 11, 12, 13],
[8, 9, 10, 11, 12, 13, 14],
[9, 10, 11, 12, 13, 14, 15]]
To access individual lists you just need to index L appropriately
In [341]: L[0] #first list
Out[341]: [1, 2, 3, 4, 5, 6, 7]
In [342]: L[1] #second list
Out[342]: [2, 3, 4, 5, 6, 7, 8]
In [343]: L[-1] #last list
Out[343]: [9, 10, 11, 12, 13, 14, 15]
use slice to do this.
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
# a = [1, 2, 3, 4, 5, 6, 7]
LENGTH = 7
result = []
if len(a) < LENGTH:
result.append(a[:])
else:
for i in range(len(a)-LENGTH+1):
result.append(a[i:i+LENGTH])
print result
# [[1, 2, 3, 4, 5, 6, 7], [2, 3, 4, 5, 6, 7, 8], [3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 10], [5, 6, 7, 8, 9, 10, 11], [6, 7, 8, 9, 10, 11, 12], [7, 8, 9, 10, 11, 12, 13], [8, 9, 10, 11, 12, 13, 14], [9, 10, 11, 12, 13, 14, 15]]
Its always a good practise to post your own effort (code) in solving the problem. However you can try my solution and I hope it helps:
def overlap_split(array, split_length):
for i in range(len(array) - split_length):
print(array[i:i + (split_length + 1)])
If you look very closely you'ld notice that I've converted #eugene's code to a function which you can re-use. If you plan to make use of the splits then you will have to store each split in individual arrays i.e. l1, l2 ... ln as demonstrated by #eugene