Python如果多次使用字符串
下面是我将男性和女性一个接一个地分类到变量“group”中的脚本:Python如果多次使用字符串,python,string,sorting,grouping,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'] female
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
如果str(person)中的“:M”和str(person)中的“:F”,则应为
您的代码的意思是:
if (" : M") and (" : F" in str(person) ):
我认为for循环中的a=a+1
是不需要的。似乎for循环将更新a的值,即使您更改了循环中的a。它不同于C/C++中的for循环。
if (" : M") and (" : F" in str(person) ):