If you have a list of elements lets say:
res =
['(18,430)', '(19,430)', '(19,429)', '(19,428)', '(19,427)', '(18,426)', '(17,426)', '(17,425)', '(17,424)', '(17,423)', '(17,422)', '(17,421)', '(17,420)', '(16,421)', '(14,420)', '(11,419)', '(9,417)', '(7,416)', '(4,414)', '(3,414)', '(2,412)', '(1,412)', '(-1,410)', '(-2,409)', '(-2,408)', '(-3,407)', '(-3,406)', '(-3,405)', '(-3,404)', '(-3,403)', '(-3,402)', '(-3,401)', '(-3,400)', '(-4,399)', '(-4,398)', '(-5,398)', '(-6,398)', '(-7,397)', '(-7,396)', '(-6,395)', '(-5,395)', '(-4,393)', '(-3,391)', '(6,384)', '(12,378)', '(24,370)', '(42,358)', '(107,304)', '(151,255)', '(207,196)', '(259,121)', '(389,-28)', '(456,-84)', '(515,-134)', '(569,-182)', '(650,-260)', '(688,-294)', '(723,-317)', '(740,-328)', '(762,-342)', '(767,-347)', '(768,-349)', '(769,-352)', '(769,-357)', '(769,-359)', '(768,-361)', '(768,-364)', '(766,-370)', '(765,-371)', '(764,-374)', '(763,-376)', '(761,-378)', '(760,-381)', '(758,-385)', '(752,-394)', '(747,-401)', '(742,-407)', '(735,-413)', '(724,-421)', '(719,-424)', '(718,-425)', '(717,-425)'], ['(18,430)', '(19,430)', '(19,429)', '(19,428)', '(19,427)', '(18,426)', '(17,426)', '(17,425)', '(17,424)', '(17,423)', '(17,422)', '(17,421)', '(17,420)', '(16,421)', '(14,420)', '(11,419)', '(9,417)', '(7,416)', '(4,414)', '(3,414)', '(2,412)', '(1,412)', '(-1,410)', '(-2,409)', '(-2,408)', '(-3,407)', '(-3,406)', '(-3,405)', '(-3,404)', '(-3,403)', '(-3,402)', '(-3,401)', '(-3,400)', '(-4,399)', '(-4,398)', '(-5,398)', '(-6,398)', '(-7,397)', '(-7,396)', '(-6,395)', '(-5,395)', '(-4,393)', '(-3,391)', '(6,384)', '(12,378)', '(24,370)', '(42,358)', '(107,304)', '(151,255)', '(207,196)', '(259,121)', '(389,-28)', '(456,-84)', '(515,-134)', '(569,-182)', '(650,-260)', '(688,-294)', '(723,-317)', '(740,-328)', '(762,-342)', '(767,-347)', '(768,-349)', '(769,-352)', '(769,-357)', '(769,-359)', '(768,-361)', '(768,-364)', '(766,-370)', '(765,-371)', '(764,-374)', '(763,-376)', '(761,-378)', '(760,-381)', '(758,-385)', '(752,-394)', '(747,-401)', '(742,-407)', '(735,-413)', '(724,-421)', '(719,-424)', '(718,-425)', '(717,-425)']
and we want to make all these values into a matrix where we can update values.
All these values in the list are going to be the values of the rows and columns of a matrix?
Basically:
row1 = '(18,430)', row2 = '(19,430)', row3 = '(19,429)',.....,rown='(717,-425)', column1 = '(18,430)', column2 = '(19,430)', column3 = '(19,429)', ..... ,columnn= '(717,-425)'
How can we do that in Python and later I want to update values in the rows and columns? I tried to do this where I repeat the list and make it into a matrix.
But it does not give me what I want.
Res_List = [res,res]
print(np.array(Res_List))
So I am still wondering how we can do this in Python.
I also tried:
mat = np.array([res,res]).T
print(mat)
and it kind of gives me what I want but not quite.
This gives me:
[['(18,430)' '(18,430)']
['(19,430)' '(19,430)']
['(19,429)' '(19,429)']
['(19,428)' '(19,428)']
['(19,427)' '(19,427)']
['(18,426)' '(18,426)']
['(17,426)' '(17,426)']
['(17,425)' '(17,425)']
['(17,424)' '(17,424)']
['(17,423)' '(17,423)']
['(17,422)' '(17,422)']
['(17,421)' '(17,421)']
['(17,420)' '(17,420)']
['(16,421)' '(16,421)']
['(14,420)' '(14,420)']
['(11,419)' '(11,419)']
['(9,417)' '(9,417)']
['(7,416)' '(7,416)']
['(4,414)' '(4,414)']
['(3,414)' '(3,414)']
['(2,412)' '(2,412)']
['(1,412)' '(1,412)']
['(-1,410)' '(-1,410)']
['(-2,409)' '(-2,409)']
['(-2,408)' '(-2,408)']
['(-3,407)' '(-3,407)']
['(-3,406)' '(-3,406)']
['(-3,405)' '(-3,405)']
['(-3,404)' '(-3,404)']
['(-3,403)' '(-3,403)']
['(-3,402)' '(-3,402)']
['(-3,401)' '(-3,401)']
['(-3,400)' '(-3,400)']
['(-4,399)' '(-4,399)']
['(-4,398)' '(-4,398)']
['(-5,398)' '(-5,398)']
['(-6,398)' '(-6,398)']
['(-7,397)' '(-7,397)']
['(-7,396)' '(-7,396)']
['(-6,395)' '(-6,395)']
['(-5,395)' '(-5,395)']
['(-4,393)' '(-4,393)']
['(-3,391)' '(-3,391)']
['(6,384)' '(6,384)']
['(12,378)' '(12,378)']
['(24,370)' '(24,370)']
['(42,358)' '(42,358)']
['(107,304)' '(107,304)']
['(151,255)' '(151,255)']
['(207,196)' '(207,196)']
['(259,121)' '(259,121)']
['(389,-28)' '(389,-28)']
['(456,-84)' '(456,-84)']
['(515,-134)' '(515,-134)']
['(569,-182)' '(569,-182)']
['(650,-260)' '(650,-260)']
['(688,-294)' '(688,-294)']
['(723,-317)' '(723,-317)']
['(740,-328)' '(740,-328)']
['(762,-342)' '(762,-342)']
['(767,-347)' '(767,-347)']
['(768,-349)' '(768,-349)']
['(769,-352)' '(769,-352)']
['(769,-357)' '(769,-357)']
['(769,-359)' '(769,-359)']
['(768,-361)' '(768,-361)']
['(768,-364)' '(768,-364)']
['(766,-370)' '(766,-370)']
['(765,-371)' '(765,-371)']
['(764,-374)' '(764,-374)']
['(763,-376)' '(763,-376)']
['(761,-378)' '(761,-378)']
['(760,-381)' '(760,-381)']
['(758,-385)' '(758,-385)']
['(752,-394)' '(752,-394)']
['(747,-401)' '(747,-401)']
['(742,-407)' '(742,-407)']
['(735,-413)' '(735,-413)']
['(724,-421)' '(724,-421)']
['(719,-424)' '(719,-424)']
['(718,-425)' '(718,-425)']
['(717,-425)' '(717,-425)']]
but what I want is the columns like how
they are designed but the rows to be the same
as the columns and that we are able to update
and put values into the matrix.
Maybe what you want is a dict:
matrix = {
k: {l: 0 for l in res}
for k in res
}
All the values are initialized to 0.
You can easily update values in matrix; for example, you can increase the value of a 'cell' of one:
matrix['(18,430)']['(19,430)'] += 1
or set it to a specific value:
matrix['(18,430)']['(19,430)'] = 10
and retrieve it:
val = matrix['(18,430)']['(19,430)']
you can use Numpy.
for converting a list to a matrix like array you should write it as list of lists (or tuples). First of all your list contain strings so we first convert strings to tuples as follow:
new_list = [eval(i) for i in res]
I used eval because your strings is in tuple form so we can tell python treat them as a chunk of code.
then lets convert this new_list to array as follow:
import numpy as np
matrix = np.array(new_list )
now you can access your matrix elements as matrix[i, j] where i, j are row and column respectively. for changing a specific value of in certain location just assign it as usual:
matrix[i, j] = new_value
i have the following list:
erra_eus_repo = [(u'RHSA-2017:2796', u'6.7'), (u'RHSA-2017:2796', u'6.8'), (u'RHSA-2017:2794', u'7.2'), (u'RHSA-2017:2793', u'7.3')]
what I am trying to take the floating point numbers from each tuple:
6.7, 6.8 ,7.2, 7.3
and get the max number for each version that before the dot .ie :
new_list = [ 6.8, 7.3 ]
Note that max() will not work here, since if I have 5.9 and 5.11, I will get the max as 5.9, I want the result to be 5.11 since 11 > 9.
What I have tried:
eus_major = []
eus_minor = []
for major in erra_eus_repo:
minor = (major[1][2])
major = (major[1][0])
if major not in eus_major:
eus_major.append(major)
if minor not in eus_minor:
eus_minor.append(minor)
print(eus_major, eus_minor)
currently i am getting:
[u'6', u'7'] [u'7', u'2', u'3']
You can achieve this for instance with a combination of groupby and sorting:
from itertools import groupby
srt_list = sorted(erra_eus_repo, key=lambda x: x[1]);
max_list = []
for key, group in groupby(srt_list, lambda x: x[1].split('.')[0]):
max_el = max(list(group), key = lambda y: int(y[1].split('.')[1]))
max_list.append(float(max_el[1]))
First the array is sorted based on second element of each tuple to get sequences of elements with matching non-decimal number for grouping with groupby. groupby groups the elements into just that - each group will represent a sequence X.Z with common X. In each of these sequences - groups the program finds the one with maximum decimal part treated as a stand-along number. The whole number is then appended to the list with max values as a float.
Do not treat the version numbers as floating point, treat as '.' separated strings. Then split each version string (split on '.') and compare. Like this:
def normalize_version(v):
return tuple(map(int, v.split('.')))
Then you can see:
>>> u'5.11' > u'5.9'
False
>>> normalize_version(u'5.11') > normalize_version(u'5.9')
True
Here is another take on the problem which provides the highest value for each RHSA value (which is want I think you're after):
erra_eus_repo = [(u'RHSA-2017:2796', u'6.7'), (u'RHSA-2017:2796', u'6.8'), (u'RHSA-2017:2794', u'7.2'), (u'RHSA-2017:2793', u'7.3')]
eus_major = {}
for r in erra_eus_repo:
if r[0] not in eus_major.keys():
eus_major[r[0]] = 0
if float(r[1]) > float(eus_major[r[0]]):
eus_major[r[0]] = r[1]
print(eus_major)
output:
{'RHSA-2017:2796': '6.8', 'RHSA-2017:2794': '7.2', 'RHSA-2017:2793': '7.3'}
I left the value as a string, but it could easily be cast as a float.
The following simply uses the built-in min and max functions:
erra_eus_repo = [(u'RHSA-2017:2796', u'6.7'),
(u'RHSA-2017:2796', u'6.8'),
(u'RHSA-2017:2794', u'7.2'),
(u'RHSA-2017:2793', u'7.3')]
eus_major = max(float(major[1])for major in erra_eus_repo)
eus_minor = min(float(major[1])for major in erra_eus_repo)
newlist = [eus_minor, eus_major]
print(newlist) # -> [6.7, 7.3]
This may look like you're trying to compare decimal values, but you really aren't. It goes without saying (but I will) that while 9<11, .9>.11. So the idea of splitting the number into two separate values is really the only way to get a valid comparison.
The list is a list of lists - you have the master and each has a sub-list of RHSA and a value. Apparently you want discard the first item in the list and only get the (I assume) version of that item. Here's some code that, while crude, will give you an idea of what to do. (I'd welcome comments on how to clean that up...) So I've taken the lists, split them, then split the versions into major and minor, then compared them and if nothing exists in the list, add the value. I also added for sake of testing a 6.11 version number.
lstVersion = []
lstMaxVersion=[]
erra_eus_repo = [(u'RHSA-2017:2796', u'6.7'), (u'RHSA-2017:2796', u'6.8'), (u'RHSA-2017:2796', u'6.11'), (u'RHSA-2017:2794', u'7.2'), (u'RHSA-2017:2793', u'7.3')]
for strItem in erra_eus_repo:
lstVersion.append(strItem[1])
for strVersion in lstVersion:
blnAdded = False
intMajor = int(strVersion.split('.')[0])
intMinor = int(strVersion.split('.')[1])
print 'intMajor: ', intMajor
print 'intMinor:' , intMinor
for strMaxItem in lstMaxVersion:
intMaxMajor = int(strMaxItem.split('.')[0])
intMaxMinor = int(strMaxItem.split('.')[1])
print 'strMaxitem: ', strMaxItem
print 'intMaxMajor: ', intMaxMajor
print 'intMaxMinor: ', intMaxMinor
if intMajor == intMaxMajor:
blnAdded = True
if intMinor > intMaxMinor:
lstMaxVersion.remove(strMaxItem)
lstMaxVersion.append(str(intMajor)+'.'+str(intMinor))
if not blnAdded:
lstMaxVersion.append(str(intMajor)+'.'+str(intMinor))