list1= ['ALEX', 'George', 'shyam']
list2= ['George','Ram', 'shyam']
output=['Alex', 'George','Shyam','Ram']
我试过喜欢它。
list1=['ALEX', 'George', 'shyam']
list2=['George','Ram', 'shyam']
output=list1
for j in list2:
for i in list1:
if str(j)!=str(i):
output=output+[j]
print(output)
但是,这是错误的。请帮助我知道如何处理上述问题!我是编程的菜鸟。提前致谢。
答案 0 :(得分:1)
list1=['ALEX', 'George', 'shyam']
list2=['George','Ram', 'shyam']
res = list1.copy()
for el in list2:
if el not in res:
res.append(el)
print(res)
答案 1 :(得分:0)
要合并两个列表,请使用:
list1 = ['ALEX', 'George', 'shyam']
list2 = ['George','Ram', 'shyam']
resultList= list(set(list1 ) | set(list2))
print(resultList)
输出:
['shyam', 'George', 'ALEX', 'Ram']
答案 2 :(得分:0)
集合不能有重复项。因此,将列表转换为集合并返回到列表可以消除所有重复项。因此,添加您的列表并将较大的列表转换为一组:
list1= ['ALEX', 'George', 'shyam']
list2= ['George','Ram', 'shyam']
result = set(list1 + list2)
print(list(result))
#prints ['Ram', 'George', 'ALEX', 'shyam']
答案 3 :(得分:0)
这将给出与问题中所述相同的顺序
list1= ['ALEX', 'George', 'shyam']
list2= ['George','Ram', 'shyam']
#output=['Alex', 'George','Shyam','Ram']
output=[x.capitalize() for x in list1] # Copy list but capitalize each string e.g. ALEX -> Alex
for name in list2: # iterate through list2
if name.capitalize() not in output: # if it is not already in output, append it
output.append(name)
print(output)