简体   繁体   English

打印列表中的所有字符串元素,除了 Python 中的一个

[英]Printing all string elements in list except for one in Python

So I'm making a scheduling program that randomly schedules NHL teams.所以我正在制作一个随机安排 NHL 球队的日程安排程序。 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:当用户输入团队缩写时,他们会打印给他们的元素,减去列表中的团队,例如,如果我输入BUF ,用户将返回:

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:一个简单的list推导式将生成一个没有不需要的元素的新list

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 .这会临时创建一个新list ,其中包含除等于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. *解包为print顺序位置参数,并且sep="\\n"告诉print在打印时在每个元素之间放置一个换行符。 If you want to permanently remove the element in question from the list , you can instead do:如果您想从list永久删除有问题的元素,您可以改为:

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 ;请注意,如果没有完全匹配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 .如果不存在匹配的元素, remove将抛出异常,并且如果list有多个值的实例,它只会删除一个副本。 The list comprehension will silently remove all copies of the value, and doesn't care if there weren't any to start with. list推导式将默默地删除该值的所有副本,并且不关心是否没有任何副本。

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.一种可以说是更 Pythonic 的做同样事情的方式,虽然效率不高(像这样的小列表效率并不那么重要)你可以做一个列表理解,并制作一个列表。 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 .在这两种解决方案中的任何一个中,您都可以更加宽容,并将.upper()类的字符串转换应用于input_team以允许它,以便输入buf的用户仍然可以获得预期的列表, 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']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 增加列表的所有元素,除了 Python 中的第一个元素 - Increase all elements of a list except the first one in Python 如果字符串在列表中,则打印列表的所有元素? - Printing all elements of list if string is in list? 从列表中删除除字符串之外的所有元素 - Remove all elements except string from list 编写一个 Python 函数将给定列表的所有元素(最后一个元素除外)连接成一个字符串并返回该字符串 - Write a Python function to concatenate all elements (except the last element) of a given list into a string and return the string 列表不打印所有元素? - List not printing all elements? 在python中替换字符串中除N、n和空格之外的所有元素 - Replace all elements in a string except N, n and spaces in python 如何从列表中删除除Python中的一个元素之外的所有重复项? - How to remove all duplicates from list except one element in Python? 使用正则表达式查找字符串中除单词中的所有单词以外的所有单词 - Find all the words in string except one word in python with regex Python:将字符串拆分为列表,取出除“ - Python: Split a string into a list, taking out all special characters except ' 在列表列表中检索除一个元素之外的所有元素(Python) - Retrieving all but one elements in a list of lists (Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM