如何在使用多个 if 语句时存储数字?

您应该将可选匹配项移动到一个捕获组中:


import pandas as pd


data = """\

2930 Beverly Glen Circle Los Angeles

435 S. La Cienega Blvd. Los Angeles

12224 Ventura Blvd. Studio City

9570 Wilshire Blvd. Beverly Hills

26025 Pacific Coast Hwy. Malibu""".split('\n')


df = pd.DataFrame(data)

print(df)


cities = ['Los Angeles', 'Studio City', 'Beverly Hills','Malibu']

c = '|'.join(cities)

pat = fr'(.*?)\s({c})'                     # fixed pattern with f and r

df = df[0].str.extract(pat,expand=True)

print(df)

输出:


                          0              1

0  2930 Beverly Glen Circle    Los Angeles

1   435 S. La Cienega Blvd.    Los Angeles

2       12224 Ventura Blvd.    Studio City

3       9570 Wilshire Blvd.  Beverly Hills

4  26025 Pacific Coast Hwy.         Malibu

分享

编辑

跟随

于 2020 年 6 月 4 日 1


弑天下
浏览 90回答 1
1回答

婷婷同学_

这是固定版本。我建议你再做一些工作:)from random import choicet = ["rock", "paper", "scissors"]tie = 0lose = 0win = 0for i in range(0, 10):    print("1... 2... 3... go!")    # you need to refresh these variables on every for iteration    computer = choice(t)    player = None    # if you're using while to make sure player inputs, that's the only thing that needs    # to be within the while loop    while not player:        player = input("rock, paper, scissors: ")    print("Computer: ", computer)    print("User: ", player)    # I would look for a way to simplify determining the winner    if player == computer:        # tie += 1 is the same as tie = tie + 1        tie +=1        print("Tie!")    elif player == "rock":        if computer == "paper":            lose += 1            print("You lose!")        else:            win += 1            print("You win!")    elif player == "paper":        if computer == "scissors":            lose += 1            print("You lose!")        else:            win += 1            print("You win!")    elif player == "scissors":        if computer == "rock":            lose += 1            print("You lose!")        else:            win += 1            print("You win!")    else:        print("That's not a valid play. Check your spelling!")print("Final Tally")print("************")print("User Wins: ", win)print("Computer Wins: ", lose)print("Ties: ", tie)if tie > win and tie > lose:    print("It's a tie!")elif win > tie and win > lose:    print("You won!")else:    print("The computer won!")更新:显然我无事可做。好吧,这是一种简化获胜条件的直接方法。    win_condition_rock = player == 'rock' and computer == 'scissors'    win_condition_paper = player == 'paper' and computer == 'rock'    win_condition_scissors = player == 'scissors' and computer == 'paper'    if player == computer:        # tie += 1 is the same as tie = tie + 1        tie +=1        print("Tie!")    elif any([win_condition_paper, win_condition_scissors, win_condition_rock]):        win += 1        print('You win')    else:        lose += 1        print('You lose')更新 2:这是对有效输入的检查    while player not in t:        player = input("rock, paper, scissors: ").lower()        if player not in t:            print('Invalid input')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python