Python如果在字符串中多次

时间:2013-12-03 02:44:58

标签: python string sorting grouping

这是我将一个接一个地将男性和女性分类到变量'group'中的脚本:

males = ['John Smith 1 : M', 'John Smith 2 : M', 'John Smith 3 : M', 'John Smith 4 : M', 'John Smith 5 : M', 'John Smith 6 : M', 'John Smith 7 : M', 'John Smith 8 : M', 'John Smith 9 : M', 'John Smith 10 : M']
females = ['Jane Smith 1 : F', 'Jane Smith 2 : F', 'Jane Smith 3 : F', 'Jane Smith 4 : F', 'Jane Smith 5 : F', 'Jane Smith 6 : F']

group_number = 3
while int(group_number)%2 != 0:
    group_number = raw_input("What is the size of the group? (even numbers only)")

group = []
a=0

for a in range(max(len(males), len(females))):
    if a < len(males):
        group.append(males[a])
    if a < len(females):
        group.append(females[a])
    a=a+1

group = zip(*(iter(group),) * int(group_number))

print group

现在我需要做的就是将输入的数字(仅限偶数)拆分,然后将不平衡的男性:女性组放在一个单独的组中...我认为它是2,但它不起作用任何事情&gt; 2

n=0

for person in group:
    if " : M" and " : F" in str(person):
            print "Group",n,person
            print "--------------------------------------------------------"
    else:
        print "UNBALANCED",person
    n=n+1

1 个答案:

答案 0 :(得分:1)

应为if " : M" in str(person) and " : F" in str(person):

您的代码的含义是:

if (" : M") and (" : F" in str(person) ):

我认为不需要for循环中的a = a +1