Why do values in nested list not update? - python

I have a python function (resultsUpdate), which updates a nested list named resultsTable, containing values taken from a CSV file by changing the values contained in three of its columns with the values contained in three other lists, which the function takes as input.
def resultsUpdate(self, resultsTable, listPrefInd_a, listPrefInd_b, listPerfInd):
count = 0
for row in resultsTable:
while(count<len(resultsTable)-1):
print(count)
row[12] = listPrefInd_a[count]
row[13] = listPrefInd_b[count]
row[14] = listPerfInd[count]
print(row[12]) #debugging print
count+=1
print(resultsTable[count])
When I check if the values have updated with the print statement, they seem to have updated), however when I print the whole resultsTable the three columns are not updated. Any ideas why?
Example output
0
0.4230769230769231
['1', '19.01', '0', '0', 'AM', '15', '7', '4', 'BA', '11', '6', '13', '0', '0', '0']
1
-0.3793103448275862
['2', '19.01', '0', '0', 'AM', '6', '6', '17', 'BA', '4', '11', '15', '0', '0', '0']
2
-0.03125
['3', '19.01', '0', '0', 'BA', '9', '13', '10', 'AM', '8', '14', '11', '0', '0', '0']
3
-0.0625
['4', '19.01', '0', '0', 'AM', '10', '10', '12', 'BA', '7', '9', '14', '0', '0', '0']
4
-0.0625
['5', '19.01', '0', '0', 'BA', '10', '10', '12', 'AM', '10', '7', '13', '0', '0', '0']
5
-0.72
['6', '19.01', '0', '0', 'AM', '1', '5', '19', 'BA', '7', '9', '13', '0', '0', '0']
resultsTable:
[['nummer', 'datum', 'larvae', 'zeit', 'gruppe1', 'cs_a', 'med_a', 'neut_a', 'gruppe2', 'cs_b', 'med_b', 'neut_b', 'prefInd_a', 'prefInd_b', 'perfInd'], ['1', '19.01', '0', '0', 'AM', '15', '7', '4', 'BA', '11', '6', '13', '0', '0', '0'], ['2', '19.01', '0', '0', 'AM', '6', '6', '17', 'BA', '4', '11', '15', '0', '0', '0'], ['3', '19.01', '0', '0', 'BA', '9', '13', '10', 'AM', '8', '14', '11', '0', '0', '0'], ['4', '19.01', '0', '0', 'AM', '10', '10', '12', 'BA', '7', '9', '14', '0', '0', '0'], ['5', '19.01', '0', '0', 'BA', '10', '10', '12', 'AM', '10', '7', '13', '0', '0', '0'], ['6', '19.01', '0', '0', 'AM', '1', '5', '19', 'BA', '7', '9', '13', '0', '0', '0'], ['7', '23.01', '0', '0', 'BA', '2', '1', '25', 'AM', '14', '3', '11', '0', '0', '0'], ['8', '23.01', '0', '0', 'AM', '6', '10', '8', 'BA', '2', '7', '18', '0', '0', '0'], ['9', '24.01', '17.01', '0', 'BA', '8', '9', '7', 'AM', '6', '8', '10', '0', '0', '0'], ['10', '24.01', '17.01', '0', 'AM', '5', '8', '16', 'BA', '10', '5', '16', '0', '0', '0'], ['11', '24.01', '17.01', '0', 'BA', '9', '11', '6', 'AM', '7', '12', '16', '0', '0', '0'], ['12', '24.01', '17.01', '0', 'AM', '14', '7', '12', 'BA', '8', '5', '15', '0', '0', '0'], ['13', '25.01', '17.01', '13.23', 'BA', '4', '9', '17', 'AM', '2', '3', '25', '0', '0', '0'], ['14', '25.01', '17.01', '13.46', 'AM', '8', '11', '15', 'BA', '6', '5', '19', '0', '0', '0'], ['15', '25.01', '18.01', '14.49', 'BA', '6', '9', '12', 'AM', '6', '5', '17', '0', '0', '0'], ['16', '25.01', '18.01', '15.24', 'AM', '2', '7', '21', 'BA', '3', '7', '20', '0', '0', '0'], ['17', '25.01', '18.01', '16.14', 'AM', '18', '3', '8', 'BA', '8', '9', '12', '0', '0', '0'], ['18', '26.01', '18.01', '09:36', 'AM', '1', '17', '9', 'BA', '0', '0', '0', '0', '0', '0'], ['19', '26.01', '18.01', '13.4', 'BA', '3', '9', '14', 'AM', '10', '8', '11', '0', '0', '0'], ['20', '26.01', '19.01', '14.3', 'BA', '1', '14', '11', 'AM', '7', '10', '15', '0', '0', '0'], ['21', '26.01', '19.01', '0', 'AM', '9', '12', '14', 'BA', '4', '10', '12', '0', '0', '0'], ['22', '26.01', '19.01', '0', 'BA', '4', '7', '16', 'AM', '6', '7', '11', '0', '0', '0'], ['23', '26.01', '19.01', '0', 'AM', '7', '3', '14', 'BA', '5', '11', '13', '0', '0', '0'], ['24', '30.01', '22.01', '13.01', 'BA', '6', '7', '15', 'AM', '7', '9', '10', '0', '0', '0'], ['25', '30.01', '22.01', '13.01', 'AM', '18', '7', '7', 'BA', '5', '14', '10', '0', '0', '0'], ['26', '30.01', '22.01', '13.39', 'AM', '3', '9', '22', 'BA', '3', '3', '22', '0', '0', '0'], ['27', '30.01', '22.01', '13.39', 'BA', '5', '11', '16', 'AM', '5', '17', '7', '0', '0', '0'], ['28', '31.01', '23.01', '15.46', 'BA', '7', '6', '11', 'AM', '7', '10', '5', '0', '0', '0'], ['29', '31.01', '23.01', '15.46', 'BA', '10', '7', '9', 'AM', '7', '10', '5', '0', '0', '0'], ['30', '31.01', '23.01', '0', 'AM', '11', '6', '9', 'BA', '2', '7', '18', '0', '0', '0'], ['31', '31.01', '23.01', '0', 'BA', '7', '11', '13', 'AM', '10', '3', '11', '0', '0', '0'], ['32', '2.01', '24.01', '13.08', 'BA', '7', '11', '10', 'AM', '8', '11', '10', '0', '0', '0'], ['33', '2.01', '24.01', '13.08', 'AM', '10', '6', '9', 'BA', '9', '13', '12', '0', '0', '0'], ['34', '2.01', '25.01', '14.29', 'AM', '6', '7', '17', 'BA', '6', '10', '16', '0', '0', '0'], ['35', '2.01', '25.01', '14.29', 'BA', '3', '10', '16', 'AM', '2', '6', '18', '0', '0', '0'], ['36', '2.01', '25.01', '15.2', 'BA', '9', '11', '10', 'AM', '7', '7', '15', '0', '0', '0'], ['37', '2.01', '25.01', '15.2', 'AM', '10', '7', '14', 'BA', '6', '7', '18', '0', '0', '0'], ['38', '2.01', '25.01', '16.26', 'AM', '7', '13', '8', 'BA', '8', '6', '12', '0', '0', '0'], ['39', '2.01', '25.01', '16.26', 'BA', '4', '12', '14', 'AM', '9', '12', '6', '0', '0', '0'], ['40', '2.01', '25.01', '17.1', 'BA', '5', '8', '15', 'AM', '13', '9', '10', '0', '0', '0'], ['41', '2.01', '25.01', '17.1', 'AM', '9', '12', '9', 'BA', '17', '5', '10', '0', '0', '0']]
function returning resultsTable:
def getResults(self, csvInput):
with open (csvInput, 'rt') as csvfile:
reader = csv.reader(csvfile)
resultsTable = []
for row in reader:
resultsTable.append(row)
return resultsTable
listPrefInd_a:
[0.4230769230769231, -0.3793103448275862, -0.03125, -0.0625, -0.0625, -0.72, -0.8214285714285714, -0.08333333333333333, 0.041666666666666664, -0.3793103448275862, 0.11538461538461539, 0.06060606060606061, -0.43333333333333335, -0.20588235294117646, -0.2222222222222222, -0.6333333333333333, 0.3448275862068966, 0, -0.4230769230769231, -0.38461538461538464, -0.14285714285714285, -0.4444444444444444, -0.2916666666666667, -0.32142857142857145, 0.34375, -0.5588235294117647, -0.34375, -0.16666666666666666, 0.038461538461538464, 0.07692307692307693, -0.1935483870967742, -0.10714285714285714, 0.04, -0.36666666666666664, -0.4482758620689655, -0.03333333333333333, -0.12903225806451613, -0.03571428571428571, -0.3333333333333333, -0.35714285714285715, 0.0]
listPrefInd_b:
[-0.06666666666666667, -0.36666666666666664, -0.09090909090909091, -0.23333333333333334, -0.1, -0.20689655172413793, 0.10714285714285714, -0.5925925925925926, -0.16666666666666666, -0.1935483870967742, -0.2571428571428571, -0.25, -0.7666666666666667, -0.43333333333333335, -0.39285714285714285, -0.5666666666666667, -0.13793103448275862, 0, -0.034482758620689655, -0.25, -0.3076923076923077, -0.20833333333333334, -0.27586206896551724, -0.11538461538461539, -0.1724137931034483, -0.6785714285714286, -0.06896551724137931, 0.09090909090909091, 0.09090909090909091, -0.5925925925925926, -0.041666666666666664, -0.06896551724137931, -0.08823529411764706, -0.3125, -0.6153846153846154, -0.27586206896551724, -0.3870967741935484, -0.15384615384615385, 0.1111111111111111, 0.09375, 0.21875]
listPerfInd:
[0.1782051282051282, -0.3729885057471264, -0.061079545454545456, -0.14791666666666667, -0.08125, -0.46344827586206894, -0.35714285714285715, -0.33796296296296297, -0.0625, -0.28642936596218016, -0.07087912087912086, -0.0946969696969697, -0.6000000000000001, -0.3196078431372549, -0.30753968253968256, -0.6, 0.10344827586206898, 0, -0.22877984084880637, -0.3173076923076923, -0.22527472527472528, -0.3263888888888889, -0.28376436781609193, -0.2184065934065934, 0.08566810344827586, -0.6186974789915967, -0.20635775862068967, -0.03787878787878787, 0.06468531468531469, -0.25783475783475784, -0.11760752688172042, -0.08805418719211822, -0.024117647058823532, -0.33958333333333335, -0.5318302387267905, -0.15459770114942528, -0.25806451612903225, -0.09478021978021978, -0.1111111111111111, -0.13169642857142858, 0.109375]
The columns formed by entries at row[12], row[13], row[14] of resultsTable originally contain only zeroes.

The code logic is a bit off. It modified the header row, then the line after the header row was printed, then it modified the header row again due to the while, but printed the next unmodified line, etc.; so it only looked like the table wasn't modified. Once count was high enough the while wouldn't be entered anymore, but row would start advancing. The end result was only the header row was modified over and over again. Change print(row[12]) to print(row) and you'll see what I mean.
Here's a corrected version with comments:
count = 0
for row in resultsTable[1:]: # skip header row via slicing.
# while not needed, and prevented row from advancing
print(count)
row[12] = listPrefInd_a[count]
row[13] = listPrefInd_b[count]
row[14] = listPerfInd[count]
print(row[12]) #debugging print
count+=1 #advance to next row
print(resultsTable[count]) #this gives the correct offset skipping header.
The above is confusing. Here's an advanced version and demo. zip() returns an iterator that returns the first elements from each of its iterable parameters, then the 2nd elements, etc.; then row[12:] (element 12 to the end) is replaced with the three values in replacements:
def resultsUpdate(resultsTable, listPrefInd_a, listPrefInd_b, listPerfInd):
for row,replacements in zip(resultsTable[1:],zip(listPrefInd_a,listPrefInd_b,listPerfInd)):
row[12:] = replacements
resultsTable = [['nummer', 'datum', 'larvae', 'zeit', 'gruppe1', 'cs_a', 'med_a', 'neut_a', 'gruppe2', 'cs_b', 'med_b', 'neut_b', 'prefInd_a', 'prefInd_b', 'perfInd'], ['1', '19.01', '0', '0', 'AM', '15', '7', '4', 'BA', '11', '6', '13', '0', '0', '0'], ['2', '19.01', '0', '0', 'AM', '6', '6', '17', 'BA', '4', '11', '15', '0', '0', '0'], ['3', '19.01', '0', '0', 'BA', '9', '13', '10', 'AM', '8', '14', '11', '0', '0', '0'], ['4', '19.01', '0', '0', 'AM', '10', '10', '12', 'BA', '7', '9', '14', '0', '0', '0'], ['5', '19.01', '0', '0', 'BA', '10', '10', '12', 'AM', '10', '7', '13', '0', '0', '0'], ['6', '19.01', '0', '0', 'AM', '1', '5', '19', 'BA', '7', '9', '13', '0', '0', '0'], ['7', '23.01', '0', '0', 'BA', '2', '1', '25', 'AM', '14', '3', '11', '0', '0', '0'], ['8', '23.01', '0', '0', 'AM', '6', '10', '8', 'BA', '2', '7', '18', '0', '0', '0'], ['9', '24.01', '17.01', '0', 'BA', '8', '9', '7', 'AM', '6', '8', '10', '0', '0', '0'], ['10', '24.01', '17.01', '0', 'AM', '5', '8', '16', 'BA', '10', '5', '16', '0', '0', '0'], ['11', '24.01', '17.01', '0', 'BA', '9', '11', '6', 'AM', '7', '12', '16', '0', '0', '0'], ['12', '24.01', '17.01', '0', 'AM', '14', '7', '12', 'BA', '8', '5', '15', '0', '0', '0'], ['13', '25.01', '17.01', '13.23', 'BA', '4', '9', '17', 'AM', '2', '3', '25', '0', '0', '0'], ['14', '25.01', '17.01', '13.46', 'AM', '8', '11', '15', 'BA', '6', '5', '19', '0', '0', '0'], ['15', '25.01', '18.01', '14.49', 'BA', '6', '9', '12', 'AM', '6', '5', '17', '0', '0', '0'], ['16', '25.01', '18.01', '15.24', 'AM', '2', '7', '21', 'BA', '3', '7', '20', '0', '0', '0'], ['17', '25.01', '18.01', '16.14', 'AM', '18', '3', '8', 'BA', '8', '9', '12', '0', '0', '0'], ['18', '26.01', '18.01', '09:36', 'AM', '1', '17', '9', 'BA', '0', '0', '0', '0', '0', '0'], ['19', '26.01', '18.01', '13.4', 'BA', '3', '9', '14', 'AM', '10', '8', '11', '0', '0', '0'], ['20', '26.01', '19.01', '14.3', 'BA', '1', '14', '11', 'AM', '7', '10', '15', '0', '0', '0'], ['21', '26.01', '19.01', '0', 'AM', '9', '12', '14', 'BA', '4', '10', '12', '0', '0', '0'], ['22', '26.01', '19.01', '0', 'BA', '4', '7', '16', 'AM', '6', '7', '11', '0', '0', '0'], ['23', '26.01', '19.01', '0', 'AM', '7', '3', '14', 'BA', '5', '11', '13', '0', '0', '0'], ['24', '30.01', '22.01', '13.01', 'BA', '6', '7', '15', 'AM', '7', '9', '10', '0', '0', '0'], ['25', '30.01', '22.01', '13.01', 'AM', '18', '7', '7', 'BA', '5', '14', '10', '0', '0', '0'], ['26', '30.01', '22.01', '13.39', 'AM', '3', '9', '22', 'BA', '3', '3', '22', '0', '0', '0'], ['27', '30.01', '22.01', '13.39', 'BA', '5', '11', '16', 'AM', '5', '17', '7', '0', '0', '0'], ['28', '31.01', '23.01', '15.46', 'BA', '7', '6', '11', 'AM', '7', '10', '5', '0', '0', '0'], ['29', '31.01', '23.01', '15.46', 'BA', '10', '7', '9', 'AM', '7', '10', '5', '0', '0', '0'], ['30', '31.01', '23.01', '0', 'AM', '11', '6', '9', 'BA', '2', '7', '18', '0', '0', '0'], ['31', '31.01', '23.01', '0', 'BA', '7', '11', '13', 'AM', '10', '3', '11', '0', '0', '0'], ['32', '2.01', '24.01', '13.08', 'BA', '7', '11', '10', 'AM', '8', '11', '10', '0', '0', '0'], ['33', '2.01', '24.01', '13.08', 'AM', '10', '6', '9', 'BA', '9', '13', '12', '0', '0', '0'], ['34', '2.01', '25.01', '14.29', 'AM', '6', '7', '17', 'BA', '6', '10', '16', '0', '0', '0'], ['35', '2.01', '25.01', '14.29', 'BA', '3', '10', '16', 'AM', '2', '6', '18', '0', '0', '0'], ['36', '2.01', '25.01', '15.2', 'BA', '9', '11', '10', 'AM', '7', '7', '15', '0', '0', '0'], ['37', '2.01', '25.01', '15.2', 'AM', '10', '7', '14', 'BA', '6', '7', '18', '0', '0', '0'], ['38', '2.01', '25.01', '16.26', 'AM', '7', '13', '8', 'BA', '8', '6', '12', '0', '0', '0'], ['39', '2.01', '25.01', '16.26', 'BA', '4', '12', '14', 'AM', '9', '12', '6', '0', '0', '0'], ['40', '2.01', '25.01', '17.1', 'BA', '5', '8', '15', 'AM', '13', '9', '10', '0', '0', '0'], ['41', '2.01', '25.01', '17.1', 'AM', '9', '12', '9', 'BA', '17', '5', '10', '0', '0', '0']]
listPrefInd_a = [0.4230769230769231, -0.3793103448275862, -0.03125, -0.0625, -0.0625, -0.72, -0.8214285714285714, -0.08333333333333333, 0.041666666666666664, -0.3793103448275862, 0.11538461538461539, 0.06060606060606061, -0.43333333333333335, -0.20588235294117646, -0.2222222222222222, -0.6333333333333333, 0.3448275862068966, 0, -0.4230769230769231, -0.38461538461538464, -0.14285714285714285, -0.4444444444444444, -0.2916666666666667, -0.32142857142857145, 0.34375, -0.5588235294117647, -0.34375, -0.16666666666666666, 0.038461538461538464, 0.07692307692307693, -0.1935483870967742, -0.10714285714285714, 0.04, -0.36666666666666664, -0.4482758620689655, -0.03333333333333333, -0.12903225806451613, -0.03571428571428571, -0.3333333333333333, -0.35714285714285715, 0.0]
listPrefInd_b = [-0.06666666666666667, -0.36666666666666664, -0.09090909090909091, -0.23333333333333334, -0.1, -0.20689655172413793, 0.10714285714285714, -0.5925925925925926, -0.16666666666666666, -0.1935483870967742, -0.2571428571428571, -0.25, -0.7666666666666667, -0.43333333333333335, -0.39285714285714285, -0.5666666666666667, -0.13793103448275862, 0, -0.034482758620689655, -0.25, -0.3076923076923077, -0.20833333333333334, -0.27586206896551724, -0.11538461538461539, -0.1724137931034483, -0.6785714285714286, -0.06896551724137931, 0.09090909090909091, 0.09090909090909091, -0.5925925925925926, -0.041666666666666664, -0.06896551724137931, -0.08823529411764706, -0.3125, -0.6153846153846154, -0.27586206896551724, -0.3870967741935484, -0.15384615384615385, 0.1111111111111111, 0.09375, 0.21875]
listPerfInd = [0.1782051282051282, -0.3729885057471264, -0.061079545454545456, -0.14791666666666667, -0.08125, -0.46344827586206894, -0.35714285714285715, -0.33796296296296297, -0.0625, -0.28642936596218016, -0.07087912087912086, -0.0946969696969697, -0.6000000000000001, -0.3196078431372549, -0.30753968253968256, -0.6, 0.10344827586206898, 0, -0.22877984084880637, -0.3173076923076923, -0.22527472527472528, -0.3263888888888889, -0.28376436781609193, -0.2184065934065934, 0.08566810344827586, -0.6186974789915967, -0.20635775862068967, -0.03787878787878787, 0.06468531468531469, -0.25783475783475784, -0.11760752688172042, -0.08805418719211822, -0.024117647058823532, -0.33958333333333335, -0.5318302387267905, -0.15459770114942528, -0.25806451612903225, -0.09478021978021978, -0.1111111111111111, -0.13169642857142858, 0.109375]
resultsUpdate(resultsTable, listPrefInd_a, listPrefInd_b, listPerfInd)
for row in resultsTable:
print(row)
Output:
['nummer', 'datum', 'larvae', 'zeit', 'gruppe1', 'cs_a', 'med_a', 'neut_a', 'gruppe2', 'cs_b', 'med_b', 'neut_b', 'prefInd_a', 'prefInd_b', 'perfInd']
['1', '19.01', '0', '0', 'AM', '15', '7', '4', 'BA', '11', '6', '13', 0.4230769230769231, -0.06666666666666667, 0.1782051282051282]
['2', '19.01', '0', '0', 'AM', '6', '6', '17', 'BA', '4', '11', '15', -0.3793103448275862, -0.36666666666666664, -0.3729885057471264]
['3', '19.01', '0', '0', 'BA', '9', '13', '10', 'AM', '8', '14', '11', -0.03125, -0.09090909090909091, -0.061079545454545456]
['4', '19.01', '0', '0', 'AM', '10', '10', '12', 'BA', '7', '9', '14', -0.0625, -0.23333333333333334, -0.14791666666666667]
['5', '19.01', '0', '0', 'BA', '10', '10', '12', 'AM', '10', '7', '13', -0.0625, -0.1, -0.08125]
['6', '19.01', '0', '0', 'AM', '1', '5', '19', 'BA', '7', '9', '13', -0.72, -0.20689655172413793, -0.46344827586206894]
['7', '23.01', '0', '0', 'BA', '2', '1', '25', 'AM', '14', '3', '11', -0.8214285714285714, 0.10714285714285714, -0.35714285714285715]
['8', '23.01', '0', '0', 'AM', '6', '10', '8', 'BA', '2', '7', '18', -0.08333333333333333, -0.5925925925925926, -0.33796296296296297]
['9', '24.01', '17.01', '0', 'BA', '8', '9', '7', 'AM', '6', '8', '10', 0.041666666666666664, -0.16666666666666666, -0.0625]
['10', '24.01', '17.01', '0', 'AM', '5', '8', '16', 'BA', '10', '5', '16', -0.3793103448275862, -0.1935483870967742, -0.28642936596218016]
['11', '24.01', '17.01', '0', 'BA', '9', '11', '6', 'AM', '7', '12', '16', 0.11538461538461539, -0.2571428571428571, -0.07087912087912086]
['12', '24.01', '17.01', '0', 'AM', '14', '7', '12', 'BA', '8', '5', '15', 0.06060606060606061, -0.25, -0.0946969696969697]
['13', '25.01', '17.01', '13.23', 'BA', '4', '9', '17', 'AM', '2', '3', '25', -0.43333333333333335, -0.7666666666666667, -0.6000000000000001]
['14', '25.01', '17.01', '13.46', 'AM', '8', '11', '15', 'BA', '6', '5', '19', -0.20588235294117646, -0.43333333333333335, -0.3196078431372549]
['15', '25.01', '18.01', '14.49', 'BA', '6', '9', '12', 'AM', '6', '5', '17', -0.2222222222222222, -0.39285714285714285, -0.30753968253968256]
['16', '25.01', '18.01', '15.24', 'AM', '2', '7', '21', 'BA', '3', '7', '20', -0.6333333333333333, -0.5666666666666667, -0.6]
['17', '25.01', '18.01', '16.14', 'AM', '18', '3', '8', 'BA', '8', '9', '12', 0.3448275862068966, -0.13793103448275862, 0.10344827586206898]
['18', '26.01', '18.01', '09:36', 'AM', '1', '17', '9', 'BA', '0', '0', '0', 0, 0, 0]
['19', '26.01', '18.01', '13.4', 'BA', '3', '9', '14', 'AM', '10', '8', '11', -0.4230769230769231, -0.034482758620689655, -0.22877984084880637]
['20', '26.01', '19.01', '14.3', 'BA', '1', '14', '11', 'AM', '7', '10', '15', -0.38461538461538464, -0.25, -0.3173076923076923]
['21', '26.01', '19.01', '0', 'AM', '9', '12', '14', 'BA', '4', '10', '12', -0.14285714285714285, -0.3076923076923077, -0.22527472527472528]
['22', '26.01', '19.01', '0', 'BA', '4', '7', '16', 'AM', '6', '7', '11', -0.4444444444444444, -0.20833333333333334, -0.3263888888888889]
['23', '26.01', '19.01', '0', 'AM', '7', '3', '14', 'BA', '5', '11', '13', -0.2916666666666667, -0.27586206896551724, -0.28376436781609193]
['24', '30.01', '22.01', '13.01', 'BA', '6', '7', '15', 'AM', '7', '9', '10', -0.32142857142857145, -0.11538461538461539, -0.2184065934065934]
['25', '30.01', '22.01', '13.01', 'AM', '18', '7', '7', 'BA', '5', '14', '10', 0.34375, -0.1724137931034483, 0.08566810344827586]
['26', '30.01', '22.01', '13.39', 'AM', '3', '9', '22', 'BA', '3', '3', '22', -0.5588235294117647, -0.6785714285714286, -0.6186974789915967]
['27', '30.01', '22.01', '13.39', 'BA', '5', '11', '16', 'AM', '5', '17', '7', -0.34375, -0.06896551724137931, -0.20635775862068967]
['28', '31.01', '23.01', '15.46', 'BA', '7', '6', '11', 'AM', '7', '10', '5', -0.16666666666666666, 0.09090909090909091, -0.03787878787878787]
['29', '31.01', '23.01', '15.46', 'BA', '10', '7', '9', 'AM', '7', '10', '5', 0.038461538461538464, 0.09090909090909091, 0.06468531468531469]
['30', '31.01', '23.01', '0', 'AM', '11', '6', '9', 'BA', '2', '7', '18', 0.07692307692307693, -0.5925925925925926, -0.25783475783475784]
['31', '31.01', '23.01', '0', 'BA', '7', '11', '13', 'AM', '10', '3', '11', -0.1935483870967742, -0.041666666666666664, -0.11760752688172042]
['32', '2.01', '24.01', '13.08', 'BA', '7', '11', '10', 'AM', '8', '11', '10', -0.10714285714285714, -0.06896551724137931, -0.08805418719211822]
['33', '2.01', '24.01', '13.08', 'AM', '10', '6', '9', 'BA', '9', '13', '12', 0.04, -0.08823529411764706, -0.024117647058823532]
['34', '2.01', '25.01', '14.29', 'AM', '6', '7', '17', 'BA', '6', '10', '16', -0.36666666666666664, -0.3125, -0.33958333333333335]
['35', '2.01', '25.01', '14.29', 'BA', '3', '10', '16', 'AM', '2', '6', '18', -0.4482758620689655, -0.6153846153846154, -0.5318302387267905]
['36', '2.01', '25.01', '15.2', 'BA', '9', '11', '10', 'AM', '7', '7', '15', -0.03333333333333333, -0.27586206896551724, -0.15459770114942528]
['37', '2.01', '25.01', '15.2', 'AM', '10', '7', '14', 'BA', '6', '7', '18', -0.12903225806451613, -0.3870967741935484, -0.25806451612903225]
['38', '2.01', '25.01', '16.26', 'AM', '7', '13', '8', 'BA', '8', '6', '12', -0.03571428571428571, -0.15384615384615385, -0.09478021978021978]
['39', '2.01', '25.01', '16.26', 'BA', '4', '12', '14', 'AM', '9', '12', '6', -0.3333333333333333, 0.1111111111111111, -0.1111111111111111]
['40', '2.01', '25.01', '17.1', 'BA', '5', '8', '15', 'AM', '13', '9', '10', -0.35714285714285715, 0.09375, -0.13169642857142858]
['41', '2.01', '25.01', '17.1', 'AM', '9', '12', '9', 'BA', '17', '5', '10', 0.0, 0.21875, 0.109375]

you are using csv.reader which reads from csv files and gives you the data in the form of variables. You're updating the variables, but of course the variables doesn't update the file.
You need to use csv.writer to update the file.

Related

How can I remove the numbers in this list?

I need to remove the numbers (str) in a list I made for a project, and if is possible I want to save them in a variable. I'm a beginner at python, so I dont know how to do it. Can anyone help me?
names = ['MATEO', '3869', 'AGUSTIN', '2641', 'BENJAMIN', '2337', 'TOMAS', '2235', 'LUCAS', '2146', 'SANTIAGO', '2124', 'GASPAR', '1922', 'JOAQUIN', '1750', 'VICENTE', '1717', 'MATIAS', '1712', 'MAXIMILIANO', '1704', 'MARTIN', '1587', 'ALONSO', '1573', 'FACUNDO', '1439', 'LUCIANO', '1402', 'EMILIANO', '1296', 'JOSE', '1287', 'CRISTOBAL', '1267', 'MAXIMO', '1248', 'BRUNO', '1227', 'JULIAN', '1126', 'LIAM', '1088', 'DANTE', '1083', 'GABRIEL', '1082', 'DIEGO', '1052', 'NICOLAS', '1021', 'SANTINO', '950', 'LEON', '946', 'SIMON', '940', 'JUAN', '901', 'SEBASTIAN', '855', 'RENATO', '717', 'IAN', '701', 'THOMAS', '684', 'GAEL', '673', 'AMARO', '670', 'RAFAEL', '656', 'DANIEL', '652', 'PEDRO', '652', 'IGNACIO', '640', 'FELIPE', '638', 'SAMUEL', '578', 'ANGEL', '565', 'BASTIAN', '564', 'FRANCO', '560', 'VALENTIN', '546', 'THIAGO', '530', 'MARIANO', '525', 'LUIS', '502', 'CLEMENTE', '482', 'FRANCISCO', '479', 'DAVID', '464', 'MATTEO', '464', 'JAVIER', '462', 'ISAAC', '444', 'EMILIO', '435', 'PABLO', '434', 'DYLAN', '432', 'DAMIAN', '425', 'CARLOS', '415', 'ALEXANDER', '415', 'NOAH', '411', 'SALVADOR', '409', 'BALTAZAR', '401', 'FERNANDO', '401', 'MATHEO', '381', 'ELIAN', '372', 'AARON', '348', 'LEONARDO', '337', 'GUSTAVO', '335', 'ESTEBAN', '330', 'ELIAS', '328', 'BORJA', '322', 'MANUEL', '307', 'JORGE', '302', 'VALENTINO', '301', 'CRISTIAN', '299', 'ANDRES', '297', 'OLIVER', '296', 'MIGUEL', '277', 'VICTOR', '274', 'ALEJANDRO', '269', 'ETHAN', '238', 'RODRIGO', '238', 'EDUARDO', '231', 'LAUTARO', '231', 'CAMILO', '229', 'JOSUE', '228', 'MATHIAS', '227', 'ALAN', '217', 'FABIAN', '217', 'RAIMUNDO', '216', 'JESÚS', '205', 'EITHAN', '198', 'ALVARO', '194', 'LEANDRO', '183', 'IKER', '177', 'CHRISTOPHER', '174', 'EZEQUIEL', '173', 'ISAIAS', '173', 'MILAN', '173', 'LIAN', '172', 'LUKAS', '170', 'ISMAEL', '169', 'RICARDO', '168', 'CRISTOPHER', '167', 'HECTOR', '162', 'ABRAHAM', '162', 'GUILLERMO', '157', 'LORENZO', '153', 'PASCUAL', '151', 'JEAN', '150', 'AXEL', '148', 'EMMANUEL', '148', 'SERGIO', '148', 'DEMIAN', '147', 'ALFONSO', '142', 'ANTONIO', '140', 'ALEX', '139', 'OSCAR', '138', 'JEREMIAS', '132', 'GONZALO', '128', 'ANTHONY', '127', 'MOISÉS', '126', 'JONATHAN', '125', 'DOMINGO', '124', 'ARTURO', '122', 'NAHUEL', '121', 'MAURICIO', '120', 'MARIO', '119', 'ERICK', '119', 'THEO', '118', 'MARCELO', '116', 'AMARU', '114', 'AUGUSTO', '114', 'PIERO', '114', 'ADRIAN', '114', 'LYAN', '112', 'ABDIEL', '110', 'AUSTIN', '109', 'CÉSAR', '109', 'ROMAN', '109', 'IVAN', '108', 'ENZO', '107', 'KEVIN', '104', 'CLAUDIO', '99', 'EMANUEL', '99', 'MAX', '98', 'CHRISTIAN', '98', 'ALEXIS', '95', 'GASTÓN', '94', 'DEREK', '93', 'FEDERICO', '92', 'MARCO', '91', 'EYDAN', '89', 'ANIBAL', '87', 'PATRICIO', '86', 'RAÚL', '82', 'OCTAVIO', '78', 'JACOB', '78', 'FÉLIX', '78', 'DARÍO', '78', 'LYAM', '77', 'ROBERTO', '75', 'ARIEL', '74', 'ZAID', '72', 'WILLIAM', '72', 'EMIR', '71', 'ISRAEL', '71', 'STEFANO', '71', 'DILAN', '70', 'JAIME', '70', 'BAUTISTA', '70', 'WILLIAMS', '69', 'SANTI', '68', 'EVAN', '68', 'ADRIEL', '68', 'BALTASAR', '66', 'CRISTOFER', '66', 'JHON', '65', 'ELUNEY', '64', 'HUGO', '63', 'CALEB', '63', 'JOSEPH', '63', 'PAOLO', '63', 'KYLIAN', '63', 'SAMIR', '62', 'TAHIEL', '62', 'NEHEMIAS', '62', 'JEREMY', '61', 'JOSIAS', '60', 'LEONEL', '59', 'ANDER', '59', 'IÑAKI', '59', 'OMAR', '59', 'ANGELO', '57', 'ALESSANDRO', '57', 'JOEL', '57', 'BELTRÁN', '56', 'NATHAN', '56', 'JOHN', '56', 'JOSHUA', '56', 'MILOVAN', '55', 'EFRAÍN', '55', 'JORDAN', '55', 'EDWARD', '54', 'HANS', '54', 'GIOVANNI', '53', 'ANTU', '53', 'ARON', '53', 'JARED', '53', 'LOGAN', '52', 'JOHAN', '51', 'MARCOS', '51', 'TOBIAS', '51', 'LEONIDAS', '51', 'EVANS', '50', 'MISAEL', '50', 'JULIO', '49', 'PAULO', '48', 'RODOLFO', '48', 'EDGAR', '48', 'ADOLFO', '46', 'MAURO', '46', 'RUBEN', '46', 'ABEL', '45', 'IZAN', '45', 'ENRIQUE', '44', 'RICHARD', '44', 'ALBERTO', '44', 'NICANOR', '43', 'TEO', '43', 'DANILO', '43', 'ELIEL', '42', 'LUCA', '42', 'EXEQUIEL', '42', 'EIDAN', '42', 'SAID', '41', 'DORIAN', '41', 'LUAN', '41', 'MIKE', '40', 'DERECK', '40', 'INTI', '40', 'SAÚL', '40', 'ALONZO', '39', 'MICHAEL', '39', 'GERARDO', '38', 'LUCCA', '37', 'HERNÁN', '37', 'ANDERSON', '37', 'YAHIR', '37', 'FABIO', '36', 'AMIR', '36', 'ISAI', '35', 'JAYDEN', '35', 'EINAR', '35', 'NELSON', '35', 'ANTUAN', '35', 'GERMÁN', '34', 'LIONEL', '33', 'ANDRE', '33', 'AUKAN', '33', 'BRAYAN', '32', 'BRANDON', '32', 'PATRICK', '32', 'SILVESTRE', '31', 'TADEO', '31', 'ALEN', '31', 'STEVEN', '31', 'PAUL', '30', 'DOMINIC', '30', 'NATANAEL', '30', 'LUKA', '30', 'ADRIANO', '30', 'RAMIRO', '30', 'JAMES', '30', 'MATTHEW', '30', 'DEMIR', '30', 'MARVENS', '29', 'DARIEL', '29', 'ALFREDO', '29', 'GADIEL', '29', 'LUCCIANO', '29', 'ULISES', '28', 'JOAN', '28', 'ELIAM', '28', 'ADAM', '28', 'URIEL', '28', 'YAIR', '28', 'LUCCAS', '28', 'BRYAN', '28', 'CHRIS', '28', 'MARC', '27', 'BERNARDO', '27', 'ROBERT', '27', 'IBRAHIM', '27', 'TEODORO', '26', 'ANDY', '26', 'MASSIMO', '26', 'NEYTHAN', '26', 'OWEN', '26', 'ERIK', '25', 'ELISEO', '25', 'JHOAN', '25', 'CIRO', '25', 'VLADIMIR', '24', 'EYTHAN', '24', 'BAYRON', '24', 'FRANCESCO', '24', 'JOAO', '24', 'YOEL', '24', 'JAIRO', '24', 'ERIC', '23', 'KILIAN', '23', 'OSVALDO', '23', 'JAIR', '23', 'IHAN', '23', 'JUAQUIN', '23', 'FARID', '23', 'MAEL', '23', 'GIANLUCA', '22', 'IÑIGO', '22', 'FERRÁN', '22', 'ARTHUR', '22', 'NEITHAN', '22', 'JACK', '22', 'ANDREW', '22', 'ERNESTO', '22', 'GERALD', '22', 'WOOD', '22', 'ANTOINE', '21', 'FLAVIO', '21', 'EDISON', '21', 'RENÉ', '21', 'BRAULIO', '21', 'PRINCE', '21', 'BORIS', '21', 'NATHANIEL', '20', 'RAPHAEL', '20', 'JUSTIN', '20', 'TRISTÁN', '20', 'GIULIANO', '20', 'VINCENT', '20', 'LUCIO', '20', 'ADONIS', '20', 'LEO', '20', 'ITHAN', '19', 'APOLO', '19', 'VITTORIO', '19', 'NAIM', '19', 'KARIM', '19', 'ADÁN', '19', 'ALEXANDRO', '19', 'ANTHUAN', '18', 'DIDIER', '18', 'NAWEL', '18', 'AQUILES', '18', 'MAICOL', '18', 'FABRIZIO', '18', 'BALTHAZAR', '18', 'SANTY', '17', 'RYAN', '17', 'RAMÓN', '17', 'ELIEZER', '17', 'NÉSTOR', '17', 'FABRICIO', '17', 'DARIEN', '17', 'JONAS', '17', 'ADAMS', '17', 'JERÓNIMO', '17', 'SALOMÓN', '17', 'YAEL', '16', 'ALLAN', '16', 'ALDO', '16', 'KAI', '16', 'LUCIAN', '16', 'EDER', '16', 'IAM', '16', 'OSTIN', '16', 'BENICIO', '16', 'ZABDIEL', '16', 'ELLIOT', '15', 'XAVIER', '15', 'ELEAZAR', '15', 'ESTEFANO', '15', 'EDWIN', '15', 'GERÓNIMO', '15', 'MARCUS', '15', 'HENRY', '15', 'EDUARD', '15', 'EMERSON', '15', 'JUANPABLO', '14', 'GIOVANNY', '14', 'WLADIMIR', '14', 'LEVI', '14', 'WILSON', '14', 'GIAN', '14', 'AIDAN', '14', 'TYRONE', '14', 'JHAIR', '14', 'NEIZAN', '14', 'ITALO', '14', 'WALTER', '14', 'GIANFRANCO', '14', 'GAMALIEL', '14', 'DASTIN', '14', 'JADIEL', '14', 'ESAI', '14', 'ZAMIR', '13', 'JAEL', '13', 'RONALD', '13', 'ZAHID', '13', 'GREGORIO', '13', 'DONATO', '13', 'ROQUE', '13', 'ZAHIR', '13', 'DASTAN', '13', 'RAMSES', '13', 'ROBINSON', '12', 'ANYELO', '12', 'GIORGIO', '12', 'SAM', '12', 'JOSE-TOMAS', '12', 'OSMAN', '12', 'FIDEL', '12', 'BYRON', '12', 'ILIAN', '12', 'ORLANDO', '12', 'CRISTHIÁN', '12', 'MARLON', '12', 'MIRKO', '12', 'RALPH', '12', 'RAYAN', '12', 'STEFAN', '12', 'TYLER', '12', 'GREGORY', '12', 'JUNIOR', '12', 'JEISON', '12', 'VASCO', '12', 'ROOD', '12', 'NIKOLAS', '12', 'FRANCHESCO', '11', 'TIAGO', '11', 'EREN', '11', 'TOMMY', '11', 'BENITO', '11', 'FRANK', '11', 'DOMINICK', '11', 'ELIOT', '11', 'DEYLAN', '11', 'JEFFERSON', '11', 'ÉTIENNE', '11', 'ANTONY', '11', 'MAIKEL', '11', 'JHONNY', '11', 'NEWEN', '11', 'KEYLOR', '11', 'MAYKOL', '11', 'EITAN', '11', 'MAIKOL', '11', 'EIDER', '11', 'JULIANO', '11', 'KILLIAN', '11', 'ABDIAS', '11', 'CRESCENTE', '11', 'SALVATORE', '11', 'HASSAN', '11', 'RENZO', '11', 'VINCENZO', '10', 'BRUCE', '10', 'RAYMUNDO', '10', 'ALÍ', '10', 'YAMIR', '10', 'MILO', '10', 'ARLEY', '10', 'JOE', '10', 'NEFTALÍ', '10', 'ERWIN', '10', 'IANN', '10', 'STEPHANO', '10', 'MARCIAL', '10', 'SANDRO', '10', 'DAVE', '10', 'DAWENS', '10', 'LENIN', '10', 'BRIAN', '10', 'JADEN', '10', 'YEISON', '10', 'EZRA', '10', 'FILIPPO', '10', 'YULIANO', '10', 'YERIK', '10', 'FRANTZ', '10', 'IVAR', '10', 'YADIEL', '10', 'FRANKO', '10', 'WOODLEY', '10', 'JAHAZIEL', '10', 'ELYAN', '10', 'EDEN', '9', 'PASCAL', '9', 'TAYRON', '9', 'AIDEN', '9', 'NATANIEL', '9', 'JACOBO', '9', 'BAIRON', '9', 'DARWIN', '9', 'JIMMY', '9', 'KALETH', '9', 'ASHER', '9', 'BRADLEY', '9', 'ROGER', '9', 'GERARD', '9', 'JOB', '9', 'GUIDO', '9', 'JAZIEL', '9', 'DAWENSKY', '9', 'YEREMI', '9', 'SANTOS', '9', 'ROY', '9', 'CRISTIANO', '9', 'JHOEL', '9', 'DENZEL', '9', 'LION', '9', 'ARMANDO', '9', 'UZIEL', '9', 'KERVENS', '9', 'ILAN', '9', 'NAHUM', '9', 'MAGDIEL', '9', 'BRANCO', '9', 'SANTHIAGO', '9', 'BERNABÉ', '9', 'AWKÁN', '8', 'NEYMAR', '8', 'STEPHAN', '8', 'JAYSON', '8', 'ROLANDO', '8', 'FAUSTINO', '8', 'DÉNIS', '8', 'ALESSIO', '8', 'TAYLER', '8', 'JASON', '8', 'YORDAN', '8', 'NOAM', '8', 'EDINSON', '8', 'HORACIO', '8', 'NIKOLA', '8', 'PIERRE', '8', 'DANNY', '8', 'AYUN', '8', 'REINALDO', '8', 'BASTHIAN', '8', 'ARIAN', '8', 'ENOC', '8', 'YERAY', '8', 'MIQUEAS', '8', 'DEIVID', '8', 'THOMMAS', '8', 'MATTIAS', '8', 'VIGGO', '8', 'FAUSTO', '8', 'CHRIST', '8', 'RAGNAR', '8', 'FABRIZZIO', '8', 'KAEL', '8', 'GIANCARLO', '8', 'NATHANAEL', '8', 'FRANCIS', '8', 'ROMÉO', '8', 'MATTHEO', '7', 'EUGENIO', '7', 'TRAVIS', '7', 'MILLER', '7', 'GHAEL', '7', 'EDRIC', '7', 'SELIM', '7', 'LÁZARO', '7', 'NOEL', '7', 'ETHIÁN', '7', 'LOVENSKY', '7', 'PANGAL', '7', 'CARLO', '7', 'PHILIPPE', '7', 'MICHEL', '7', 'EMIL', '7', 'POLO', '7', 'ALLEN', '7', 'ABDEL', '7', 'CARLENS', '7', 'DENNIS', '7', 'LOUIS', '7', 'GIORDANO', '7', 'ANKATU', '7', 'CAETANO', '7', 'DANTTE', '7', 'REIMUNDO', '7', 'LUCKAS', '7', 'PELAYO', '7', 'KILIAM', '7', 'GIUSEPPE', '7', 'BRUNNO', '7', 'MATIA', '7', 'FABIANO', '7', 'TAYLOR', '7', 'LIFKO', '7', 'FRANKLIN', '7', 'AZIEL', '7', 'ALAIN', '7', 'JIA', '7', 'YULIAN', '7', 'ENMANUEL', '7', 'CARL', '7', 'GIANLUCAS', '7', 'DAYRON', '7', 'MAXIMILIAN', '7', 'GAHEL', '7', 'WENSLEY', '7', 'DARELL', '7', 'GENARO', '7', 'JUN', '6', 'EFRAÍM', '6', 'NIL', '6']
I was thinking in a def to use names.pop(x) with x as every number of index that is impair, cause the total of str is 1246, so the index quantity is 1247. But although I have tried many times, I haven't been able to get it right.
You can use a list comprehension:
[item for item in names if item.isnumeric()]
It also appears that the list contains a pattern where there's one name followed by one numeric value. If this is the case, you can use list slicing instead:
names[1::2]
These output (first three / last three elements):
['3869', '2641', '2337', ..., '6', '6', '6']
For instance, you can do something like this:
names = ['MATEO', '3869', 'AGUSTIN', '2641']
numbers = []
for string in names:
if string.isnumeric():
numbers.append(string)
>>> numbers
>>> ['3869', '2641']
Or you can use list comprehensions:
numbers = [string for string in names if string.isnumeric()]
You can use list comprehension and save its output to a variable, like this:
names = ['MATEO', '3869', 'AGUSTIN', '2641']
numbers = [item for item in names if item.isnumeric()]
>>> numbers
>>> ['3869', '2641']

How to bubble sort a 2D array with python

I have this list that's a summary of a few NHL player stats in 2018. I want to sort them by pts which is the 7th value using bubble. I am aware of the built-in sort function on python but I would rather use bubble sort or even quicksort for that matter. Can anyone help out?
[['A.J. Greer', 'COL', 'LW', '15', '1', '1', '2', '14', '9', '20', '5'], ['Aaron Ekblad', 'FLA', 'D', '82', '13', '24', '37', '47', '180', '114', '88'], ['Adam Clendening', 'CLS', 'D', '4', '0', '0', '0', '0', '3', '1', '3'], ['Adam Cracknell', 'FA', 'C', '2', '0', '0', '0', '0', '3', '6', '0'], ['Adam Erne', 'DET', 'LW', '65', '7', '13', '20', '40', '70', '159', '26'], ['Adam Gaudette', 'VAN', 'C', '56', '5', '7', '12', '18', '55', '48', '15']]
This is what I did so far:
def sortByPoints(stats):
lengthOfstats = len(stats) - 1
for i in range(lengthOfstats):
for j in range(lengthOfstats - i):
if stats[j] < stats[j + 1]:
stats[j], stats[j + 1] = stats[j + 1], stats[j]
return stats
print(sortByPoints(readStatsFromFile()))
Create bubble sort that can sort nested-arrays based upon an index of sub-array
Modification of BubbleSort
def bubbleSort(arr, ind = 6):
"""Bubble sort arr based upon subelement ind (default of index 6)
which is 7th element of sub-array since 0 based indexing"""
n = len(arr)
# Traverse through all array elements
for i in range(n):
# Last i elements are already in place
for j in range(0, n-i-1):
# traverse the array from 0 to n-i-1
# Swap if the element found is greater
# than the next element
if int(arr[j][ind]) > int(arr[j+1][ind]) :
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
Test
arr = [['A.J. Greer', 'COL', 'LW', '15', '1', '1', '2', '14', '9', '20', '5'], ['Aaron Ekblad', 'FLA', 'D', '82', '13', '24', '37', '47', '180', '114', '88'], ['Adam Clendening', 'CLS', 'D', '4', '0', '0', '0', '0', '3', '1', '3'], ['Adam Cracknell', 'FA', 'C', '2', '0', '0', '0', '0', '3', '6', '0'], ['Adam Erne', 'DET', 'LW', '65', '7', '13', '20', '40', '70', '159', '26'], ['Adam Gaudette', 'VAN', 'C', '56', '5', '7', '12', '18', '55', '48', '15']]
import pprint
print('pre-sorted')
pprint.pprint(arr)
print('sorted')
pprint.pprint(bubbleSort(arr))
Output
pre-sorted
[['A.J. Greer', 'COL', 'LW', '15', '1', '1', '2', '14', '9', '20', '5'],
['Aaron Ekblad', 'FLA', 'D', '82', '13', '24', '37', '47', '180', '114', '88'],
['Adam Clendening', 'CLS', 'D', '4', '0', '0', '0', '0', '3', '1', '3'],
['Adam Cracknell', 'FA', 'C', '2', '0', '0', '0', '0', '3', '6', '0'],
['Adam Erne', 'DET', 'LW', '65', '7', '13', '20', '40', '70', '159', '26'],
['Adam Gaudette', 'VAN', 'C', '56', '5', '7', '12', '18', '55', '48', '15']]
sorted
[['Adam Clendening', 'CLS', 'D', '4', '0', '0', '0', '0', '3', '1', '3'],
['Adam Cracknell', 'FA', 'C', '2', '0', '0', '0', '0', '3', '6', '0'],
['A.J. Greer', 'COL', 'LW', '15', '1', '1', '2', '14', '9', '20', '5'],
['Adam Gaudette', 'VAN', 'C', '56', '5', '7', '12', '18', '55', '48', '15'],
['Adam Erne', 'DET', 'LW', '65', '7', '13', '20', '40', '70', '159', '26'],
['Aaron Ekblad', 'FLA', 'D', '82', '13', '24', '37', '47', '180', '114', '88']]

Removing all non digits from a list

After reading from a file I have a list of lists contaning not only digits but also other characters, which I would like to get rid of.
I've tried using re.sub function but this doesn't seem to work
import re
Poly_id= [['0', '[4', '8', '18', '20', '5', '0', '4]'], ['1', '[13', '16',
'6', '11', '13]'], ['2', '[3', '1', '10', '9', '2', '15', '3]'], ['3',
'[13', '12', '16', '13]'], ['4', '[13', '11', '17', '14', '7', '13]']]
for x in Poly_id:
[re.sub(r'\W', '', ch) for ch in x]
This doesn't seem to change a thing in this list.
I would like to have a list with only numbers as elements so that I could convert them into integers
I guess technically [4 is non numeric so you can do something like this:
Poly_id = [[char for char in _list if str.isnumeric(char)] for _list in Poly_id]
Output:
['0', '8', '18', '20', '5', '0']
['1', '16', '6', '11']
['2', '1', '10', '9', '2', '15']
['3', '12', '16']
['4', '11', '17', '14', '7']
If you just want to remove the non numeric values and not the complete entry then you can do this:
Poly_id = [[''.join(char for char in substring if str.isnumeric(char)) for substring in _list] for _list in Poly_id]
Output:
['0', '4', '8', '18', '20', '5', '0', '4']
['1', '13', '16', '6', '11', '13']
['2', '3', '1', '10', '9', '2', '15', '3']
['3', '13', '12', '16', '13']
['4', '13', '11', '17', '14', '7', '13']
Here a solution if you want to get rid of the '[' in '[4' but keep the '4':
res = [[re.sub(r'\W', '', st) for st in inlist] for inlist in Poly_id]
res is:
[
['0', '4', '8', '18', '20', '5', '0', '4'],
['1', '13', '16', '6', '11', '13'],
['2', '3', '1', '10', '9', '2', '15', '3'],
['3', '13', '12', '16', '13'],
['4', '13', '11', '17', '14', '7', '13']
]
You can use a module, "itertools"
import itertools
list_of_lists = [[1, 2], [3, 4]]
print(list(itertools.chain(*list_of_lists)))
>>>[1, 2, 3, 4]

Strange error, TypeError: object of type 'method' has no len()

I'm trying to do some webscraping to download all the results of euromillions, stuck with errors now. I'm using jupyter and python 3 with the modules specified. With just one link the code worked just fine but now I added a loop and some modifications and rip xD
import bs4
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
years = list(range(2004,2018))
for year in years:
my_urls = ('https://www.euro-millions.com/pt/arquivo-de-resultados-' + str(year),)
my_url = my_urls[0]
for my_url in my_urls:
Client = uReq(my_url)
html = Client.read
Client.close()
euro = soup(html, "html")
containers = euro.findAll("div",{"class":"archives"})
print(containers)
container = containers[0]
for container in containers:
data = container.a["href"].replace('/pt/resultados/','') #Usamos os [] como num dicionario, .strip tbm retira o lixo sometimes
bolasN = container.ul.findAll("li",{"class":"ball"})
bolasS = container.ul.findAll("li",{"class":"lucky-star"})
bola1 = bolasN[0].text
bola2 = bolasN[1].text
bola3 = bolasN[2].text
bola4 = bolasN[3].text
bola5 = bolasN[4].text
star1 = bolasS[0].text
star2 = bolasS[1].text
TUDO = [data, bola1, bola2, bola3, bola4, bola5, star1, star2]
print(TUDO)
TRACEBACK:
TypeError Traceback (most recent call last)
<ipython-input-31-b11e2044b5ea> in <module>
12 html = Client.read
13 Client.close()
---> 14 euro = soup(html, "html")
15 containers = euro.findAll("div",{"class":"archives"})
16 print(containers)
/usr/local/lib/python3.5/dist-packages/bs4/__init__.py in __init__(self, markup, features, builder, parse_only, from_encoding, exclude_encodings, **kwargs)
244 if hasattr(markup, 'read'): # It's a file-type object.
245 markup = markup.read()
--> 246 elif len(markup) <= 256 and (
247 (isinstance(markup, bytes) and not b'<' in markup)
248 or (isinstance(markup, str) and not '<' in markup)
TypeError: object of type 'method' has no len()
The markup was wrong, it should be 'html.parser' or 'lxml'
import requests
from bs4 import BeautifulSoup as soup
years = list(range(2004,2018))
for year in years:
my_urls = ('https://www.euro-millions.com/pt/arquivo-de-resultados-' + str(year),)
my_url = my_urls[0]
for my_url in my_urls:
Client = requests.get(my_url)
euro = soup(Client.content, "html.parser")
containers = euro.findAll("div",{"class":"archives"})
#print(containers)
container = containers[0]
for container in containers:
data = container.a["href"].replace('/pt/resultados/','') #Usamos os [] como num dicionario, .strip tbm retira o lixo sometimes
bolasN = container.ul.findAll("li",{"class":"ball"})
bolasS = container.ul.findAll("li",{"class":"lucky-star"})
bola1 = bolasN[0].text
bola2 = bolasN[1].text
bola3 = bolasN[2].text
bola4 = bolasN[3].text
bola5 = bolasN[4].text
star1 = bolasS[0].text
star2 = bolasS[1].text
TUDO = [data, bola1, bola2, bola3, bola4, bola5, star1, star2]
print(TUDO)
Output:
['29-12-2017', '4', '8', '22', '23', '48', '1', '12']
['26-12-2017', '4', '17', '30', '43', '44', '2', '10']
['22-12-2017', '5', '24', '30', '31', '43', '3', '6']
['19-12-2017', '8', '15', '30', '38', '46', '4', '7']
['15-12-2017', '25', '30', '31', '42', '50', '2', '11']
['12-12-2017', '20', '37', '39', '44', '50', '4', '8']
['08-12-2017', '4', '22', '30', '32', '34', '3', '4']
['05-12-2017', '11', '36', '43', '44', '48', '2', '7']
['01-12-2017', '5', '24', '29', '35', '46', '11', '12']
['28-11-2017', '1', '6', '12', '18', '42', '2', '7']
['24-11-2017', '19', '24', '28', '30', '50', '3', '10']
['21-11-2017', '2', '10', '14', '28', '31', '5', '7']
['17-11-2017', '20', '26', '35', '36', '42', '5', '12']
['14-11-2017', '14', '16', '39', '40', '41', '8', '10']
['10-11-2017', '13', '22', '29', '36', '37', '1', '9']
['07-11-2017', '7', '19', '20', '37', '41', '2', '12']
['03-11-2017', '5', '12', '17', '33', '41', '4', '9']
['31-10-2017', '1', '12', '36', '43', '46', '3', '5']
['27-10-2017', '3', '16', '23', '32', '39', '1', '4']
['24-10-2017', '9', '11', '13', '27', '33', '7', '10']
['20-10-2017', '4', '17', '23', '27', '30', '3', '8']
['17-10-2017', '13', '17', '19', '26', '36', '2', '3']
['13-10-2017', '23', '29', '37', '45', '50', '5', '11']
['10-10-2017', '4', '21', '34', '36', '37', '3', '6']
['06-10-2017', '1', '9', '15', '19', '25', '1', '7']
['03-10-2017', '6', '24', '32', '48', '50', '1', '5']
['29-09-2017', '7', '18', '19', '32', '48', '3', '7']
['26-09-2017', '1', '29', '40', '41', '48', '6', '12']
['22-09-2017', '6', '11', '31', '39', '42', '1', '3']
['19-09-2017', '1', '8', '21', '30', '45', '2', '3']
['15-09-2017', '13', '18', '37', '44', '49', '9', '12']
['12-09-2017', '10', '17', '27', '29', '35', '4', '11']
['08-09-2017', '9', '24', '42', '47', '49', '1', '5']
['05-09-2017', '6', '9', '18', '28', '29', '1', '9']
['01-09-2017', '3', '7', '8', '14', '49', '5', '8']
['29-08-2017', '4', '12', '15', '32', '38', '1', '5']
['25-08-2017', '1', '5', '7', '15', '47', '9', '12']
['22-08-2017', '3', '10', '12', '17', '27', '3', '5']
['18-08-2017', '2', '24', '39', '42', '45', '2', '8']
['15-08-2017', '10', '14', '30', '35', '46', '4', '10']
['11-08-2017', '18', '28', '39', '46', '48', '5', '12']
['08-08-2017', '15', '25', '26', '40', '41', '4', '5']
['04-08-2017', '29', '30', '36', '40', '41', '2', '9']
['01-08-2017', '14', '21', '24', '29', '30', '8', '10']
['28-07-2017', '5', '9', '29', '31', '41', '2', '4']
['25-07-2017', '12', '14', '43', '44', '48', '2', '11']
['21-07-2017', '1', '8', '9', '26', '49', '5', '9']
['18-07-2017', '1', '25', '27', '41', '45', '5', '7']
['14-07-2017', '11', '14', '20', '21', '47', '7', '10']
['11-07-2017', '14', '22', '26', '42', '50', '8', '10']
['07-07-2017', '11', '20', '35', '37', '45', '3', '6']
['04-07-2017', '10', '22', '25', '37', '49', '5', '8']
['30-06-2017', '17', '35', '39', '47', '50', '6', '8']
['27-06-2017', '9', '17', '21', '28', '45', '1', '3']
['23-06-2017', '3', '4', '21', '31', '38', '3', '7']
['20-06-2017', '11', '18', '26', '43', '44', '8', '10']
['16-06-2017', '15', '17', '38', '41', '42', '9', '12']
['13-06-2017', '3', '12', '22', '27', '49', '4', '11']
['09-06-2017', '9', '20', '27', '39', '43', '10', '11']
['06-06-2017', '20', '22', '25', '37', '40', '3', '7']
['02-06-2017', '8', '10', '24', '33', '42', '3', '9']
['30-05-2017', '7', '12', '27', '38', '48', '6', '9']
['26-05-2017', '5', '7', '26', '36', '39', '2', '10']
['23-05-2017', '8', '15', '25', '27', '42', '1', '4']
['19-05-2017', '9', '11', '12', '19', '30', '4', '9']
['16-05-2017', '8', '11', '15', '20', '30', '3', '8']
['12-05-2017', '2', '20', '28', '29', '44', '3', '9']
['09-05-2017', '8', '12', '16', '22', '26', '6', '7']
['05-05-2017', '3', '7', '30', '35', '43', '1', '3']
['02-05-2017', '6', '19', '23', '25', '27', '11', '12']
['28-04-2017', '14', '20', '25', '30', '39', '2', '8']
['25-04-2017', '9', '11', '19', '32', '43', '3', '9']
['21-04-2017', '2', '13', '16', '22', '49', '4', '5']
['18-04-2017', '17', '22', '31', '38', '45', '5', '12']
['14-04-2017', '4', '14', '20', '23', '33', '6', '10']
['11-04-2017', '5', '21', '22', '31', '49', '2', '8']
['07-04-2017', '2', '10', '19', '35', '50', '6', '7']
['04-04-2017', '1', '9', '24', '33', '34', '2', '6']
['31-03-2017', '17', '24', '26', '28', '45', '4', '12']
['28-03-2017', '9', '13', '31', '33', '46', '6', '10']
['24-03-2017', '2', '17', '21', '27', '34', '5', '9']
['21-03-2017', '1', '20', '23', '44', '47', '4', '11']
['17-03-2017', '6', '10', '19', '29', '36', '3', '9']
['14-03-2017', '3', '5', '21', '36', '44', '3', '6']
['10-03-2017', '31', '36', '38', '47', '49', '8', '11']
['07-03-2017', '6', '37', '41', '48', '50', '4', '5']
['03-03-2017', '2', '11', '29', '30', '47', '1', '12']
['28-02-2017', '10', '20', '31', '35', '42', '2', '12']
['24-02-2017', '2', '4', '13', '22', '43', '8', '9']
['21-02-2017', '13', '19', '41', '45', '49', '3', '4']
['17-02-2017', '19', '25', '33', '36', '48', '2', '9']
['14-02-2017', '2', '10', '24', '40', '44', '3', '10']
['10-02-2017', '7', '21', '26', '35', '43', '2', '9']
['07-02-2017', '4', '10', '31', '38', '44', '8', '10']
['03-02-2017', '3', '4', '15', '46', '50', '5', '9']
['31-01-2017', '3', '4', '17', '23', '44', '6', '9']
['27-01-2017', '17', '20', '28', '45', '48', '5', '9']
['24-01-2017', '1', '5', '7', '17', '23', '3', '8']
['20-01-2017', '10', '17', '27', '31', '49', '3', '5']
['17-01-2017', '4', '16', '25', '43', '47', '2', '10']
['13-01-2017', '3', '7', '16', '26', '50', '4', '7']
['10-01-2017', '2', '11', '29', '35', '44', '4', '9']
['06-01-2017', '10', '14', '18', '21', '49', '9', '11']
['03-01-2017', '19', '23', '27', '34', '49', '1', '11']
try passing HTML text directly
soup = BeautifulSoup(html.text)

Python: sort list except first line

I have this list :
[['Nom', 'Francais', 'Anglais', 'Maths'], ['Catherine', '9', '17', '9'], ['Karim', '12', '15', '11'], ['Rachel', '15', '15', '14'], ['Roger', '12', '14', '12'], ['Gabriel', '7', '13', '8'], ['Francois', '14', '8', '15'], ['Henri', '10', '12', '13'], ['Stephane', '18', '12', '8'], ['Karine', '9', '10', '10'], ['Marie', '10', '10', '10'], ['Claire', '15', '9', '12'], ['Marine', '12', '9', '12']]
I want to sort it with the names (or, in another words, by alphabetical order of the [0] element of each list within the list) but i don't want don't want the first list (['Nom', 'Francais', 'Anglais', 'Maths']) to be sorted with the others , how can in do that ?
Thanks a lot !
You can use range assignment:
>>> from pprint import pprint # just to have a nice display
>>> data = [['Nom', 'Francais', 'Anglais', 'Maths'], ['Catherine', '9', '17', '9'], ['Karim', '12', '15', '11'], ['Rachel', '15', '15', '14'], ['Roger', '12', '14', '12'], ['Gabriel', '7', '13', '8'], ['Francois', '14', '8', '15'], ['Henri', '10', '12', '13'], ['Stephane', '18', '12', '8'], ['Karine', '9', '10', '10'], ['Marie', '10', '10', '10'], ['Claire', '15', '9', '12'], ['Marine', '12', '9', '12']]
>>> pprint(data)
[['Nom', 'Francais', 'Anglais', 'Maths'],
['Catherine', '9', '17', '9'],
['Karim', '12', '15', '11'],
['Rachel', '15', '15', '14'],
['Roger', '12', '14', '12'],
['Gabriel', '7', '13', '8'],
['Francois', '14', '8', '15'],
['Henri', '10', '12', '13'],
['Stephane', '18', '12', '8'],
['Karine', '9', '10', '10'],
['Marie', '10', '10', '10'],
['Claire', '15', '9', '12'],
['Marine', '12', '9', '12']]
>>> data[1:] = sorted(data[1:])
>>> pprint(data)
[['Nom', 'Francais', 'Anglais', 'Maths'],
['Catherine', '9', '17', '9'],
['Claire', '15', '9', '12'],
['Francois', '14', '8', '15'],
['Gabriel', '7', '13', '8'],
['Henri', '10', '12', '13'],
['Karim', '12', '15', '11'],
['Karine', '9', '10', '10'],
['Marie', '10', '10', '10'],
['Marine', '12', '9', '12'],
['Rachel', '15', '15', '14'],
['Roger', '12', '14', '12'],
['Stephane', '18', '12', '8']]
Personally, I'd do something like this. But it assumes you're semi-comfortable with Pandas. This gives you a lot more flexibility to do more with the data.
import pandas as pd
nl = [['Nom', 'Francais', 'Anglais', 'Maths'], ['Catherine', '9', '17', '9'], ['Karim', '12', '15', '11'], ['Rachel', '15', '15', '14'], ['Roger', '12', '14', '12'], ['Gabriel', '7', '13', '8'], ['Francois', '14', '8', '15'], ['Henri', '10', '12', '13'], ['Stephane', '18', '12', '8'], ['Karine', '9', '10', '10'], ['Marie', '10', '10', '10'], ['Claire', '15', '9', '12'], ['Marine', '12', '9', '12']]
df = pd.DataFrame(columns = nl[0])
for l, c in zip(nl[0], range(4)):
df[l] = [ r[c] for r in nl[1:] ]
df.sort_values(by = 'Nom', inplace = True)
df.reset_index(drop = True, inplace = True)
which yields:
Nom Francais Anglais Maths
0 Catherine 9 17 9
1 Claire 15 9 12
2 Francois 14 8 15
3 Gabriel 7 13 8
4 Henri 10 12 13
5 Karim 12 15 11
6 Karine 9 10 10
7 Marie 10 10 10
8 Marine 12 9 12
9 Rachel 15 15 14
10 Roger 12 14 12
11 Stephane 18 12 8
and then if you need a .csv per your most recent comment, it's simply:
df.to_csv('/directory/my_filename.csv', index = False)

Categories

Resources