Make a program that filters a list of strings and returns a list with only your friends name in it. If a name has exactly 4 letters in it, you can be sure that it has to be a friend of yours! Otherwise, you can be sure he's not... Ex: Input = ["Ryan", "Kieran", "Jason", "Yous"], Output = ["Ryan", "Yous"]
时间: 2024-03-31 11:33:55 浏览: 158
好友列表的实现
5星 · 资源好评率100%
Sure, I can help you with that! Here's a Python program that filters a list of strings and returns only your friends' names:
```
def filter_friends(names):
friends = []
for name in names:
if len(name) == 4:
friends.append(name)
return friends
```
You can use this function by passing in a list of names as an argument, like this:
```
names = ["Ryan", "Kieran", "Jason", "Yous"]
friends = filter_friends(names)
print(friends)
```
The output will be:
```
["Ryan", "Yous"]
```
This is because "Ryan" and "Yous" both have exactly 4 letters, so they are your friends! "Kieran" and "Jason" have more than 4 letters, so they are not your friends.
阅读全文