如何将包含元素的列表反转为以大写字母开头,后跟小写字母的字符串

2024-09-28 19:07:34 发布

您现在位置:Python中文网/ 问答频道 /正文

def combine_lists(list1, list2):
  # Generate a new list containing the elements of list2
  # Followed by the elements of list1 in reverse order
  list3=list2.extend(list1.reverse())
  return list3


Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]                     
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]

print(combine_lists(Jamies_list, Drews_list))

Tags: ofthenewdefelementsgeneratelistslist
3条回答

看看这是否是您正在寻找的:

>>> Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]
>>> Drews_list = ["Mike", "Carol", "Greg", "Marcia"]
>>>
>>> Drews_list + Jamies_list[::-1]
['Mike', 'Carol', 'Greg', 'Marcia', 'Peter', 'Jan', 'Bobby', 'Cindy', 'Alice']
def combine_lists(list1, list2):
  # Generate a new list containing the elements of list2
  # Followed by the elements of list1 in reverse order
  buf = list2[:] # or list2.copy()
  buf.extend(reversed(list1))
  return buf


Jamies_list = ["Alice", "Cindy", "Bobby", "Jan", "Peter"]                     
Drews_list = ["Mike", "Carol", "Greg", "Marcia"]

print(combine_lists(Jamies_list, Drews_list))

list的extend方法修改当前列表,但不返回任何内容,因此您的表达式:list3=list2.extend(list1.reverse())assign to list3valueNone

关于reverse也一样。它修改当前列表,但返回None。您可以用reverse(list)方法替换它

.reverse.extend都返回None和palce中的突变列表

试试这个

def combine_lists(list1, list2):
  # Generate a new list containing the elements of list2
  # Followed by the elements of list1 in reverse order
  return list1+list2[::-1]

若要反转,请尝试这些reversed(list2)[::-1]中的任何一种方法。这些方法不会在适当的位置改变列表。注意:reversed(list2)给出了一个迭代器

相关问题 更多 >