Python | 用列表的前一半和后一半元素创建两个列表

Given a list, and we have to create two lists from first half elements and second half elements of a list in Python.

给定一个列表,我们必须在Python中从列表的前一半元素和后一半元素创建两个列表。

Example:

例:

    Input:
    list:  [10, 20, 30, 40, 50, 60]

    Output:
    list1: [10, 20, 30]
    list2: [40, 50, 60]

Logic:

逻辑:

  • First take list (Here, we are taking list with 6 elements).

    第一个清单(此处,我们列出了包含6个元素的清单)。

  • To get the elements from/till specified index, use list[n1:n2] notation.

    要从/直到指定索引获取元素,请使用list [n1:n2]表示法。

  • To get first half elements, we are using list[:3], it will return first 3 elements of the list.

    为了获得前半个元素,我们使用list [:3] ,它将返回列表的前三个元素。

  • And, to get second half elements, we are using list[3:], it will return elements after first 3 elements. In this example, we have only 6 elements, so next 3 elements will be returned.

    并且,要获取后半个元素,我们使用list [3:] ,它将在前三个元素之后返回元素。 在此示例中,我们只有6个元素,因此将返回下3个元素。

  • Finally, print the lists.

    最后,打印列表。

Program:

程序:

# define a list 
list = [10, 20, 30, 40, 50, 60]

# Create list1 with half elements (first 3 elements)
list1 = list [:3]
# Create list2 with next half elements (next 3 elements)
list2 = list [3:]

# print list (s)
print "list : ",list
print "list1: ",list1
print "list2: ",list2

Output

输出量

    list :  [10, 20, 30, 40, 50, 60]
    list1:  [10, 20, 30]
    list2:  [40, 50, 60]

使用列表[:3]和列表[3:]的列表[0:3]和列表[3:6] (Using list[0:3] and list[3:6] instaed of list[:3] and list[3:])

We can also use list[0:3] instead of list[:3] to get first 3 elements and list[3:6] instead of list[3:] to get next 3 elements after first 3 elements.

我们还可以使用list [0:3]代替list [:3]获得前3个元素,并使用list [3:6]代替list [3:]获得前3个元素之后的下3个元素。

Consider the program:

考虑该程序:

# define a list 
list = [10, 20, 30, 40, 50, 60]

# Create list1 with half elements (first 3 elements)
list1 = list [0:3]
# Create list2 with next half elements (next 3 elements)
list2 = list [3:6]

# print list (s)
print "list : ",list
print "list1: ",list1
print "list2: ",list2

Output

输出量

    list :  [10, 20, 30, 40, 50, 60]
    list1:  [10, 20, 30]
    list2:  [40, 50, 60]


通过考虑清单的长度 (By considering length of the list)

Let suppose list has n elements, then we can use list[0:n/2] and list[n/2:n].

假设list有n个元素,那么我们可以使用list [0:n / 2]和list [n / 2:n] 。

Consider the program:

考虑该程序:

If there are ODD numbers of elements in the list, program will display message "List has ODD number of elements." And exit.

如果列表中包含奇数个元素,程序将显示消息“列表中包含奇数个元素”。 然后退出。

# define a list 
list = [10, 20, 30, 40, 50, 60]

# get the length of the list 
n = len(list)

# condition to check length is EVEN or not
# if lenght is ODD, show message and exit
if( n%2 != 0 ):
    print "List has ODD number of elements."
    exit()
    
# Create list1 with half elements (first 3 elements)
list1 = list [0:n/2]
# Create list2 with next half elements (next 3 elements)
list2 = list [n/2:n]

# print list (s)
print "list : ",list
print "list1: ",list1
print "list2: ",list2

Output

输出量

    list :  [10, 20, 30, 40, 50, 60]
    list1:  [10, 20, 30]
    list2:  [40, 50, 60]


翻译自: https://www.includehelp.com/python/create-two-lists-with-first-half-and-second-half-elements-of-a-list.aspx