使用python循环遍历列表并插入str.contains

我的目做每一个动作。


DataFramedf有几列,但我只想将该函数应用于标题为“条目”的列,该列包含在线扑克桌上发生的所有操作的日志(列中的每一行都是一个字符串)。


这是“条目”列的样子(每行都是一个字符串):


-- ending hand #174 --

"Prof @ ZY_G_5ZOve" gained 100

"tom_thumb @ g1PBaozt7k" folds

"Prof @ ZY_G_5ZOve" calls with 50

"tom_thumb @ g1PBaozt7k" checks

river: 9♦, 5♣, Q♥, 7♠ [K♠]

"Prof @ ZY_G_5ZOve" checks

"tom_thumb @ g1PBaozt7k" checks

turn: 9♦, 5♣, Q♥ [7♠]

"Prof @ ZY_G_5ZOve" checks

"tom_thumb @ g1PBaozt7k" checks

flop:  [9♦, 5♣, Q♥]

"Prof @ ZY_G_5ZOve" checks

"tom_thumb @ g1PBaozt7k" calls with 50

"Bob T. @ fjZTXUGV2G" folds

"danny G @ tNE1_lEFYv" folds

"Prof @ ZY_G_5ZOve" posts a big blind of 50

"tom_thumb @ g1PBaozt7k" posts a small blind of 25

-- starting hand #174  (Texas Hold'em) (dealer: "Bob T. @ fjZTXUGV2G") --

-- ending hand #173 --

"tom_thumb @ g1PBaozt7k" gained 475

"danny G @ tNE1_lEFYv" folds

"Prof @ ZY_G_5ZOve" folds

"tom_thumb @ g1PBaozt7k" raises with 356

flop:  [4♥, Aâ™ , 6â™ ]

"danny G @ tNE1_lEFYv" calls with 150

"Prof @ ZY_G_5ZOve" calls with 150

"tom_thumb @ g1PBaozt7k" raises with 150

"Bob T. @ fjZTXUGV2G" folds

"danny G @ tNE1_lEFYv" calls with 50

"Prof @ ZY_G_5ZOve" calls with 50

"tom_thumb @ g1PBaozt7k" posts a big blind of 50

"Bob T. @ fjZTXUGV2G" posts a small blind of 25

-- starting hand #173  (Texas Hold'em) (dealer: "danny G @ tNE1_lEFYv") --

这是我尝试过的一些示例代码:


player_list = ['danny G', 'Jane', 'Prof', 'spn', 'tim', 'Bob T.', 'joon', 'tom_thumb']

action_list = ['call', 'fold']


def action_amount(df, player_list, action):

    for player in player_list:

        action_number =len(df[df['entry'].str.contains('(player).*(action)', regex=True)])

        print(f'{player} {action}ed {action_number} times.')


action_amount(df, player_list, 'call')

值得注意的是,len(df[df['entry'].str.contains('(danny G).*(call)', regex=True)])返回正确的值(我正在使用正则表达式,因为我要查找的两个词在同一行中,中间有一堆不同的字符)。


该问题似乎与尝试将值循环到str.contains. 我如何遍历列表并打印姓名以及该人执行给定输入操作的次数?


理想情况下,我希望同时遍历代码顶部的两个列表。


Qyouu
浏览 136回答 1
1回答

慕码人8056858

这行得通吗?def action_amount(df, player_list, action_list):    for player in player_list:        for action in action_list:            pattern = f'{player}.*{action}'            matching_rows = df[df['entry'].str.contains(pattern, regex=True)]            action_number = len(matching_rows)            print(f'{player} {action}ed {action_number} times.')action_amount(df, player_list, possible_actions)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python