简体   繁体   中英

Printing all string elements in list except for one in Python

So I'm making a scheduling program that randomly schedules NHL teams. I have a mini list of teams from the Atlantic Division:

atl_div = ["BOS", "BUF", "DET", "FLA"]

When a user enters a team abbreviation, they get the elements printed to them, minus the team in the list, for example if I entered BUF , the user would get back:

BOS
DET
FLA

I tried

input_team = input("Enter a team abbrev.:")

if input_team == "BUF":
   print(atl_div[~0])

but that won't work, since ~ is used on numbers and not lists.

I have to admit I'm a bit of a novice programmer, so I apologize if it sounds that way in my question. We all start somewhere :)

Thank you for any help and feel free to ask for more details if necessary!

input_team = input('Enter a team abbrev.:')
print('\n'.join([team for team in atl_div if team != input_team]))

EDIT: Updated print formatting

A simple list comprehension will produce a new list without the undesired element:

print(*[team for team in atl_div if team != input_team], sep="\n")

That makes a new list temporarily with all elements except those equal to input_team . The * unpacks it as sequential positional arguments to print , and sep="\\n" tells print to put a newline between each element when printing. If you want to permanently remove the element in question from the list , you can instead do:

atl_div.remove(input_team)
print(*atl_div, sep="\n")

Note that the behavior will differ a bit if there isn't exactly one entry matching input_team ; remove will throw an exception if no matching element exists, and it will only remove one copy if there is more than one instance of the value in the list . The list comprehension will silently remove all copies of the value, and doesn't care if there weren't any to start with.

Two easy ways you can do this:

The simplest way would be to go one by one through the list, and print any one that is not the string you are explicitly looking not to pick.

for team in atl_div:
    if input_team != team:
        print(team)

An arguably more Pythonic way of doing the same thing, although not as efficient (with a small list like this efficiency is not as important) you can do a list comprehension, and make a list like. This way, you're going to make a new list, which doesn't have the team you're looking for.

included_teams = [x for x in atl_div if x != input_team]

You can then print out that list, element by element:

for team in included_teams:
    print(included_teams)

This can be simplified to the following, perhaps slightly less readable version of that same problem:

for team in [x for x in atl_div if x != input_team]:
    print(team)

In either of these solutions, you can be more forgiving, and apply string transforms like .upper() to the input_team to allow it so that a user who enters buf would still get the expected list, BOS, DET, FLA .

With the first answer, this can be done like so:

for team in atl_div:
    if input_team.upper() != team:
        print(team)

If you want to use pandas you can do this:

import pandas as pd
aa = pd.Series(["BOS", "BUF", "DET", "FLA"]) 
aa[aa != 'BOS'].tolist()
# ['BUF', 'DET', 'FLA']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM