The following code partitions a list in spaces of 5.
o_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200]
def partition(lst, size):
for i in range(0, len(lst), size):
yield lst[i :: size]
# size of each partition
n = 5
p_list = list(partition(o_list, n))
print("Original List: ")
print(o_list)
print("Partitioned List:")
print(p_list)
The following are the results:
Original List:
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200]
Partitioned List:
[[10, 60, 110, 160], [60, 110, 160], [110, 160], [160]]
However I want the second array to be [20, 70, 120, 170] and the third and so on follow suit.
Just replace the comma in the range function to a // operator:
o_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200]
def partition(lst, size):
for i in range(0, len(lst) // size):
yield lst[i :: size]
# size of each partition
n = 5
p_list = list(partition(o_list, n))
print("Original List: ")
print(o_list)
print("Partitioned List:")
print(p_list)
This prints:
Original List:
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200]
Partitioned List:
[[10, 60, 110, 160], [20, 70, 120, 170], [30, 80, 130, 180], [40, 90, 140, 190]]
Something like this?
o_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200]
[o_list[x::5] for x in range(len(o_list)//5)]
[[10, 60, 110, 160],
[20, 70, 120, 170],
[30, 80, 130, 180],
[40, 90, 140, 190]]
With division you can figure out how many slices can be produced, then simply produce those slices in a loop.
partition = lambda L, N: [L[n::N] for n in range(len(L)//N)]
print(partition(list(range(10, 201, 10)), 5))
#[[10, 60, 110, 160], [20, 70, 120, 170], [30, 80, 130, 180], [40, 90, 140, 190]]
Related
i have some array
`
arr_values = [[71, 70, 80, 80, 50, 90, 100, 85, 75], [70, 70, 80, 80, 50, 50, 100, 85, 75], [70, 70, 80, 80, 50, 90, 100, 85, 78]],
[[70, 70, 80, 80, 50, 90, 100, 85, 75], [70, 70, 80, 80, 50, 50, 100, 85, 75], [70, 70, 80, 80, 50, 90, 100, 85, 79]],
[[70, 70, 80, 80, 50, 90, 100, 85, 75], [70, 70, 80, 80, 50, 50, 100, 85, 75], [70, 70, 80, 80, 50, 90, 100, 85, 80]],
[[70, 70, 80, 80, 50, 90, 100, 85, 75], [70, 70, 80, 80, 50, 50, 100, 85, 75], [70, 70, 80, 80, 50, 90, 100, 85, 81]],
[[70, 70, 80, 80, 50, 90, 100, 85, 73], [70, 70, 80, 80, 50, 50, 100, 85, 74], [70, 70, 80, 80, 50, 90, 100, 85, 76], [70, 70, 80, 80, 50, 90, 100, 82, 73]]
`
i want it become like this
`
[[701] [660] [703]]
[[700] [660] [704]]
[[700] [660] [705]]
[[700] [660] [706]]
[[698] [659] [701] [695]]
`
i have try with this way
`
for values in arr_values:
for value in range(len(values)):
a=0
for a in values[value]:
a += a
print(a)
`
the result is not what i want. how to loop correctly?
`
for values in arr_values:
for value in range(len(values)):
a=0
for a in values[value]:
a += a
print(a)
`
the result is not what i want. how to loop correctly?
If you want to return a list of sums, do it by creating an empty list and then appending each sum iteratively.
arraySums = []
for subArray in arr_values:
arrays = []
for array in subArray:
curSum = 0
for elem in array:
curSum += elem
arrays.append([curSum])
arraySums.append(arrays)
Note: arr_values is a tuple of lists
Explanation: Create a list that will hold the result of everything outside of the loop so you can store the results. After that, iterate over the arr_values tuple to get the 2d lists you'll be summing. The inner two for loops are then the sum of a 2 dimensional list, appended to the master list after.
I wanted to create a random array with the lenght of 24 in python, by assigning the values of a list seq=[30,170]. So I wanted to have an array which looks like a=[30,30,30,170,30,170,...]. The problem is, that the values shouldn't be assigned completely random, but with a certain probability. In this case, if the first value is 30 (or 170) the probability of the next value to be 30 (or 170) as well, should be 90% and the probability of the next value to be 170 (or 30) should be 10%.
Does anyone know how to do this? For the moment I only found how to arrange them randomly. Thanks!!
import random
seq=[30,170]
a = random.choices(seq,k=24)
You can provide weights to random.choices(population, weights=None, *, cum_weights=None, k=1).
You need to do it iteratively to be able to reference the last value when looking up weight you need to use:
import random
seq = [30, 170]
# weight lookup
wgt = {30: (90, 10), 170:(10, 90)}
r = []
for _ in range(24):
if not r:
# need some initial value: 50/50
r.append(random.choice(seq))
else:
# k == 1, using correct weights from lookup, use only single value and add it
r.append(random.choices(seq, weights=wgt[r[-1]], k=1)[0])
print(r)
Output over 8 runs:
[170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170]
[30, 30, 30, 30, 30, 30, 170, 170, 170, 170, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]
[30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]
[30, 30, 30, 30, 30, 170, 170, 170, 170, 170, 170, 170, 170, 30, 30, 170, 170, 170, 170, 170, 170, 170, 170, 170]
[170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 30, 30, 30, 170, 170, 170, 170]
[170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 30, 30, 30, 30, 30, 30, 30, 30, 30]
[30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]
[30, 30, 170, 170, 30, 170, 170, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]
You can also provide the weighting yourself:
seq = [30,170]
w = {30: (30,30,30,30,30,30,30,30,30,170), # if 30: use 9 * 30 + 1 * 170
170:(30,170,170,170,170,170,170,170,170,170)} # if 170: use 9 * 170 + 1 * 30
r = []
for _ in range(24):
if not r:
r.append(random.choice(seq))
else:
r.append(random.choice(w[r[-1]]))
I have the following box plot which plots some values with different mean and median values for each box; I am wondering if there is any way to label them so that they appear on the graph legend (because the current box plot plots an orange line for the median and a blue dot for the mean and it is not so clear which is which)? Also is there a way to make one legend for these subplots, instead of having a legend for each one, since they are essentially the same objects just different data?
Here's a code example for one of the subplots, the other subplots are the same but have different data:
fig = plt.figure()
xlim = (4, 24)
ylim = (0, 3700)
plt.subplot(1,5,5)
x_5_diff = {5: [200, 200, 291, 200, 291, 200, 291, 200, 291, 200, 291, 200, 291, 200, 291],
7: [161, 161, 179, 161, 179, 161, 179, 161, 179, 161, 179, 161, 179, 161, 179],
9: [205, 205, 109, 205, 109, 205, 109, 205, 109, 205, 109, 205, 109, 205, 109],
11: [169, 169, 95, 169, 95, 169, 95, 169, 95, 169, 95, 169, 95, 169, 95],
13: [43, 43, 70, 43, 70, 43, 70, 43, 70, 43, 70, 43, 70, 43, 70],
15: [33, 33, 39, 33, 39, 33, 39, 33, 39, 33, 39, 33, 39, 33, 39],
17: [23, 23, 126, 23, 126, 23, 126, 23, 126, 23, 126, 23, 126, 23, 126],
19: [17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17],
21: [15, 15, 120, 15, 120, 15, 120, 15, 120, 15, 120, 15, 120, 15, 120],
23: [63, 63, 25, 63, 25, 63, 25, 63, 25, 63, 25, 63, 25, 63, 25]}
keys = sorted(x_5_diff)
plt.boxplot([x_5_diff[k] for k in keys], positions=keys) # box-and-whisker plot
plt.hlines(y = 1600, colors= 'r', xmin = 5, xmax = 23, label = "Level 1 Completed")
plt.title("x = 5 enemies")
plt.ylim(0,3700)
plt.plot(keys, [sum(x_5_diff[k]) / len(x_5_diff[k]) for k in keys], '-o')
plt.legend()
plt.show()
Any help would be appreciated.
Its a bit late, but try this:
bp = plt.boxplot([x_5_diff[k] for k in keys], positions=keys)
# You can access boxplot items using ist dictionary
plt.legend([bp['medians'][0], bp['means'][0]], ['median', 'mean'])
Store the mean as a separate vector. Loop over the vectors to plot.
(Will try to give implementation, as soon as I have my laptop)
can someone explain me the last line of the code briefly about list comprehension
Tried to understand with different range values
def Function_1(x):
return x*2
def Function_2(x):
return x*4
empty_list = []
for i in range(16):
empty_list.append(Function_1(Function_2(i)))
print(empty_list)
print([Function_1(x) for x in range(64) if x in [Function_2(j) for j in range(16)]])
Output:
[0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120]
[0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120]
Okay, what the last line does is,
[(2 * x) for x in range(64)], which gives you a list of 64 numbers (multiplied by 2), and then we have a condition saying if x in [(4 * j) for j in range(16)]. It will check in the second list if the same number from the first lists exists and only those numbers will be considered in the final OP.
OP function_1:
[(2 * x) for x in range(64)]
# [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126]
OP function_2:
[(4 * j) for j in range(16)]
# [0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60]
Find if there are numbers from the first list in the second list and call them x, and return 2 * x
print([x*2 for x in range(64) if x in [j*4 for j in range(16)]])
# [0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120]
Maybe this will help:
The line
output = [function_1(x) for x in range(64) if x in [function_2(j) for j in range(16)]]
is equivalent to
output = []
y = [function_2(j) for j in range(16)]
for x in range(64):
if x in y:
output.append(function_1(x))
Python represents escape sequences with \ as I understand. So if I tryo to insert a single backslash into a string, I get the string variable with double backslashes as below:
x = '/x91/x84/xa4/x74'
b = x.replace(r'/', '\\')
>>> b
'\\x91\\x84\\xa4\\x74'
But then If I have two bytes objects - one with single backslash and another with double backslashes, and give them each to pandas.read_msgpack() function, why does it give different outputs in each case? Please see what I have tried below:
byte_obj1 = b'\x91\x84\xa4\x74\x69\x6d\x65\x92\xcb\x41\xdd\xcd\x65\x00\x00\x00\x00\xcb\x41\xdd\xcd\x65\x00\x00\xa3\xd7\xa4\x76\x61\x72\x30\x92\xcb\x40\x49\x0c\xcc\xcc\xcc\xcc\xcd\xcb\x40\x49\x0c\xcc\xcc\xcc\xcc\xcd\xa4\x76\x61\x72\x31\x92\xcb\xff\xf8\x00\x00\x00\x00\x00\x00\xcb\x40\x4e\x0c\xcc\xcc\xcc\xcc\xcd\xa4\x76\x61\x72\x32\x92\xcb\xff\xf8\x00\x00\x00\x00\x00\x00\xcb\xff\xf8\x00\x00\x00\x00\x00\x00'
d1=pandas.read_msgpack(byte_obj1)
>>> d1
({'time': (2000000000.0, 2000000000.01), 'var0': (50.1, 50.1), 'var1': (nan, 60.1), 'var2': (nan, nan)},)
byte_obj2=
b'\\x91\\x84\\xa4\\x74\\x69\\x6d\\x65\\x92\\xcb\\x41\\xdd\\xcd\\x65\\x00\\x00\\x00\\x00\\xcb\\x41\\xdd\\xcd\\x65\\x00\\x00\\xa3\\xd7\\xa4\\x76\\x61\\x72\\x30\\x92\\xcb\\x40\\x49\\x0c\\xcc\\xcc\\xcc\\xcc\\xcd\\xcb\\x40\\x49\\x0c\\xcc\\xcc\\xcc\\xcc\\xcd\\xa4\\x76\\x61\\x72\\x31\\x92\\xcb\\xff\\xf8\\x00\\x00\\x00\\x00\\x00\\x00\\xcb\\x40\\x4e\\x0c\\xcc\\xcc\\xcc\\xcc\\xcd\\xa4\\x76\\x61\\x72\\x32\\x92\\xcb\\xff\\xf8\\x00\\x00\\x00\\x00\\x00\\x00\\xcb\\xff\\xf8\\x00\\x00\\x00\\x00\\x00\\x00'
d2=pandas.read_msgpack(byte_obj2)
>>> d2
[92, 120, 57, 49, 92, 120, 56, 52, 92, 120, 97, 52, 92, 120, 55, 52, 92, 120, 54, 57, 92, 120, 54, 100, 92, 120, 54, 53, 92, 120, 57, 50, 92, 120, 99, 98, 92, 120, 52, 49, 92, 120, 100, 100, 92, 120, 99, 100, 92, 120, 54, 53, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 99, 98, 92, 120, 52, 49, 92, 120, 100, 100, 92, 120, 99, 100, 92, 120, 54, 53, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 97, 51, 92, 120, 100, 55, 92, 120, 97, 52, 92, 120, 55, 54, 92, 120, 54, 49, 92, 120, 55, 50, 92, 120, 51, 48, 92, 120, 57, 50, 92, 120, 99, 98, 92, 120, 52, 48, 92, 120, 52, 57, 92, 120, 48, 99, 92, 120, 99, 99, 92, 120, 99, 99, 92, 120, 99, 99, 92, 120, 99, 99, 92, 120, 99, 100, 92, 120, 99, 98, 92, 120, 52, 48, 92, 120, 52, 57, 92, 120, 48, 99, 92, 120, 99, 99, 92, 120, 99, 99, 92, 120, 99, 99, 92, 120, 99, 99, 92, 120, 99, 100, 92, 120, 97, 52, 92, 120, 55, 54, 92, 120, 54, 49, 92, 120, 55, 50, 92, 120, 51, 49, 92, 120, 57, 50, 92, 120, 99, 98, 92, 120, 102, 102, 92, 120, 102, 56, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 99, 98, 92, 120, 52, 48, 92, 120, 52, 101, 92, 120, 48, 99, 92, 120, 99, 99, 92, 120, 99, 99, 92, 120, 99, 99, 92, 120, 99, 99, 92, 120, 99, 100, 92, 120, 97, 52, 92, 120, 55, 54, 92, 120, 54, 49, 92, 120, 55, 50, 92, 120, 51, 50, 92, 120, 57, 50, 92, 120, 99, 98, 92, 120, 102, 102, 92, 120, 102, 56, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 99, 98, 92, 120, 102, 102, 92, 120, 102, 56, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48]
Why does Python not consider double backslahes and '\' same as in case of escape
sequence? Could someone please help me in this dilemma. Thank you very much in advance.
In your initial setting, you wrote x = '/x91/x84/xa4/x74'. These are forward slashes and not backward slashes. Backward slashes in python are escape characters, so the first backslash in a double backslash functions as an escape character for the second backslash.