如何在 Python 中避免区分大小写
How to avoid case sensitivity in Python
我有这段代码,想比较两个列表。
list2= [('Tom','100'),('Alex','200')]
list3= [('tom','100'),('alex','200')]
non_match = []
for line in list2:
if line not in list3:
non_match.append(line)
print(non_match)
结果将是:
[('Tom', '100'), ('Alex', '200')]
因为区分大小写!在这种情况下有什么办法可以避免区分大小写吗?我不想将列表更改为大写或小写。
或者任何其他可以匹配这些列表的方法?
您无法避免将数据转换为某种不区分大小写的格式,在某些时候。
您可以做的是避免重新创建完整列表:
def make_canonical(line):
name, number = line
return (name.lower(), number)
non_match = []
for line2 in list2:
search = make_canonical(line2)
for line3 in list3:
canonical = make_canonical(line3)
if search == canonical:
break
else:
# Did not hit the break
non_match.append(line3)
使用lower将元组转换为小写进行比较
list2= [('Tom','100'),('Alex','200')]
list3= [('tom','100'),('alex','200')]
non_match = []
for line in list2:
name, val = line
if (name.lower(), val) not in list3:
non_match.append(line)
print(non_match)
您还需要在循环内迭代元组
for line2 in list2:
for line3 in list3:
if len(line3) == len(line2):
lenth = len(line3)
successCount = 0
match = False
for i in range(lenth):
result = line2[i].lower() == line3[i].lower()
if result == True:
successCount = successCount +1;
result = False
if successCount == lenth:
non_match.append(line2)
print(non_match)
享受......
您可以通过从您的元组列表中创建两个字典并比较列表,使游戏中的 int
和空格混合更加通用:
def unify(v):
return str(v).lower().strip()
list2= [('Tom ','100'),(' AleX',200)]
list3= [('toM',100),('aLex ','200')]
d2 = {unify(k):unify(v) for k,v in list2} # create a dict
d3 = {unify(k):unify(v) for k,v in list3} # create another dict
print(d2 == d3) # dicts compare (key,value) wise
应用的方法将从整数生成字符串,去除空格,然后比较字典。
输出:
True
这对我有用!两个列表都将转换为小写。
list2= [('Tom','100'),('Alex','200'),('Tom', '13285')]
list3= [('tom','100'),('ALex','200'),('Tom', '13285')]
def make_canonical(line):
name, number = line
return (name.lower(), number)
list22 = []
for line2 in list2:
search = make_canonical(line2)
list22.append(search)
list33 =[]
for line3 in list3:
search1 = make_canonical(line3)
list33.append(search1)
non_match = []
for line in list22:
if line not in list33:
non_match.append(line)
print(non_match)
我有这段代码,想比较两个列表。
list2= [('Tom','100'),('Alex','200')]
list3= [('tom','100'),('alex','200')]
non_match = []
for line in list2:
if line not in list3:
non_match.append(line)
print(non_match)
结果将是:
[('Tom', '100'), ('Alex', '200')]
因为区分大小写!在这种情况下有什么办法可以避免区分大小写吗?我不想将列表更改为大写或小写。
或者任何其他可以匹配这些列表的方法?
您无法避免将数据转换为某种不区分大小写的格式,在某些时候。 您可以做的是避免重新创建完整列表:
def make_canonical(line):
name, number = line
return (name.lower(), number)
non_match = []
for line2 in list2:
search = make_canonical(line2)
for line3 in list3:
canonical = make_canonical(line3)
if search == canonical:
break
else:
# Did not hit the break
non_match.append(line3)
使用lower将元组转换为小写进行比较
list2= [('Tom','100'),('Alex','200')]
list3= [('tom','100'),('alex','200')]
non_match = []
for line in list2:
name, val = line
if (name.lower(), val) not in list3:
non_match.append(line)
print(non_match)
您还需要在循环内迭代元组
for line2 in list2:
for line3 in list3:
if len(line3) == len(line2):
lenth = len(line3)
successCount = 0
match = False
for i in range(lenth):
result = line2[i].lower() == line3[i].lower()
if result == True:
successCount = successCount +1;
result = False
if successCount == lenth:
non_match.append(line2)
print(non_match)
享受......
您可以通过从您的元组列表中创建两个字典并比较列表,使游戏中的 int
和空格混合更加通用:
def unify(v):
return str(v).lower().strip()
list2= [('Tom ','100'),(' AleX',200)]
list3= [('toM',100),('aLex ','200')]
d2 = {unify(k):unify(v) for k,v in list2} # create a dict
d3 = {unify(k):unify(v) for k,v in list3} # create another dict
print(d2 == d3) # dicts compare (key,value) wise
应用的方法将从整数生成字符串,去除空格,然后比较字典。
输出:
True
这对我有用!两个列表都将转换为小写。
list2= [('Tom','100'),('Alex','200'),('Tom', '13285')]
list3= [('tom','100'),('ALex','200'),('Tom', '13285')]
def make_canonical(line):
name, number = line
return (name.lower(), number)
list22 = []
for line2 in list2:
search = make_canonical(line2)
list22.append(search)
list33 =[]
for line3 in list3:
search1 = make_canonical(line3)
list33.append(search1)
non_match = []
for line in list22:
if line not in list33:
non_match.append(line)
print(non_match)