(1)定义Set集合,存入多个字符串 (2)删除集合中不满足身份证号码的数据 身份证要求: - 长度必须18位 - 数字0不能开头 - 除了最后一位,中间不允许有数字 - 最后一位可以是数字或者Xx (3)然后利用迭代器遍历集合元素并输出
时间: 2023-06-11 10:09:52 浏览: 80
map/list集合转化成xml字符串 xml字符串转化成map/list集合
5星 · 资源好评率100%
(1)定义Set集合,存入多个字符串:
```python
string_set = {"Hello", "World", "123", "身份证号码", "430524199901010011", "43052419990101001X", "012345678901234567"}
```
(2)删除集合中不满足身份证号码的数据:
```python
for s in string_set.copy():
if len(s) != 18 or s[0] == '0' or not s[0:17].isdigit() or (s[17] not in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'x', 'X']):
string_set.remove(s)
```
(3)然后利用迭代器遍历集合元素并输出:
```python
for s in string_set:
print(s)
```
完整代码如下:
```python
string_set = {"Hello", "World", "123", "身份证号码", "430524199901010011", "43052419990101001X", "012345678901234567"}
for s in string_set.copy():
if len(s) != 18 or s[0] == '0' or not s[0:17].isdigit() or (s[17] not in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'x', 'X']):
string_set.remove(s)
for s in string_set:
print(s)
```
阅读全文