In Jupyter notebooks, or in IPython, long lists are displayed one element per line. How do I display them on a single line? I don't mind if the line wraps.
In the following example, I'd like the 3rd list to be shown as a "row", not as a "column".
In [1]: [list(range(n)) for n in range(10,40,10)]
Out[1]:
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29]]
The output I am looking for is the following or similar:
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]]
My goal is to make the output easier to read for humans.
I would simply use
A = [list(range(n)) for n in range(10,40,10)]
for i in A:
print(i)
I'm learning python and I wrote a program to pick a random office episode to watch and 75% of the time my program works but the other 25%, it gives me the error :
list index out of range.
I've tried messing with the min max values in the line that chooses the episode
import random as r
seasons = [
[1, 2, 3, 4, 5, 5, 6],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
]
def choose_ep():
global seasons
s = r.randint(1,10) # 1 - 10, not including 10
e = r.randint(min(seasons[s - 1]), max(seasons[s - 1]))
print("Season: " + str(s) + ", Episode: " + str(e))
while True:
choose_ep()
if input() == "exit":
break
I want it to give me a random episode from a random season. I don't want it to give me an episode that doesn't exist like "season 1, episode 14"
https://docs.python.org/3/library/random.html#random.randint
random.randint(a, b)
Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1).
So 10 is in fact included in the values of variable s and hence you get your error.
Alternative solution with no index generation:
import random as r
season_number, episode_list = r.choice(list(enumerate(seasons)))
season_number += 1
episode_number = r.choice(episode_list)
print(season_number, episode_number)
I am not sure what I am doing wrong, if anyone can see exactly, please inform me.
Tried:
grid = [[n in range(1,6)*5]]
and
grid [[]*5 for n in range(1,25)]
My code:
grid = [[x for x in range(1,25)] for y in range(5)]
Output:
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]]
Desired:
[[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15],[16,17,18,19,20],[21,22,23,24,25]]
It's fairly obvious that values in inner lists have to be related both to internal counter in each list (x) and position of list in outer list (y). Your code fails to take care about second requirement.
grid = [[1+x+5*y for x in range(5)] for y in range(5)]
You are iterating in a range from 1 to 24(inclusive) in your inner loop, which is why your lists are too long, and have the same values in them.
You somehow need to make the variable of your outer loop dictate the inner loop so each range within the inner loop start from a different point. Also, calibrating the length of the inner range is essential.
That being said, I suggest the following to get your desired output.
grid = [list(range(i, i+5)) for i in range(1, 26, 5)]
You can cut a list into sublist of 5, see this code
data=range(1,26)
len_sublist = 5
print [data[x:x+len_sublist] for x in xrange(0, len(data), len_sublist)]
I have a list of ints in python, and I would like the output to be a list of different "craters" in the sequence. I define a crater as a sequence of numbers that begins with descending ints that gradually grow bigger, until they reach a number that is equal to the first int of the crater or greater than the first int in the crater.
If the last number is equal or smaller than the first - it should be in the output at the end of the crater. If the last number is greater than the first number in the crater - it should not be appended to the end of the output list.
For example, for the list:
1,2,3,4,5,6,5,4,3,4,5,6,7,8,9
A "crater" would be:
6,5,4,3,4,5,6
My code:
def find_crater(my_list):
cur_crater = []
for i in range(len(my_list)-1):
#check if the numbers are decreasing
if (my_list[i] - my_list[i+1] > 0):
cur_crater.append(my_list[i])
#if increasing - check if we're in a crater
#and if current number is smaller than begining of crater
elif len(cur_crater) > 1 and my_list[i] <= cur_crater[0]:
cur_crater.append(my_list[i])
#print crater and empty list
elif len(cur_crater) > 1 and my_list[i+1] > cur_crater[0]:
print(cur_crater)
cur_crater = []
#continue if we're out of a crater
else:
continue
#catching the last crater in the list
if len(cur_crater) > 1:
#include last int
if my_list[i] - my_list[-1] < 0 and my_list[-1] <= cur_crater[0]:
cur_crater.append(my_list[-1])
print(cur_crater)
return
I have called the function on 2 lists. The 1st output is as expected:
ok_list = [12, 4, 7, 4, 2, 4, 5, 6, 5, 12, 21, 23,
24, 26, 25, 22, 10, 9, 8, 6, 9, 10, 11, 13, 12, 14,
17, 27, 28, 19, 17, 19, 21, 19, 12, 23, 25, 27]
Ok list output [edit: not so ok - it ommited the first '4' so turns out it's not so OK after all]:
[12, 7, 4, 2, 4, 5, 6, 5, 12]
[26, 25, 22, 10, 9, 8, 6, 9, 10, 11, 13, 12, 14, 17]
[28, 19, 17, 19, 21, 19, 12, 23, 25, 27]
However, the second list combines 2 lists of craters into 1 list in the output:
problematic = [12, 4, 7, 4, 2, 4, 5, 6, 5, 12, 21, 23,
24, 26, 25, 22, 10, 9, 8, 6, 9, 10, 11, 13, 12, 14,
17, 27, 19, 25, 19, 12, 23, 25, 27]
problematic Output:
[12, 7, 4, 2, 4, 5, 6, 5, 12]
[26, 25, 22, 10, 9, 8, 6, 9, 10, 11, 13, 12, 14, 17, 27, 19, 25, 19, 12, 23, 25]
I was expecting:
[12, 4, 7, 4, 2, 4, 5, 6, 5, 12]
[26, 25, 22, 10, 9, 8, 6, 9, 10, 11, 13, 12, 14, 17]
[27, 19, 25, 19, 12, 23, 25, 27]
I tried to take care of this by writing:
my_list[i+1] > cur_crater[0]
and thus checking the size of next int in the list - but that does not seem to fix it [I left it in the code even though it does not do the trick in hopes of someone explaining why is that wrong/ not working?].
In conclusion, my code can't handle it when there is a crater right after a crater with only one int in between.
If the input is:
[12, 4, 7, 4, 2, 4, 5, 6, 5, 12, 21, 10, 9, 8, 6, 9, 10]
Then the output is one long list, but I'd like to split it after list item 12 and before list item 21, but the output is one long list.
I would love to get info about any library or method that can give me more ideas regarding a better and more efficient solution.
Presuming your expected output is wrong which it seems to be, all you want to do is go until you find an element >= to the start of the chain and catch chains that only contain a single element:
def craters(lst):
it = iter(lst)
# get start of first chain
first = next(it)
tmp = [first]
# iterate over the remaining
for ele in it:
# >= ends chain
if ele >= first:
# if equal, add it and call
# next(it) to set the next first element.
if ele == first:
tmp.append(ele)
yield tmp
first = next(it)
tmp = [first]
# else yield the group if it has > 1 element.
# if it has 1 element it is not a descending start sequecne
else:
if len(tmp) > 1:
yield tmp
tmp = [ele]
first = ele
else:
tmp.append(ele)
if len(tmp) > 1: # catch single element last
yield tmp
Output:
In [5]: ok_list = [12, 4, 7, 4, 2, 4, 5, 6, 5, 12, 21, 23,
...: 24, 26, 25, 22, 10, 9, 8, 6, 9, 10, 11, 13, 12, 14,
...: 17, 27, 28, 19, 17, 19, 21, 19, 12, 23, 25, 27]
In [6]: problematic = [12, 4, 7, 4, 2, 4, 5, 6, 5, 12, 21, 23,
...: 24, 26, 25, 22, 10, 9, 8, 6, 9, 10, 11, 13, 12, 14,
...: 17, 27, 19, 25, 19, 12, 23, 25, 27]
In [7]: ex_ok = [[12, 4, 7, 4, 2, 4, 5, 6, 5, 12],
...: [26, 25, 22, 10, 9, 8, 6, 9, 10, 11, 13, 12, 14, 17],
...: [28, 19, 17, 19, 21, 19, 12, 23, 25, 27]]
In [8]: ex_p = [[12,4, 7, 4, 2, 4, 5, 6, 5, 12],
...: [26, 25, 22, 10, 9, 8, 6, 9, 10, 11, 13, 12, 14, 17],
...: [27, 19, 25, 19, 12, 23, 25, 27]]
In [9]: print(list(craters(problematic)) == ex_p)
True
In [10]: print(list(craters(ok_list)) == ex_ok)
True
In [11]: print(list(craters([12, 4, 7, 4, 2, 4, 5, 6, 5, 12, 21, 10, 9, 8, 6, 9, 10])))
[[12, 4, 7, 4, 2, 4, 5, 6, 5, 12], [21, 10, 9, 8, 6, 9, 10]]
In [12]: list(craters([1, 2, 3, 4, 5, 6, 5, 4, 3, 4, 5, 6, 7, 8, 9]))
Out[13]: [[6, 5, 4, 3, 4, 5, 6]]
That does not involve any slicing/indexing you can lazily return each group.
To simplify your algorithm, the steps are:
Start with the first element, then check if the next element is >=, if it is and it is equal add it to the group, if it is > set that to be the new start of the group.
If the new first is not greater than the next element the length of that group will be one as it does not satisfy sequence of numbers that begins with descending. So we keep consuming and setting the next element to be the first element of the sequence until we find one the is > the next element, that is what the call to next(it) is doing then we just need to wash and repeat.
Here is what I came with:
def find_crater(my_list):
previous = None
current_crater = []
crater_list = []
for elem in my_list:
#For the first element
if not previous:
previous = elem
continue
if len(current_crater) == 0:
if elem > previous:
previous = elem
else:
current_crater.append(previous)
previous = elem
else:
if elem > current_crater[0]:
current_crater.append(previous)
crater_list.append(current_crater)
current_crater = []
previous = elem
else:
current_crater.append(previous)
previous = elem
if len(current_crater) != 0:
if elem > current_crater[0]:
current_crater.append(previous)
crater_list.append(current_crater)
else:
current_crater.append(previous)
crater_list.append(current_crater)
return crater_list
That gave me the exact output you want:
In [5]: ok_list = [12, 4, 7, 4, 2, 4, 5, 6, 5, 12, 21, 23,
...: 24, 26, 25, 22, 10, 9, 8, 6, 9, 10, 11, 13, 12, 14,
...: 17, 27, 28, 19, 17, 19, 21, 19, 12, 23, 25, 27]
In [6]: find_crater(ok_list)
Out[6]:
[[12, 4, 7, 4, 2, 4, 5, 6, 5, 12],
[26, 25, 22, 10, 9, 8, 6, 9, 10, 11, 13, 12, 14, 17],
[28, 19, 17, 19, 21, 19, 12, 23, 25, 27]]
In [7]: problematic = [12, 4, 7, 4, 2, 4, 5, 6, 5, 12, 21, 23,
...: 24, 26, 25, 22, 10, 9, 8, 6, 9, 10, 11, 13, 12, 14,
...: 17, 27, 19, 25, 19, 12, 23, 25, 27]
In [8]: find_crater(problematic)
Out[8]:
[[12, 4, 7, 4, 2, 4, 5, 6, 5, 12],
[26, 25, 22, 10, 9, 8, 6, 9, 10, 11, 13, 12, 14, 17],
[27, 19, 25, 19, 12, 23, 25, 27]]
Try this:
We just slice the list finding start_index:end_index making three cases
When element is equal
When element is greater
Boundary case when last element even if smaller may need to be sliced
def find_crater(l):
result = []
start_index = 0
for i in range( start_index +1,len(l)):
start = l[start_index]
if i==len(l)-1 and l[i]!=start:
result = l[start_index:i+1]
print result
elif l[i]==start:
end_index = i+1
result = l[start_index:end_index]
start_index = end_index +1
if len(result)>1:
print result
elif l[i]>start:
end_index = i
result = l[start_index:end_index]
start_index = end_index
if len(result)>1:
print result
Input:
ok_list = [12, 4, 7, 4, 2, 4, 5, 6, 5, 12, 21, 23,
24, 26, 25, 22, 10, 9, 8, 6, 9, 10, 11, 13, 12, 14,
17, 27, 28, 19, 17, 19, 21, 19, 12, 23, 25, 27]
problematic = [12, 4, 7, 4, 2, 4, 5, 6, 5, 12, 21, 23,
24, 26, 25, 22, 10, 9, 8, 6, 9, 10, 11, 13, 12, 14,
17, 27, 19, 25, 19, 12, 23, 25, 27]
Output:
find_crater(ok_list)
find_crater(problematic)
[12, 4, 7, 4, 2, 4, 5, 6, 5, 12]
[25, 22, 10, 9, 8, 6, 9, 10, 11, 13, 12, 14, 17]
[28, 19, 17, 19, 21, 19, 12, 23, 25, 27]
[12, 4, 7, 4, 2, 4, 5, 6, 5, 12]
[25, 22, 10, 9, 8, 6, 9, 10, 11, 13, 12, 14, 17]
[27, 19, 25, 19, 12, 23, 25, 27]
Interesting answers, allow me to modify the question.
After some change on the code I got this:
#coding:utf-8
import itertools
stuff = [1, 2, 3, 4, 5, 8, 10, 13, 16, 17, 18, 20, 21, 22, 25]
for L in range(5, 6):
for subset in itertools.combinations(stuff, L):
subset = list(subset)
subset.extend([7, 9, 11, 15, 19, 23, 6, 12, 14, 24])
print(subset)
the output of it is like this:
[1, 2, 3, 4, 5, 7, 9, 11, 15, 19, 23, 6, 12, 14, 24]
[1, 2, 3, 4, 8, 7, 9, 11, 15, 19, 23, 6, 12, 14, 24]
...
It generates approximately 3000 lines.
It did all possible combination with five numbers of the list stuff and add to every single combination (subset) the another list (subset.extend([7, 9, 11, 15, 19, 23, 6, 12, 14, 24])). It seems to be right, I'm not sure.
But what I really want it to do is:
1 - Input three lists (pair and unpair)
stuff = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
pair = [2 , 6, 12, 20, 16, 10, 22]
unpair = [1, 5, 11, 19, 23, 25, 13, 17]
2 - Than the program will generate all combinations of 4 numbers from the 7 numbers of pair and will do the same with unpair, generating all combinations of 4 numbers from the 8 numbers of unpair, and will bind it together generating a list with 8 number of al possible combinations of 4 numbers from pair combined with 4 number from unpair like:
[2, 12, 20, 10, 1, 5, 19, 23]
[2, 12, 20, 10, 5, 19, 25, 13]
...
3 - Than for each line from the combination of pair and unpair generated it will complete with a 7 numbers combination from the list stuff generating a list with 15 numbers without repeating a number like
[2, 12, 20, 10, 1, 5, 19, 23, 25, 3, 4, 8, 17, 21, 22]
[2, 12, 20, 10, 5, 19, 25, 13, 3, 4, 8, 17, 21, 22, 11]
...
Here is where I got stuck. How to generate a combination for each list and bind them generating a 15 numbers list without repeating a number and a sequence.
you can just manually code that logic in your for loop:
for combo in itertools.combinations(stuff, 15):
if set(combo).issuperset(pair) and set(combo).issuperset(unpair):
print(combo)
Note: This particular code only works when "stuff" has no duplicates