如何比较两个不同列表中的两个字母或值?
How do I compare two letters or values in two different lists?
我正在制作一个包含两个列表(在 Python 中)的程序,每个列表包含 5 个不同的字母。我如何才能使我为两个列表选择的任何索引号进行比较,如果条件为真,则将字母大写?如果列表中的前两个值相同(在我的例子中是小写字母),那么我希望第二个列表中的字母变为大写。
example/attempt(我不知道我在做什么):
if list1[0] = list2[0]:
upper(list2[0])
我想你可以使用这样的东西:
def compare_and_upper(lst1, lst2):
for i in range(len(lst1)):
if lst1[i].upper() == lst2[i].upper():
return lst1[i].upper()
return None
如果没有输入和输出的示例,很难理解您的目标是什么,但是如果您的目标是在 list2
中的任何字符串上使用 .upper()
,其中 list1[i]
和list2[i]
相等,可以用zip
和enumerate
的组合来比较,然后把list2[i]
的值赋给大写的字符串,像这样:
list1 = ['a', 'b', 'c']
list2 = ['a', 'p', 'q']
for i, (x, y) in enumerate(zip(list1, list2)):
if x == y:
list2[i] = y.upper()
print(list2)
输出:
['A', 'p', 'q']
这不是您问题的完整解决方案,更多的是如何进行比较的表示,然后您可以重新使用/修改它以最终实现您想要的解决方案。
import string
from random import choices
def create_random_string(str_len=10):
# k = the number of letters that we want it to return.
return "".join(choices(string.ascii_lowercase, k=str_len))
def compare(str_len=10):
# Create the two strings that we want to compare
first_string = create_random_string(str_len)
second_string = create_random_string(str_len)
# comp_string will hold the final string that we want to return.
comp_string = ""
# Because the length of the strings are based on the variable str_len,
# we can use the range of that number to iterate over our comparisions.
for i in range(str_len):
# Compares the i'th position of the strings
# If they match, then add the uppercase version to comp_string
if first_string[i] == second_string[i]:
comp_string += first_string[i].upper()
else:
comp_string += "-"
return comp_string
for _ in range(10):
print(compare(20))
示例输出:
--------------------
---AS---D---------D-
----W--Q--------E---
--------------------
-----------------E--
------T-------------
--------------------
-------------P------
-----S--------------
--B-----------------
我正在制作一个包含两个列表(在 Python 中)的程序,每个列表包含 5 个不同的字母。我如何才能使我为两个列表选择的任何索引号进行比较,如果条件为真,则将字母大写?如果列表中的前两个值相同(在我的例子中是小写字母),那么我希望第二个列表中的字母变为大写。
example/attempt(我不知道我在做什么):
if list1[0] = list2[0]:
upper(list2[0])
我想你可以使用这样的东西:
def compare_and_upper(lst1, lst2):
for i in range(len(lst1)):
if lst1[i].upper() == lst2[i].upper():
return lst1[i].upper()
return None
如果没有输入和输出的示例,很难理解您的目标是什么,但是如果您的目标是在 list2
中的任何字符串上使用 .upper()
,其中 list1[i]
和list2[i]
相等,可以用zip
和enumerate
的组合来比较,然后把list2[i]
的值赋给大写的字符串,像这样:
list1 = ['a', 'b', 'c']
list2 = ['a', 'p', 'q']
for i, (x, y) in enumerate(zip(list1, list2)):
if x == y:
list2[i] = y.upper()
print(list2)
输出:
['A', 'p', 'q']
这不是您问题的完整解决方案,更多的是如何进行比较的表示,然后您可以重新使用/修改它以最终实现您想要的解决方案。
import string
from random import choices
def create_random_string(str_len=10):
# k = the number of letters that we want it to return.
return "".join(choices(string.ascii_lowercase, k=str_len))
def compare(str_len=10):
# Create the two strings that we want to compare
first_string = create_random_string(str_len)
second_string = create_random_string(str_len)
# comp_string will hold the final string that we want to return.
comp_string = ""
# Because the length of the strings are based on the variable str_len,
# we can use the range of that number to iterate over our comparisions.
for i in range(str_len):
# Compares the i'th position of the strings
# If they match, then add the uppercase version to comp_string
if first_string[i] == second_string[i]:
comp_string += first_string[i].upper()
else:
comp_string += "-"
return comp_string
for _ in range(10):
print(compare(20))
示例输出:
--------------------
---AS---D---------D-
----W--Q--------E---
--------------------
-----------------E--
------T-------------
--------------------
-------------P------
-----S--------------
--B-----------------