英文:
How to write a list comprehension instead of a nested loop In Python?
问题
Here's a revised version of your code using list comprehensions to determine the location of NBA teams based on their names:
city = [
next((k for k in cities['city'] if any(j == l for l in k.split())), 'Not in list')
for i in nba_df['team']
]
nba_df['city'] = city
This code iterates through the nba_df['team']
values, and for each team name, it searches for a matching city in the cities['city']
column using a generator expression with next
. If a matching city is found, it's added to the city
list; otherwise, 'Not in list' is added.
This code is more concise and efficient than the nested loop approach and should work correctly for your task.
英文:
I was dealing with different dataframes in python. The first dataframe named cities looks like this:
city Population NBA
0 New York City 20153634 Knicks Nets
1 Los Angeles 13310447 Lakers Clippers
2 San Francisco Bay Area 6657982 Warriors
3 Chicago 9512999 Bulls[note 9]
4 Dallas–Fort Worth 7233323 Mavericks
And second dataframe named nba_df look like this:
team W L W/L% GB PS/G PA/G SRS year League
0 Toronto Raptors 59 23 0.720 0.0 111.7 103.9 7.29 2018 NBA
1 Boston Celtics 55 27 0.671 4.0 104.0 100.4 3.23 2018 NBA
2 Philadelphia 76ers 52 30 0.634 7.0 109.8 105.3 4.30 2018 NBA
3 Cleveland Cavaliers 50 32 0.610 9.0 110.9 109.9 0.59 2018 NBA
4 Indiana Pacers 48 34 0.585 11.0 105.6 104.2 1.18 2018 NBA
What I am trying to do is to determine where the team is located from teams name. For ex. Philadelphia 76ers are located at Philadelpia. So to do that I wrote a nested loop below:
city = []
for i in nba_df['team']:
added = False
for j in i.split():
for k in cities['city']:
for l in k.split():
if j == l and added == False:
city.append(k)
added = True
if added == False:
city.append('Not in list')
nba_df['city'] = city
The loop iterates through the nba_df['team'] and splits the string and iterates for each word over the cities['city'] column which are splitted too, and if a word matches with the other, then the city name will be added into list. Otherwise it will be added as Not in list. I know it has some bugs, what will happen if New York City is being iterated before just saying as an example New Orleans? Than it will append the New York City to the list and skip the rest of it. So it might append wrong cities. Anyways code does what I want but I wanted to write it in list comprehension. Because it looks amateur to me.
I tried something like this:
city = [k if j == l for l in k.split() in [k for k in cities['city'] in [j for j in i.split() in [i for i in nba_df['team']]]]]
But of course it didn't work. It raised and sytanx error pointing on l for l
which is at the beginning.
What I am asking for you is to review my loop and I wonder how would you rewrite it? List comprehension or another way. How would you approach to the problem?
Thank you.
答案1
得分: 1
你在列表推导式的第一个split()函数中忘记了大括号。
英文:
You forgot the braces at the first split() in the list comprehention.
答案2
得分: 1
尝试这个:
city = [k if j == l else j for l in k.split() for k in cities['city'] for j in i.split() for i in nba_df['team']]
而不是这个:
city = [k if j == l else j for l in k.split for k in cities['city'] for j in i.split() for i in nba_df['team']]
英文:
try this:
city = [k if j == l for l in k.split() in [k for k in cities['city'] in [j for j in i.split() in [i for i in nba_df['team']]]]]
instead of this:
city = [k if j == l for l in k.split in [k for k in cities['city'] in [j for j in i.split() in [i for i in nba_df['team']]]]]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论