Home > Blockchain >  How to convert negative numbers and zeros into Intigers which are already Strings in a list
How to convert negative numbers and zeros into Intigers which are already Strings in a list

Time:09-16

Hi i like Python very much and even i am 40 i started to lean it. I need some help.

i have a huge list that contains 28920 value in it. Each one is a string.

small = ['1', 'Manchester City', '3', '3', '0', '0', '14', '2', '12', '9', '2', 'Tottenham Hotspur', '3', '3', '0', '0', '7', '2', '5', '9', '3', 'Arsenal', '3', '3', '0', '0', '8', '4', '4', '9', '4', 'Liverpool', '3', '2',
'1', '0', '12', '2', '10', '7', '5', 'Brentford', '3', '2', '1', '0', '10', '3', '7', '7', '6', 'Brighton & Hove Albion', '3', '2', '1', '0', '6', '2', '4', '7', '7', 'Leeds United', '3', '2', '1', '0', '6', '2', '4', '7', '8', 'Fulham', '3', '2', '1', '0', '7', '5', '2', '7', '9', 'Chelsea', '3', '2', '1', '0', '6', '4', '2', '7', '10', 'Manchester United', '3', '2', '0', '1', '6', '4', '2', '6', '11', 'Newcastle United', '3', '1', '2', '0', '5', '3', '2', '5', '12', 'Wolverhampton Wanderers', '3', '1', '2', '0', '2', '1', '1', '5', '13', 'Crystal Palace', '3', '1', '1', '1', '4', '4', '0', '4', '14', 'Southampton', '3', '1', '1', '1', '4', '4', '0', '4', '15', 'Aston Villa', '3', '1', '1', '1', '3', '3', '0', '4', '16', 'AFC Bournemouth', '3', '1', '1', '1', '2', '3', '-1', '4', '17', 'Nottingham Forest', '3', '1', '0', '2', '3', '5', '-2', '3', '18', 'Everton', '3', '0', '2', '1', '1', '2', '-1', '2', '19', 'Leicester City', '3', '0', '1', '2', '3', '5', '-2', '1']

The team names are always at 1th 11th 22th 33.th index ....

All want i have two blank list

team_names = []
numbers = []

How can add team names to team_names list in Strings format as it is and remainings to numbers list in a integer format? But the order of the numbers should be same. After this process i will create my dictionaty.

'0' format makes problem for me.

Thanks very much.

CodePudding user response:

This looks like a football league table. To convert this into a meaningful format you could first split the list by element, and then load it as a pandas DataFrame:

import pandas as pd

def slice_per(source, step):
    return [source[i::step] for i in range(step)]

df = pd.DataFrame(slice_per(small, 10)).T
df[df.columns.difference([1])] = df[df.columns.difference([1])].astype(int) #convert all columns except column 1 to dtype int

This will output the league table:

0 1 2 3 4 5 6 7 8 9
0 1 Manchester City 3 3 0 0 14 2 12 9
1 2 Tottenham Hotspur 3 3 0 0 7 2 5 9
2 3 Arsenal 3 3 0 0 8 4 4 9
3 4 Liverpool 3 2 1 0 12 2 10 7
4 5 Brentford 3 2 1 0 10 3 7 7
5 6 Brighton & Hove Albion 3 2 1 0 6 2 4 7
6 7 Leeds United 3 2 1 0 6 2 4 7
7 8 Fulham 3 2 1 0 7 5 2 7
8 9 Chelsea 3 2 1 0 6 4 2 7
9 10 Manchester United 3 2 0 1 6 4 2 6
10 11 Newcastle United 3 1 2 0 5 3 2 5
11 12 Wolverhampton Wanderers 3 1 2 0 2 1 1 5
12 13 Crystal Palace 3 1 1 1 4 4 0 4
13 14 Southampton 3 1 1 1 4 4 0 4
14 15 Aston Villa 3 1 1 1 3 3 0 4
15 16 AFC Bournemouth 3 1 1 1 2 3 -1 4
16 17 Nottingham Forest 3 1 0 2 3 5 -2 3
17 18 Everton 3 0 2 1 1 2 -1 2
18 19 Leicester City 3 0 1 2 3 5 -2 1

Now you can export it to csv (df.to_csv('table.csv', header= False, index=False)) or excel (df.to_excel('table.xlsx', header=False, index=False)). Or output as a dictionary.

CodePudding user response:

Try:

team_names = []
numbers = []

for i in range(0, len(small), 10):
    x, y, *z = small[i : i   10]
    team_names.append(y)
    numbers.append([x]   z)


print(team_names)
print(numbers)

Prints:

[
    "Manchester City",
    "Tottenham Hotspur",
    "Arsenal",
    "Liverpool",
    "Brentford",
    "Brighton & Hove Albion",
    "Leeds United",
    "Fulham",
    "Chelsea",
    "Manchester United",
    "Newcastle United",
    "Wolverhampton Wanderers",
    "Crystal Palace",
    "Southampton",
    "Aston Villa",
    "AFC Bournemouth",
    "Nottingham Forest",
    "Everton",
    "Leicester City",
]
[
    ["1", "3", "3", "0", "0", "14", "2", "12", "9"],
    ["2", "3", "3", "0", "0", "7", "2", "5", "9"],
    ["3", "3", "3", "0", "0", "8", "4", "4", "9"],
    ["4", "3", "2", "1", "0", "12", "2", "10", "7"],
    ["5", "3", "2", "1", "0", "10", "3", "7", "7"],
    ["6", "3", "2", "1", "0", "6", "2", "4", "7"],
    ["7", "3", "2", "1", "0", "6", "2", "4", "7"],
    ["8", "3", "2", "1", "0", "7", "5", "2", "7"],
    ["9", "3", "2", "1", "0", "6", "4", "2", "7"],
    ["10", "3", "2", "0", "1", "6", "4", "2", "6"],
    ["11", "3", "1", "2", "0", "5", "3", "2", "5"],
    ["12", "3", "1", "2", "0", "2", "1", "1", "5"],
    ["13", "3", "1", "1", "1", "4", "4", "0", "4"],
    ["14", "3", "1", "1", "1", "4", "4", "0", "4"],
    ["15", "3", "1", "1", "1", "3", "3", "0", "4"],
    ["16", "3", "1", "1", "1", "2", "3", "-1", "4"],
    ["17", "3", "1", "0", "2", "3", "5", "-2", "3"],
    ["18", "3", "0", "2", "1", "1", "2", "-1", "2"],
    ["19", "3", "0", "1", "2", "3", "5", "-2", "1"],
]

CodePudding user response:

You can convert string into integers using int(string) which will cause an error if not possible.

For example:

>>> int('1')
1
>>> int('Brentford')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'Brentford'

invalid literal for int() with base 10: 'Brentford'

So, you could attempt to get the int and catch the exception, in a loop around all the values in your existing small list.

for value in small:
    try:
        numbers.append(int(value))
    except ValueError:
        team_names.append(value)

CodePudding user response:

small = ['1', 'Manchester City', '3', '3', '0', '0', '14', '2', '12', '9', '2', 'Tottenham Hotspur', '3', '3', '0', '0', '7', '2', '5', '9', '3', 'Arsenal', '3', '3', '0', '0', '8', '4', '4', '9', '4', 'Liverpool', '3', '2',
        '1', '0', '12', '2', '10', '7', '5', 'Brentford', '3', '2', '1', '0', '10', '3', '7', '7', '6', 'Brighton & Hove Albion', '3', '2', '1', '0', '6', '2', '4', '7', '7', 'Leeds United', '3', '2', '1', '0', '6', '2', '4', '7', '8', 'Fulham', '3', '2', '1', '0', '7', '5', '2', '7', '9', 'Chelsea', '3', '2', '1', '0', '6', '4', '2', '7', '10', 'Manchester United', '3', '2', '0', '1', '6', '4', '2', '6', '11', 'Newcastle United', '3', '1', '2', '0', '5', '3', '2', '5', '12',
 'Wolverhampton Wanderers', '3', '1', '2', '0', '2', '1', '1', '5', '13', 'Crystal Palace', '3', '1', '1', '1', '4', '4', '0', '4', '14', 'Southampton', '3', '1', '1', '1', '4', '4', '0', '4', '15', 'Aston Villa', '3', '1', '1', '1', '3', '
3', '0', '4', '16', 'AFC Bournemouth', '3', '1', '1', '1', '2', '3', '-1', '4', '17', 'Nottingham Forest', '3', '1', '0', '2', '3', '5', '-2', '3', '18', 'Everton', '3', '0', '2', '1', '1', '2', '-1', '2', '19', 'Leicester City', '3', '0',
'1', '2', '3', '5', '-2', '1']

team_names = []
numbers = []

for item in small:
    try:
        # is this an integer?
        int_value =  int(item)

        # yes, add it to the numbers list
        numbers.append(int_value)
    except ValueError as e:
        # it is not, assume it's a string, add it to the names list
        team_names.append(item)


print(team_names)
print(numbers)

This code snippet will extract numbers if they can be converted to integers, and leave the strings as strings.

It's not very efficient, but it should run fine even on a dataset with a few tens of thousands of entries.

  • Related