按性别划分姓名列表

时间:2019-04-04 11:44:19

标签: python sorting tuples

我的代码有问题。我必须创建一个给定名称列表(男性和女性)的函数,以创建两个新列表。

一个带有男性名字,另一个带有女性名字。问题在于这些是波兰语名字,因此女性名字以server { listen 80; server_name domain.com www.domain.com; return 301 https://www.$server_name$request_uri; } #################### upstream backend { server internal-new--domain-6754.ap-north-1.elb.amazonaws.com:8080; } server { #listen 443 ssl; listen 443; server_name domain.com; ssl on; ssl_certificate /home/STAR_domain.crt; ssl_certificate_key /home/domain_com.key; root /home/ubuntu/app/pub; autoindex off; proxy_buffering on; proxy_buffer_size 16k; proxy_buffers 24 24k; proxy_busy_buffers_size 24k; proxy_max_temp_file_size 2048m; proxy_temp_file_write_size 32k; location / { try_files $uri @backend; } location @backend { proxy_pass http://backend; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # Following is necessary for Websocket support proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } } 结尾。

这是我到目前为止所拥有的:

a

8 个答案:

答案 0 :(得分:2)

females = []
males = []
for name in names:
    if name.endswith("a"):
        females.append(name)
    else:
        males.append(name)

答案 1 :(得分:2)

您可以使用循环中的单个语句来完成此操作:

names = sorted(["Andrzej", "Henryk", "Alicja", "Cezary", "Barbara"])

females, males = [], []
for name in names:
    (females if name[-1] == "a" else males).append(name)

print(females)  # -> ['Alicja', 'Barbara']
print(males)    # -> ['Andrzej', 'Cezary', 'Henryk']

这是仍然很易读的另一种可能性(如果您了解Python):

males = []
females = [name for name in names if name[-1] == "a" or males.append(name)]

只需想到另一种(非常适当地)使用groupby的变体:

from itertools import groupby

names = sorted(["Andrzej", "Henryk", "Alicja", "Cezary", "Barbara"])

males, females = [], []
for sex, firstnames in groupby(names, lambda name: name[-1] == "a"):
    [males, females][sex].extend(firstnames)

答案 2 :(得分:1)

female_names = [i for i in names if i.endswith('a')]

答案 3 :(得分:1)

如果您有列表names,则可以简单地对其进行遍历,然后将男性添加到一个列表,将女性添加到另一个列表:

names = ["Andrzej", "Henryk", "Alicja", "Cezary", "Barbara"]
males = []
females = []
for name in names:
    if name[-1] == 'a':
        females.append(name)
    else:
        males.append(name)

然后,您可以按照需要对它们进行排序:

females.sort()
males.sort()

答案 4 :(得分:1)

names = ["Andrzej", "Henryk", "Alicja", "Cezary", "Barbara"]

females = [name for name in names if name[-1] == 'a']
males = [name for name in names if name not in females]

答案 5 :(得分:1)

您可以使用str.endswith()

names = ["Andrzej", "Henryk", "Alicja", "Cezary", "Barbara"]

males = []
females = []

for name in names:
    if name.endswith("a"):
        females.append(name)
    else:
        males.append(name)

print(males, females)

输出:

['Andrzej', 'Henryk', 'Cezary'] ['Alicja', 'Barbara']

答案 6 :(得分:1)

使用str.endswith()

names = ["Andrzej", "Henryk", "Alicja", "Cezary", "Barbara"]

males = [m for m in names if not m.endswith('a')]  # ['Andrzej', 'Henryk', 'Cezary']
females = [f for f in names if f not in males]      # ['Alicja', 'Barbara']

答案 7 :(得分:0)

names = ["Andrzej", "Henryk", "Alicja", "Cezary", "Barbara"]
for name in names:
    if name.endswith("a") == True:
        print("F")
    else:
        print("M")