Display the list of dates in a proper format - python

I am running this code to get a date
from datetime import timedelta, date
def daterange(start_date, end_date):
for n in range(int(start_date.day), int((end_date - start_date).days), 90):
yield start_date + timedelta(n)
start_date = date(2016, 1, 1)
end_date = date.today()
for single_date in daterange(start_date, end_date):
x = list(single_date.strftime("%Y-%m-%d"))
print(x)
and its giving me this output
['2', '0', '1', '6', '-', '0', '1', '-', '0', '2']
['2', '0', '1', '6', '-', '0', '4', '-', '0', '1']
['2', '0', '1', '6', '-', '0', '6', '-', '3', '0']
['2', '0', '1', '6', '-', '0', '9', '-', '2', '8']
['2', '0', '1', '6', '-', '1', '2', '-', '2', '7']
['2', '0', '1', '7', '-', '0', '3', '-', '2', '7']
['2', '0', '1', '7', '-', '0', '6', '-', '2', '5']
['2', '0', '1', '7', '-', '0', '9', '-', '2', '3']
['2', '0', '1', '7', '-', '1', '2', '-', '2', '2']
['2', '0', '1', '8', '-', '0', '3', '-', '2', '2']
['2', '0', '1', '8', '-', '0', '6', '-', '2', '0']
['2', '0', '1', '8', '-', '0', '9', '-', '1', '8']
['2', '0', '1', '8', '-', '1', '2', '-', '1', '7']
['2', '0', '1', '9', '-', '0', '3', '-', '1', '7']
['2', '0', '1', '9', '-', '0', '6', '-', '1', '5']
['2', '0', '1', '9', '-', '0', '9', '-', '1', '3']
['2', '0', '1', '9', '-', '1', '2', '-', '1', '2']
Process finished with exit code 0
However, I want the list to display in date in a proper date format, something like this [2019-12-12, 2019-09-13, ...]
My goal is to create a list of dates, which I will be using in Gmail to search for emails. So it needs to be in a format that Gmail understands. My current level of coding/python is at beginners so it might be a very simple solution that I am currently missing. Any help appreciated.

Why did you make a list of the characters? All you need is the formatting you already did correctly:
print(single_date.strftime("%Y-%m-%d"))

you can use a list comprehension:
x = [sd.strftime("%Y-%m-%d") for sd in daterange(start_date, end_date)]
print(x)
output:
['2016-01-02', '2016-04-01', '2016-06-30', '2016-09-28', '2016-12-27', '2017-03-27', '2017-06-25', '2017-09-23', '2017-12-22', '2018-03-22', '2018-06-20', '2018-09-18', '2018-12-17', '2019-03-17', '2019-06-15', '2019-09-13', '2019-12-12']

Related

Python - Cryptographic Feistel function (AUT64) - Math/programming trouble in implementation

Don't know how to add math symbols, hence the image paste:
Based on this:
https://tches.iacr.org/index.php/TCHES/article/view/874/826 (page 52)
I managed, I think to implement compression function (1)
However, don't understand what 2,3 and 4 mean.
Can somebody with math/programming knowledge explain it? Ideally, how to implement it in Python.
Here is my code snippet:
aut64.py
import ctypes
def hexTobinary(hexdigits):
binarydigits = ""
for hexdigit in hexdigits:
binarydigits += bin(int(hexdigit, 16))[2:].zfill(4)
return binarydigits
t_offset = [['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'],
['0', '2', '4', '6', '8', 'A', 'C', 'E', '3', '1', '7', '5', 'B', '9', 'F', 'D'],
['0', '3', '6', '5', 'C', 'F', 'A', '9', 'B', '8', 'D', 'E', '7', '4', '1', '2'],
['0', '4', '8', 'C', '3', '7', 'B', 'F', '6', '2', 'E', 'A', '5', '1', 'D', '9'],
['0', '5', 'A', 'F', '7', '2', 'D', '8', 'E', 'B', '4', '1', '9', 'C', '3', '6'],
['0', '6', 'C', 'A', 'B', 'D', '7', '1', '5', '3', '9', 'F', 'E', '8', '2', '4'],
['0', '7', 'E', '9', 'F', '8', '1', '6', 'D', 'A', '3', '4', '2', '5', 'C', 'B'],
['0', '8', '3', 'B', '6', 'E', '5', 'D', 'C', '4', 'F', '7', 'A', '2', '9', '1'],
['0', '9', '1', '8', '2', 'B', '3', 'A', '4', 'D', '5', 'C', '6', 'F', '7', 'E'],
['0', 'A', '7', 'D', 'E', '4', '9', '3', 'F', '5', '8', '2', '1', 'B', '6', 'C'],
['0', 'B', '5', 'E', 'A', '1', 'F', '4', '7', 'C', '2', '9', 'D', '6', '8', '3'],
['0', 'C', 'B', '7', '5', '9', 'E', '2', 'A', '6', '1', 'D', 'F', '3', '4', '8'],
['0', 'D', '9', '4', '1', 'C', '8', '5', '2', 'F', 'B', '6', '3', 'E', 'A', '7'],
['0', 'E', 'F', '1', 'D', '3', '2', 'C', '9', '7', '6', '8', '4', 'A', 'B', '5'],
['0', 'F', 'D', '2', '9', '6', '4', 'B', '1', 'E', 'C', '3', '8', '7', '5', 'A']]
tu = ['1', '0', '3', '2', '5', '4', '7', '6',
'0', '1', '2', '3', '4', '5', '6', '7',
'3', '2', '1', '0', '7', '6', '5', '4',
'2', '3', '0', '1', '6', '7', '4', '5',
'5', '4', '7', '6', '1', '0', '3', '2',
'4', '5', '6', '7', '0', '1', '2', '3',
'7', '6', '5', '4', '3', '2', '1', '0',
'6', '7', '4', '5', '2', '3', '0', '1',
'3', '2', '1', '0', '7', '6', '5', '4',
'2', '3', '0', '1', '6', '7', '4', '5',
'1', '0', '3', '2', '5', '4', '7', '6',
'0', '1', '2', '3', '4', '5', '6', '7',
'7', '6', '5', '4', '3', '2', '1', '0',
'6', '7', '4', '5', '2', '3', '0', '1',
'5', '4', '7', '6', '1', '0', '3', '2',
'4', '5', '6', '7', '0', '1', '2', '3',
'2', '3', '0', '1', '6', '7', '4', '5',
'3', '2', '1', '0', '7', '6', '5', '4',
'0', '1', '2', '3', '4', '5', '6', '7',
'1', '0', '3', '2', '5', '4', '7', '6',
'6', '7', '4', '5', '2', '3', '0', '1',
'7', '6', '5', '4', '3', '2', '1', '0',
'4', '5', '6', '7', '0', '1', '2', '3',
'5', '4', '7', '6', '1', '0', '3', '2']
tl = ['4', '5', '6', '7', '0', '1', '2', '3',
'5', '4', '7', '6', '1', '0', '3', '2',
'6', '7', '4', '5', '2', '3', '0', '1',
'7', '6', '5', '4', '3', '2', '1', '0',
'0', '1', '2', '3', '4', '5', '6', '7',
'1', '0', '3', '2', '5', '4', '7', '6',
'2', '3', '0', '1', '6', '7', '4', '5',
'3', '2', '1', '0', '7', '6', '5', '4',
'5', '4', '7', '6', '1', '0', '3', '2',
'4', '5', '6', '7', '0', '1', '2', '3',
'7', '6', '5', '4', '3', '2', '1', '0',
'6', '7', '4', '5', '2', '3', '0', '1',
'1', '0', '3', '2', '5', '4', '7', '6',
'0', '1', '2', '3', '4', '5', '6', '7',
'3', '2', '1', '0', '7', '6', '5', '4',
'2', '3', '0', '1', '6', '7', '4', '5',
'6', '7', '4', '5', '2', '3', '0', '1',
'7', '6', '5', '4', '3', '2', '1', '0',
'4', '5', '6', '7', '0', '1', '2', '3',
'5', '4', '7', '6', '1', '0', '3', '2',
'2', '3', '0', '1', '6', '7', '4', '5',
'3', '2', '1', '0', '7', '6', '5', '4',
'0', '1', '2', '3', '4', '5', '6', '7',
'1', '0', '3', '2', '5', '4', '7', '6']
w = 0xFFFFFF00
val = ctypes.c_uint32(~w).value
kg = [val]
kq = ['1', '2', '3', '0', '0', '0', '0', '0']
kt = ['1', '2', '3', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0']
AUT64_key = []
AUT64_key.extend(kg)
AUT64_key.extend(kq)
AUT64_key.extend(kt)
def apply_permutation_r(P_TABLE, PLAINTEXT):
permutated_M = ""
for index in P_TABLE:
permutated_M += PLAINTEXT[int(index) - 1]
return permutated_M
def apply_compression_g(o1, r_no, lk, uk):
ln = int(o1[0:4], 2)
un = int(o1[4:8], 2)
gl = int(t_offset[int(lk)][int(ln)], 16)
gu = int(t_offset[int(uk)][int(un)], 16)
g = gl >> 4 | gu
return g
def uk_round_key(i, r_no):
uk = int(tu[(r_no * 8) + i])
return uk
def lk_round_key(i, r_no):
lk = int(tl[(r_no * 8) + i])
return lk
def apply_fiestel_f(o1, r_no, i):
uk = uk_round_key(i, r_no)
lk = lk_round_key(i, r_no)
o2 = apply_compression_g(o1, r_no, lk, uk)
# o3 = apply_substitution_s(o2)
# o4 = apply_permutation_bitwise(o3)
# o5 = apply_permutation_substitution_s(o4)
# return o5
return o2
M = '01234567'
plaintext = hexTobinary(M)
enc = []
for r_no in range(8):
o1 = apply_permutation_r(kq, plaintext)
for i in range(8):
enc.append(apply_fiestel_f(o1, r_no, i))
print enc
Any help is greatly appreciate. I am a beginner, this above could be all wrong :)
Thanks in advance!

how to append items vertically python?

I have this list :
List_of_all = [
['3', '0', '6', '5', '0', '8', '4', '0', '0'],
['5', '2', '0', '0', '0', '0', '0', '0', '0'],
['0', '8', '7', '0', '0', '0', '0', '3', '1'],
['0', '0', '3', '0', '1', '0', '0', '8', '0'],
['9', '0', '0', '8', '6', '3', '0', '0', '5'],
['0', '5', '0', '0', '9', '0', '6', '0', '0'],
['1', '3', '0', '0', '0', '0', '2', '5', '0'],
['0', '0', '0', '0', '0', '0', '0', '7', '4'],
['0', '0', '5', '2', '0', '6', '3', '0', '0']
]
And I need to also 9 vertically list of items
My code :
vertical = []
x = []
n = 0
for List in List_of_all:
for num in List[n]:
x.append(num)
vertical.append(x)
x = []
n += 1
if n == 9:
break
My out put :
[['3'], ['2'], ['7'], ['0'], ['6'], ['0'], ['2'], ['7'], ['0']]
Why this iteration dont't work true ?!
How should I define this to get
Bold assumption: you are looking for the following transposition:
>>> list(map(list, zip(*List_of_all)))
[['3', '5', '0', '0', '9', '0', '1', '0', '0'],
['0', '2', '8', '0', '0', '5', '3', '0', '0'],
['6', '0', '7', '3', '0', '0', '0', '0', '5'],
['5', '0', '0', '0', '8', '0', '0', '0', '2'],
['0', '0', '0', '1', '6', '9', '0', '0', '0'],
['8', '0', '0', '0', '3', '0', '0', '0', '6'],
['4', '0', '0', '0', '0', '6', '2', '0', '3'],
['0', '0', '3', '8', '0', '0', '5', '7', '0'],
['0', '0', '1', '0', '5', '0', '0', '4', '0']]
The zip(*...) idiom is the simplest way to go about that in pure Python. Without the nested list casting, it will be an iterator over tuples.
Basically, you are taking the diagonal array, you can do:
import numpy as np
vertical = np.diag(List_of_all)
print(vertical)
array(['3', '2', '7', '0', '6', '0', '2', '7', '0'], dtype='<U1')
# to put each element in a list do this
vertical = [[x] for x in np.diag(List_of_all)]
print(vertical)
[['3'], ['2'], ['7'], ['0'], ['6'], ['0'], ['2'], ['7'], ['0']]
List_of_all = [
['3', '0', '6', '5', '0', '8', '4', '0', '0'],
['5', '2', '0', '0', '0', '0', '0', '0', '0'],
['0', '8', '7', '0', '0', '0', '0', '3', '1'],
['0', '0', '3', '0', '1', '0', '0', '8', '0'],
['9', '0', '0', '8', '6', '3', '0', '0', '5'],
['0', '5', '0', '0', '9', '0', '6', '0', '0'],
['1', '3', '0', '0', '0', '0', '2', '5', '0'],
['0', '0', '0', '0', '0', '0', '0', '7', '4'],
['0', '0', '5', '2', '0', '6', '3', '0', '0']
]
vertical=[]
for i in range(9):
print([x[i] for x in List_of_all])#for visual appeal only
vertical.append([x[i] for x in List_of_all])
print()
for line_slicer in range(0,9,3):
count=0
for line in vertical:
count+=1
print(line[line_slicer:line_slicer+3])
if count%3==0:
print()
Output:
['3', '5', '0', '0', '9', '0', '1', '0', '0']
['0', '2', '8', '0', '0', '5', '3', '0', '0']
['6', '0', '7', '3', '0', '0', '0', '0', '5']
['5', '0', '0', '0', '8', '0', '0', '0', '2']
['0', '0', '0', '1', '6', '9', '0', '0', '0']
['8', '0', '0', '0', '3', '0', '0', '0', '6']
['4', '0', '0', '0', '0', '6', '2', '0', '3']
['0', '0', '3', '8', '0', '0', '5', '7', '0']
['0', '0', '1', '0', '5', '0', '0', '4', '0']
['3', '5', '0']
['0', '2', '8']
['6', '0', '7']
['5', '0', '0']
['0', '0', '0']
['8', '0', '0']
['4', '0', '0']
['0', '0', '3']
['0', '0', '1']
['0', '9', '0']
['0', '0', '5']
['3', '0', '0']
['0', '8', '0']
['1', '6', '9']
['0', '3', '0']
['0', '0', '6']
['8', '0', '0']
['0', '5', '0']
['1', '0', '0']
['3', '0', '0']
['0', '0', '5']
['0', '0', '2']
['0', '0', '0']
['0', '0', '6']
['2', '0', '3']
['5', '7', '0']
['0', '4', '0']
If you want a list of the first elements in all of your lists:
vertical=[i[0] for i in List_of_all]
this would be the same as
vertical= ['3','5','0','0','9','0','1','0','0']
You can get the columns as folows:
from collections import defaultdict
List_of_all = [
['3', '0', '6', '5', '0', '8', '4', '0', '0'],
['5', '2', '0', '0', '0', '0', '0', '0', '0'],
['0', '8', '7', '0', '0', '0', '0', '3', '1'],
['0', '0', '3', '0', '1', '0', '0', '8', '0'],
['9', '0', '0', '8', '6', '3', '0', '0', '5'],
['0', '5', '0', '0', '9', '0', '6', '0', '0'],
['1', '3', '0', '0', '0', '0', '2', '5', '0'],
['0', '0', '0', '0', '0', '0', '0', '7', '4'],
['0', '0', '5', '2', '0', '6', '3', '0', '0']
]
if __name__ == '__main__':
columns = defaultdict(list)
for row in List_of_all:
for j, item in enumerate(row):
columns[j].append(item)
print(columns[0]) # ['3', '5', '0', '0', '9', '0', '1', '0', '0']
It is unclear what you expect of output. If you want to get all the columns in the 2D list, one option is to use numpy:
>>> import numpy as np
>>> items = np.array([[1,2,3], [4,5,6], [7,8,9]])
>>> items
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> columns = items.T
>>> columns
array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
If you transpose the numpy array, the columns will be rows and vice versa.
Another way to do this is with numpy indexing:
>>> columns = [items[:, i] for i in range(len(items[0]))]
>>> columns
[array([1, 4, 7]), array([2, 5, 8]), array([3, 6, 9])]
>>>
If you simply want to use loops, you should iterate column-wise and not row-wise:
>>> columns = []
>>> for j in range(len(items[0])):
... column = []
... for i in range(len(items)):
... column.append(items[i, j])
... columns.append(column)
...
>>> columns
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
A more pythonic way for the latter solution would be with list comprehensions:
>>> [[items[i][j] for i in range(len(items))] for j in range(len(items[0]))]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
That said, the solution provided by #schwobaseggl is a good way to do it too, very pythonic.
One-liner stolen from GeeksforGeeks:
t = [[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]

Transformation of a group of real values to a list

I am collecting measurements from an equipment in .csv format. Each cell has a group of values separated by comma. Example is given below,
cell = 0.0871666666666667,0.0866,0.0862,0.083,0.0834,0.0812857142857143,0.08075,0.0782,0.0751,0.0748181818181818,0.0737142857142857,0.072,0.07,0.07,0.0680833333333333,0.068,0.0654615384615385,0.0641818181818182,0.0616428571428571,0.0615714285714286,0.0599411764705882,0.0587857142857143,0.0573478260869565,0.0554285714285714,0.0545652173913044,0.0523225806451613,0.0511818181818182,0.0492553191489362,0.0480322580645161,
Above cell is on object format.
I want to convert this to a list of float values as
desired_output = [0.0871666666666667,0.0866,0.0862,0.083,0.0834,0.0812857142857143,0.08075,0.0782,0.0751,0.0748181818181818,0.0737142857142857,0.072,0.07,0.07,0.0680833333333333,0.068,0.0654615384615385,0.0641818181818182,0.0616428571428571,0.0615714285714286,0.0599411764705882,0.0587857142857143,0.0573478260869565,0.0554285714285714,0.0545652173913044,0.0523225806451613,0.0511818181818182,0.0492553191489362,0.0480322580645161]
I did the following conversion. But it did not do as I wanted above. My code is given below.
actual_output = list(cell)
print(actual_output)
actual_output = ['0', '.', '0', '9', '3', ',', '0', '.', '0', '9', '1', '4', '2', '8', '5', '7', '1', '4', '2', '8', '5', '7', '1', '4', ',', '0', '.', '0', '9', '0', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '7', ',', '0', '.', '0', '8', '7', '1', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '7', ',', '0', '.', '0', '8', '6', '6', ',', '0', '.', '0', '8', '6', '2', ',', '0', '.', '0', '8', '3', ',', '0', '.', '0', '8', '3', '4', ',', '0', '.', '0', '8', '1', '2', '8', '5', '7', '1', '4', '2', '8', '5', '7', '1', '4', '3', ',', '0', '.', '0', '8', '0', '7', '5', ',', '0', '.', '0', '7', '8', '2', ',', '0', '.', '0', '7', '5', '1', ',', '0', '.', '0', '7', '4', '8', '1', '8', '1', '8', '1', '8', '1', '8', '1', '8', '1', '8', ',', '0', '.', '0', '7', '3', '7', '1', '4', '2', '8', '5', '7', '1', '4', '2', '8', '5', '7', ',', '0', '.', '0', '7', '2', ',', '0', '.', '0', '7', ',', '0', '.', '0', '7', ',', '0', '.', '0', '6', '8', '0', '8', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', ',', '0', '.', '0', '6', '8', ',', '0', '.', '0', '6', '5', '4', '6', '1', '5', '3', '8', '4', '6', '1', '5', '3', '8', '5', ',', '0', '.', '0', '6', '4', '1', '8', '1', '8', '1', '8', '1', '8', '1', '8', '1', '8', '2', ',', '0', '.', '0', '6', '1', '6', '4', '2', '8', '5', '7', '1', '4', '2', '8', '5', '7', '1', ',', '0', '.', '0', '6', '1', '5', '7', '1', '4', '2', '8', '5', '7', '1', '4', '2', '8', '6', ',', '0', '.', '0', '5', '9', '9', '4', '1', '1', '7', '6', '4', '7', '0', '5', '8', '8', '2', ',', '0', '.', '0', '5', '8', '7', '8', '5', '7', '1', '4', '2', '8', '5', '7', '1', '4', '3', ',', '0', '.', '0', '5', '7', '3', '4', '7', '8', '2', '6', '0', '8', '6', '9', '5', '6', '5', ',', '0', '.', '0', '5', '5', '4', '2', '8', '5', '7', '1', '4', '2', '8', '5', '7', '1', '4', ',', '0', '.', '0', '5', '4', '5', '6', '5', '2', '1', '7', '3', '9', '1', '3', '0', '4', '4', ',', '0', '.', '0', '5', '2', '3', '2', '2', '5', '8', '0', '6', '4', '5', '1', '6', '1', '3', ',', '0', '.', '0', '5', '1', '1', '8', '1', '8', '1', '8', '1', '8', '1', '8', '1', '8', '2', ',', '0', '.', '0', '4', '9', '2', '5', '5', '3', '1', '9', '1', '4', '8', '9', '3', '6', '2', ',', '0', '.', '0', '4', '8', '0', '3', '2', '2', '5', '8', '0', '6', '4', '5', '1', '6', '1', ',']
Any suggestions please.
One of suggested approach:
x = 2.809,2.812,2.813,2.808,2.804
print(x)
str_list = x.split(',')
print(str_list)
flt_lst = [float(a) for a in str_list]
print(flt_lst)
Output is:
x = 2.809,2.812,2.813,2.808,2.804
str_list = ['2.809', '2.812', '2.813', '2.808', '2.804']
string could not be converted to float
Your variable cell is a string so your list call is making it an iterable. Try
str_lst = cell.split(',')
float_lst = [float(x) for x in str_lst]

how to sort python list in a way that if 1 should come before and 10 and 2 before 20

['2', '8', '2', '3', '6', '4', '1', '1', '10', '6', '3', '3', '6', '1', '3', '8', '4', '6', '1', '10', '8', '4', '10', '4', '1', '3', '2', '3', '2', '6', '1', '5', '2', '9', '8', '5', '10', '8', '7', '9', '6', '4', '2', '6', '3', '8', '8', '9', '8', '2', '9', '10', '3', '10', '7', '5', '7', '1', '7', '5', '1', '4', '7', '6', '1', '10', '5', '4', '8', '4', '2', '7', '8', '1', '1', '7', '4', '1', '1', '9', '8', '6', '5', '9', '9', '3', '7', '6', '3', '10', '8', '10', '7', '2', '5', '1', '1', '9', '9', '5']
after using lambda function inf following way:
a.sort(key=lambda a: int(a.split()[0]))
a = a[::-1]
I got
['10', '10', '10', '10', '10', '10', '10', '10', '10', '9', '9', '9', '9', '9', '9', '9', '9', '9', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '7', '7', '7', '7', '7', '7', '7', '7', '7', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '5', '5', '5', '5', '5', '5', '5', '5', '4', '4', '4', '4', '4', '4', '4', '4', '4', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '2', '2', '2', '2', '2', '2', '2', '2', '2', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1']
But i want
10 in end after 1 , likewise if put 20 and 2 in list than 2 should come before 20 and 20 before 10 etc
The operation:
a.sort()
with no other options sets a to:
['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '10', '10', '10', '10', '10', '10', '10', '10', '10', '2', '2', '2', '2', '2', '2', '2', '2', '2', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '4', '4', '4', '4', '4', '4', '4', '4', '4', '5', '5', '5', '5', '5', '5', '5', '5', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '7', '7', '7', '7', '7', '7', '7', '7', '7', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '9', '9', '9', '9', '9', '9', '9', '9', '9']
You want an ordered list of strings. Those strings are representations of numbers. Now you want a grouped sorting. First everything that starts with a '9', then '8' and down to '1'. In each of those groups the values should be sorted in numeric order.
An example list:
a = ['11', '105', '2', '8', '2', '3', '6', '4', '1', '1', '10', '81', '3', '3', '5', '10', '8', '7', '9', '6', '4', '2']
Now let's do a grouped sorting with a.sort(key=lambda v: v[0]):
['11', '105', '1', '1', '10', '10', '2', '2', '2', '3', '3', '3', '4', '4', '5', '6', '6', '7', '8', '81', '8', '9']
We see, that the values are grouped now, but we want the values starting with '9' first. We're going to fix this by reversing the result with a.sort(key=lambda v: v[0], reversed=True)
['9', '8', '81', '8', '7', '6', '6', '5', '4', '4', '3', '3', '3', '2', '2', '2', '11', '105', '1', '1', '10', '10']
The groups are correct, now we have to sort the values in the groups. So after the sorting according to the first character we have to sort the value by number. That's easy, we just have to create a tuple for the key: a.sort(key=lambda v: (v[0], int(v)), reverse=True)
['9', '81', '8', '8', '7', '6', '6', '5', '4', '4', '3', '3', '3', '2', '2', '2', '105', '11', '10', '10', '1', '1']
OK, the values are sorted now, but we have to reverse them in the groups. The easiest way to do that ist to take the negative number: a.sort(key=lambda v: (v[0], -int(v)), reverse=True).
['9', '8', '8', '81', '7', '6', '6', '5', '4', '4', '3', '3', '3', '2', '2', '2', '1', '1', '10', '10', '11', '105']
you can use the following sample using key=str
integers = ['2', '8', '2', '3', '6', '4', '1', '1', '10', '6', '3', '3', '6', '1', '3', '8', '4', '6', '1', '10', '8', '4', '10', '4', '1', '3', '2', '3', '2', '6', '1', '5', '2', '9', '8', '5', '10', '8', '7', '9', '6', '4', '2', '6', '3', '8', '8', '9', '8', '2', '9', '10', '3', '10', '7', '5', '7', '1', '7', '5', '1', '4', '7', '6', '1', '10', '5', '4', '8', '4', '2', '7', '8', '1', '1', '7', '4', '1', '1', '9', '8', '6', '5', '9', '9', '3', '7', '6', '3', '10', '8', '10', '7', '2', '5', '1', '1', '9', '9', '5']
print(sorted(integers, key=str))
you don't need to use lambda method here. Instead of using lambda you can use '.sort()' method to sort the items in the list. like this one:
li=['2', '8', '2', '3', '6', '4', '1', '1', '10', '6', '3', '3', '6', '1', '3', '8', '4', '6', '1', '10', '8', '4', '10', '4', '1', '3', '2', '3', '2', '6', '1', '5', '2', '9', '8', '5', '10', '8', '7', '9', '6', '4', '2', '6', '3', '8', '8', '9', '8', '2', '9', '10', '3', '10', '7', '5', '7', '1', '7', '5', '1', '4', '7', '6', '1', '10', '5', '4', '8', '4', '2', '7', '8', '1', '1', '7', '4', '1', '1', '9', '8', '6', '5', '9', '9', '3', '7', '6', '3', '10', '8', '10', '7', '2', '5', '1', '1', '9', '9', '5','20']
li.sort()
print(li)
output:
['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '10', '10', '10', '10', '10', '10', '10', '10', '10', '2', '2', '2', '2', '2', '2', '2', '2', '2', '20', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '4', '4', '4', '4', '4', '4', '4', '4', '4', '5', '5', '5', '5', '5', '5', '5', '5', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '7', '7', '7', '7', '7', '7', '7', '7', '7', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '9', '9', '9', '9', '9', '9', '9', '9', '9']
I hope you get your answere.
This might helps you:
a.sort(key=str)
or
new list = sorted(a, key=str) # if you dont want to change a
this will sort the list as you want even if the items are integer

missing last bin in histogram plot from matplot python

I'm trying to draw histrogram based of my value
x = ['3', '1', '4', '1', '5', '9', '2', '6', '5', '3', '5',
'2', '3', '4', '5', '6', '4', '2', '0', '1', '9', '8',
'8', '8', '8', '8', '9', '3', '8', '0', '9', '5', '2',
'5', '7', '2', '0', '1', '0', '6', '5']
x_num = [int(i) for i in x]
key = '0123456789'
for i in key:
print(i," count =>",x.count(i))
plt.hist(x_num, bins=[0,1,2,3,4,5,6,7,8,9])
The last 2 numbers "8, 9" bin should have distribution count of 6 , 4
But in histogram it combine 8 and 9 and get value of 10 instead of separate them. Total number of bin should be 10 => but it only giving me graph of 9..
How could I separate them and break 8 and 9 ?
import matplotlib.pyplot as plt
x = ['3', '1', '4', '1', '5', '9', '2', '6', '5', '3', '5',
'2', '3', '4', '5', '6', '4', '2', '0', '1', '9', '8',
'8', '8', '8', '8', '9', '3', '8', '0', '9', '5', '2',
'5', '7', '2', '0', '1', '0', '6', '5']
x_num = [int(i) for i in x]
key = '0123456789'
for i in key:
print(i, " count =>", x.count(i))
plt.hist(x_num, bins=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10])
plt.show()

Categories

Resources