Home > database >  Combining an element in a tuple to another tuple
Combining an element in a tuple to another tuple

Time:01-19

player_stat =[
    ('Harry Kane', '34', '19'),
    ('Player E', '35', '20'),
    ('Lionel Messi', '34', '14'),
    ('Player F', '35', '11'),
    ('Player A', '35', '17'),
    ('Player B', '35', '15'),
    ('Kylian Mbappe', '35', '18'),
    ('Player C', '35', '18'),
    ('Erling Haaland','35','21'),
    ('Player D', '35', '19'),
]

market_value = [
    ('Erling Haaland','138M'),
    ('Harry Kane', '120M'),
    ('Lionel Messi', '118.7M'),
    ('Kylian Mbappe', '115M'),
    ('Player A', '107M'),
    ('Player B', '108M'),
    ('Player E', '100M'),
    ('Player F', '98M'),
]

I want to for loop through these tuples and basically if the name is equal I want to add the market value to the player stats. (Player C,D have no data)

I tried:

for i in range(len(player_stat)):
    for j in range(len(market_value)):
        if(player_stat[i][0]==market_value[j][0]):
            player_stat[i] = player_stat[i]   (str(market_value[j][1]),)
            break
        else:
            player_stat[i] = player_stat[i]   ('undef',)

Hoping to have End result being:

player_stat =[
    ('Harry Kane', '34', '19', '120M'),
    ('Player E', '35', '20', '100M'),
    ('Lionel Messi', '34', '14', '118.7M'),
    ('Player F', '35', '11', '98M'),
    ('Player A', '35', '17', '107M'),
    ('Player B', '35', '15', '108M'),
    ('Kylian Mbappe', '35', '18', '115M'),
    ('Player C', '35', '18', 'unknown'),
    ('Erling Haaland','35','21', '138M'),
    ('Player D', '35', '19', 'unknown),
]

CodePudding user response:

At least market_value should be converted to a dictionary.

player_stat =[
    ('Harry Kane', '34', '19'),
    ('Player E', '35', '20'),
    ('Lionel Messi', '34', '14'),
    ('Player F', '35', '11'),
    ('Player A', '35', '17'),
    ('Player B', '35', '15'),
    ('Kylian Mbappe', '35', '18'),
    ('Player C', '35', '18'),
    ('Erling Haaland','35','21'),
    ('Player D', '35', '19'),
]

market_value = [
    ('Erling Haaland','138M'),
    ('Harry Kane', '120M'),
    ('Lionel Messi', '118.7M'),
    ('Kylian Mbappe', '115M'),
    ('Player A', '107M'),
    ('Player B', '108M'),
    ('Player E', '100M'),
    ('Player F', '98M'),
]

market_value = dict(market_value)
print(market_value)

This will give you

{'Erling Haaland': '138M',
 'Harry Kane': '120M',
 'Kylian Mbappe': '115M',
 'Lionel Messi': '118.7M',
 'Player A': '107M',
 'Player B': '108M',
 'Player E': '100M',
 'Player F': '98M'}

Now it's a simple list comprehension with a lookup.

players = [player   (market_value.get(player[0], 'unknown'),) for player in player_stat]

The result is

[('Harry Kane', '34', '19', '120M'),
 ('Player E', '35', '20', '100M'),
 [...]
 ('Erling Haaland', '35', '21', '138M'),
 ('Player D', '35', '19', 'unknown')]

CodePudding user response:

Using market_value as lookup dictionary:

market_dict = dict(market_value)
player_stat = [p   (market_dict.get(p[0],'unknown'),) for p in player_stat]
print(player_stat)

[('Harry Kane', '34', '19', '120M'),
 ('Player E', '35', '20', '100M'),
 ('Lionel Messi', '34', '14', '118.7M'),
 ('Player F', '35', '11', '98M'),
 ('Player A', '35', '17', '107M'),
 ('Player B', '35', '15', '108M'),
 ('Kylian Mbappe', '35', '18', '115M'),
 ('Player C', '35', '18', 'unknown'),
 ('Erling Haaland', '35', '21', '138M'),
 ('Player D', '35', '19', 'unknown')]

CodePudding user response:

You can turn second collection into dictionary and use it to fill the results:

mv_dict = dict(market_value)
result = [(x, y, z, mv_dict.get(x, "unknown")) for x, y, z in player_stat]

If you are willing to use external libraries (pandas) you can easily join data with:

players = pd.DataFrame(player_stat, columns=['name', 'games', 'goals'])
mv = pd.DataFrame(market_value, columns=['name', 'value'])
players.merge(mv, how='left').fillna("unknown")

CodePudding user response:

using this OneLiner can help you, but the names of the player is not in the first cell.

print([tuple(set(player market)) for player in player_stat for market in market_value if player[0]==market[0]])

also, I suggest you use Iteration instead of the Tuple of other types, because of the Low memory usage and you can set Unkown values when you give it for print or etc.

CodePudding user response:

I think you can achieve your desired result by converting these tupled list into dictionary and append the player_stat dictionary by iterating over market_value dictionary.

Note: Since you can't add data into tuple, so convert it into a list.

### first create a dict
player_stat={i[0]:list(i[1:]) for i in player_stat}
market_value={key: value for key, value in market_value}

#### append the player_stat dict by iterating over market_value

for k,v in market_value.items():
    if k in player_stat:
        player_stat[k].append(v)
for k,v in player_stat.items():
    if k not in market_value:
        player_stat[k].append('unknown')

print(player_stat) #### a dictionary

Now you can convert player_stat dictionary into list again

player_stat=list(player_stat.items())

now you can get the desired results

print(player_stat)

[('Harry Kane', ['34', '19', '120M']), 
('Player E', ['35', '20', '100M']), 
('Lionel Messi', ['34', '14', '118.7M']), 
('Player F', ['35', '11', '98M']), 
('Player A', ['35', '17', '107M']), 
('Player B', ['35', '15', '108M']), 
('Kylian Mbappe', ['35', '18', '115M']), 
('Player C', ['35', '18','unknown']), 
('Erling Haaland', ['35', '21', '138M']), 
('Player D', ['35', '19','unknown'])]
  • Related