``` 我正试图用Python解决生日悖论。 我很接近，但最后一块让我感到茫然。 我正在使用随机生成给定范围和要创建的项目数的数字列表。 这样可行。
然后我检查列表（上面生成的）是否有重复。 这样可行。
然后我尝试生成给定（n）列表。 这是我遇到麻烦的地方。 它生成一个列表然后返回“NoneType”不可迭代。 让我感到困惑的是，列表已生成，但Python并未将其视为列表。
这是代码：
```def make_bd(n, r):
"""Generates (r) numbers of birthdays in a range (n)."""
import random
t = [random.randrange(n) for i in range(r)]
print (t)

def has_dupe(test):
"""Here I test to see if I can detect a duplicate birthday.
This is based on problem #4."""

d = []
count = 0
for number in test:
if number in d:
count = count + 1
d.append(number)
if count >= 1:
return True
return False

def dupebd(n,r,t):
count_dupe = 0
for i in range(n):
if has_dupe(make_bd(r,t)):
count_dupe = count_dupe + 1
print (float(count)/n)

dupebd(50,365,23)
```
这是结果：
```>>> has_dupe(make_bd(50,6))
[13, 3, 8, 29, 34, 44]
Traceback (most recent call last):
File "<pyshell#45>", line 1, in <module>
has_dupe(make_bd(50,6))
File "<pyshell#44>", line 7, in has_dupe
for number in test:
TypeError: 'NoneType' object is not iterable
```I'm trying to solve the Birthday Paradox with Python. I'm close but the last piece has me at a loss. I'm using random to generate a list of numbers given a range and number of items to create. That works.
I then check to see if a list (generated above) has duplicates. That works.
I then try to generate a given (n) of lists. Here is where I run into trouble. It generates one list then returns "NoneType" is not iterable. What puzzles me is, the list is generated but Python is not seeing it as a list.
Here's the code:
```def make_bd(n, r):
"""Generates (r) numbers of birthdays in a range (n)."""
import random
t = [random.randrange(n) for i in range(r)]
print (t)

def has_dupe(test):
"""Here I test to see if I can detect a duplicate birthday.
This is based on problem #4."""

d = []
count = 0
for number in test:
if number in d:
count = count + 1
d.append(number)
if count >= 1:
return True
return False

def dupebd(n,r,t):
count_dupe = 0
for i in range(n):
if has_dupe(make_bd(r,t)):
count_dupe = count_dupe + 1
print (float(count)/n)

dupebd(50,365,23)
```
Here's the result:
```>>> has_dupe(make_bd(50,6))
[13, 3, 8, 29, 34, 44]
Traceback (most recent call last):
File "<pyshell#45>", line 1, in <module>
has_dupe(make_bd(50,6))
File "<pyshell#44>", line 7, in has_dupe
for number in test:
TypeError: 'NoneType' object is not iterable
```原文：https://stackoverflow.com/questions/10374256```

最满意答案

``` 在第5行中，您打印`t`但不返回它，因此`make_bd`返回`None` 。 将行更改为
```return t
```In line 5 you print `t` but do not return it, so that `make_bd` returns `None`. Change the line to
```return t
``````
2012-04-29

解析表时Python处理NoneType(Python handling NoneType when parsing tables)

itertools.zip_longest接受可选的fillvalue参数。 如果提供，则使用它而不是None ： >>> list(itertools.zip_longest([1, 2, 3], [4, 5])) [(1, 4), (2, 5), (3, None)] >>> list(itertools.zip_longest([1, 2, 3], [4, 5], fillvalue='NULL')) [(1, 4), (2, 5), (3, 'NULL')] 您可以提供空行（NULL值

AttributeError：'NoneType'对象没有属性'findChildren'（美丽的汤）(AttributeError: 'NoneType' object has no attribute 'findChildren' (Beautiful Soup))

soup.find('ul',{'class': "tags"})返回None 。 如果要在列表推导中使用它，则需要在使用它们之前过滤掉None值。 有一个技巧，你可以将值放在列表中，以便过滤它： tags_dict['tags'] = [child.text for tag in [soup.find('ul',{'class': "tags"})] if tag fo

创建元组列表，元组元素作为输入(Create list of tuples , tuple elements taken as Input)

append()函数不返回附加列表，它返回NoneType因为它执行附加到位 ，因此您只需更改行： l=l.append(tuple(i)) 至： l.append(tuple(i)) 这应该工作。 The append() function does not return an appended list, it returns a NoneType because it performs the append in place, so you can simply change the