Hướng dẫn how do you select multiple items from a list in python? - làm cách nào để bạn chọn nhiều mục từ một danh sách trong python?

list( myBigList[i] for i in [87, 342, 217, 998, 500] )

Tôi đã so sánh các câu trả lời với Python 2.5.2:

  • 19.7 USEC:

    >>> import numpy
    >>> myBigList = numpy.array(range(1000))
    >>> myBigList[(87, 342, 217, 998, 500)]
    Traceback (most recent call last):
      File "", line 1, in 
    IndexError: invalid index
    >>> myBigList[[87, 342, 217, 998, 500]]
    array([ 87, 342, 217, 998, 500])
    >>> myBigList[numpy.array([87, 342, 217, 998, 500])]
    array([ 87, 342, 217, 998, 500])
    
    1

  • 20,6 USEC:

    >>> import numpy
    >>> myBigList = numpy.array(range(1000))
    >>> myBigList[(87, 342, 217, 998, 500)]
    Traceback (most recent call last):
      File "", line 1, in 
    IndexError: invalid index
    >>> myBigList[[87, 342, 217, 998, 500]]
    array([ 87, 342, 217, 998, 500])
    >>> myBigList[numpy.array([87, 342, 217, 998, 500])]
    array([ 87, 342, 217, 998, 500])
    
    2

  • 22.7 USEC:

    >>> import numpy
    >>> myBigList = numpy.array(range(1000))
    >>> myBigList[(87, 342, 217, 998, 500)]
    Traceback (most recent call last):
      File "", line 1, in 
    IndexError: invalid index
    >>> myBigList[[87, 342, 217, 998, 500]]
    array([ 87, 342, 217, 998, 500])
    >>> myBigList[numpy.array([87, 342, 217, 998, 500])]
    array([ 87, 342, 217, 998, 500])
    
    3

  • 24.6 USEC:

    >>> import numpy
    >>> myBigList = numpy.array(range(1000))
    >>> myBigList[(87, 342, 217, 998, 500)]
    Traceback (most recent call last):
      File "", line 1, in 
    IndexError: invalid index
    >>> myBigList[[87, 342, 217, 998, 500]]
    array([ 87, 342, 217, 998, 500])
    >>> myBigList[numpy.array([87, 342, 217, 998, 500])]
    array([ 87, 342, 217, 998, 500])
    
    4

Lưu ý rằng trong Python 3, số 1 được thay đổi giống như thứ 4.


Một tùy chọn khác là bắt đầu với

>>> import numpy
>>> myBigList = numpy.array(range(1000))
>>> myBigList[(87, 342, 217, 998, 500)]
Traceback (most recent call last):
  File "", line 1, in 
IndexError: invalid index
>>> myBigList[[87, 342, 217, 998, 500]]
array([ 87, 342, 217, 998, 500])
>>> myBigList[numpy.array([87, 342, 217, 998, 500])]
array([ 87, 342, 217, 998, 500])
5 cho phép lập chỉ mục thông qua danh sách hoặc
>>> import numpy
>>> myBigList = numpy.array(range(1000))
>>> myBigList[(87, 342, 217, 998, 500)]
Traceback (most recent call last):
  File "", line 1, in 
IndexError: invalid index
>>> myBigList[[87, 342, 217, 998, 500]]
array([ 87, 342, 217, 998, 500])
>>> myBigList[numpy.array([87, 342, 217, 998, 500])]
array([ 87, 342, 217, 998, 500])
5:

>>> import numpy
>>> myBigList = numpy.array(range(1000))
>>> myBigList[(87, 342, 217, 998, 500)]
Traceback (most recent call last):
  File "", line 1, in 
IndexError: invalid index
>>> myBigList[[87, 342, 217, 998, 500]]
array([ 87, 342, 217, 998, 500])
>>> myBigList[numpy.array([87, 342, 217, 998, 500])]
array([ 87, 342, 217, 998, 500])

>>> import numpy
>>> myBigList = numpy.array(range(1000))
>>> myBigList[(87, 342, 217, 998, 500)]
Traceback (most recent call last):
  File "", line 1, in 
IndexError: invalid index
>>> myBigList[[87, 342, 217, 998, 500]]
array([ 87, 342, 217, 998, 500])
>>> myBigList[numpy.array([87, 342, 217, 998, 500])]
array([ 87, 342, 217, 998, 500])
7 không hoạt động giống như đó là những lát cắt.

Tạo một danh sách và có thể chọn các mục thông qua danh sách là rất quan trọng. Chọn các mặt hàng rất dễ dàng và đơn giản, vì vậy hãy bắt đầu.

Tôi có danh sách ví dụ của tôi ở đây.

Nếu tôi muốn rút ra mục đầu tiên, Apple, tôi gọi biến của tôi, list1, sử dụng dấu ngoặc vuông và nhập số không.

Nếu tôi muốn chọn mục thứ hai trong danh sách, hãy nhập một cái thay vì số 0 trong ngoặc.

Thực tiễn tốt nhất để chọn mục cuối cùng là sử dụng một mục tiêu cực. Điều này là do bạn có thể thêm/đưa các mục vào danh sách của mình, vì vậy sử dụng một mục tiêu cực sẽ luôn cung cấp mục cuối cùng.

Bạn cũng có thể chọn nhiều mục trong một danh sách. Trong ví dụ này, chúng tôi kêu gọi biến của chúng tôi, List1 và các mục từ 0 đến hai. Python chỉ kéo các mục thứ nhất và thứ hai. Bán kết của dấu ngoặc nói với Python để kéo các mục trong danh sách từ số được ghi nhận đầu tiên (không) đến số cuối cùng nhưng miễn trừ nó (ba). Vì vậy, chúng tôi thấy bên dưới, Python kéo mục đầu tiên trong danh sách và thứ hai, nhưng không phải là mục thứ ba.

Thực hành thử nghiệm với các kỹ thuật này và cam kết nó vào bộ nhớ. Cách tốt nhất để nhớ làm thế nào để làm điều này là tiếp tục thực hành!

Các hoạt động cắt trong danh sách được sử dụng để tập hợp nhiều phần tử từ một danh sách. Cú pháp cắt danh sách như sau:

list[start:end]

Hãy nhớ rằng, cú pháp này chỉ ra tập hợp tất cả các phần tử từ

>>> import numpy
>>> myBigList = numpy.array(range(1000))
>>> myBigList[(87, 342, 217, 998, 500)]
Traceback (most recent call last):
  File "", line 1, in 
IndexError: invalid index
>>> myBigList[[87, 342, 217, 998, 500]]
array([ 87, 342, 217, 998, 500])
>>> myBigList[numpy.array([87, 342, 217, 998, 500])]
array([ 87, 342, 217, 998, 500])
8 và lên đến nhưng không bao gồm phần tử
>>> import numpy
>>> myBigList = numpy.array(range(1000))
>>> myBigList[(87, 342, 217, 998, 500)]
Traceback (most recent call last):
  File "", line 1, in 
IndexError: invalid index
>>> myBigList[[87, 342, 217, 998, 500]]
array([ 87, 342, 217, 998, 500])
>>> myBigList[numpy.array([87, 342, 217, 998, 500])]
array([ 87, 342, 217, 998, 500])
9.from the
>>> import numpy
>>> myBigList = numpy.array(range(1000))
>>> myBigList[(87, 342, 217, 998, 500)]
Traceback (most recent call last):
  File "", line 1, in 
IndexError: invalid index
>>> myBigList[[87, 342, 217, 998, 500]]
array([ 87, 342, 217, 998, 500])
>>> myBigList[numpy.array([87, 342, 217, 998, 500])]
array([ 87, 342, 217, 998, 500])
8 and up to but not including the
>>> import numpy
>>> myBigList = numpy.array(range(1000))
>>> myBigList[(87, 342, 217, 998, 500)]
Traceback (most recent call last):
  File "", line 1, in 
IndexError: invalid index
>>> myBigList[[87, 342, 217, 998, 500]]
array([ 87, 342, 217, 998, 500])
>>> myBigList[numpy.array([87, 342, 217, 998, 500])]
array([ 87, 342, 217, 998, 500])
9 element.

Ngoài ra, bạn có thể sử dụng cắt mở rộng để chọn hiệu quả nhiều yếu tố từ danh sách. Ví dụ: lệnh sau trả về tất cả các phần tử từ danh sách ngoại trừ phần đầu tiên (tại chỉ mục 0):

x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]

Tương tự, lệnh sau trả về tất cả các yếu tố từ danh sách ngoại trừ hai phần cuối:

x[:-2]

[1, 2, 3]

Đưa ra một danh sách các số, hãy viết một chương trình Python để xóa nhiều yếu tố khỏi danh sách dựa trên điều kiện đã cho.

Example:  

Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10]

Input: [11, 5, 17, 18, 23, 50]
Output: Remove = [1:5], New_list = [11, 50]

Nhiều yếu tố có thể bị xóa khỏi danh sách trong Python, dựa trên kiến ​​thức chúng ta có về dữ liệu. Giống như, chúng ta chỉ biết các giá trị sẽ bị xóa hoặc cũng biết các chỉ mục của các giá trị đó. Hãy cùng xem các ví dụ khác nhau dựa trên một kịch bản khác.

Ví dụ #1: Hãy nói rằng chúng tôi muốn xóa từng phần tử trong danh sách chia hết cho 2 hoặc tất cả các số chẵn. & NBSP; Let’s say we want to delete each element in the list which is divisible by 2 or all the even numbers. 

Python3

list[start:end]
0
list[start:end]
1
list[start:end]
2
list[start:end]
3
list[start:end]
4
list[start:end]
5
list[start:end]
4
list[start:end]
7
list[start:end]
4
list[start:end]
9
list[start:end]
4
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
1
list[start:end]
4
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
3
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
4

x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
5
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
6
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
7
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
8

x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
9
x[:-2]

[1, 2, 3]
0
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
6
x[:-2]

[1, 2, 3]
2
x[:-2]

[1, 2, 3]
3
list[start:end]
1
list[start:end]
1
x[:-2]

[1, 2, 3]
6
x[:-2]

[1, 2, 3]
7

x[:-2]

[1, 2, 3]
8
x[:-2]

[1, 2, 3]
9

Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10]

Input: [11, 5, 17, 18, 23, 50]
Output: Remove = [1:5], New_list = [11, 50]
0
Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10]

Input: [11, 5, 17, 18, 23, 50]
Output: Remove = [1:5], New_list = [11, 50]
1
Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10]

Input: [11, 5, 17, 18, 23, 50]
Output: Remove = [1:5], New_list = [11, 50]
2
Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10]

Input: [11, 5, 17, 18, 23, 50]
Output: Remove = [1:5], New_list = [11, 50]
3

Output:  

New list after removing all even numbers:  [11, 5, 17, 23]

Ví dụ #2: Sử dụng danh sách hiểu Using list comprehension

Loại bỏ tất cả các yếu tố thậm chí trong danh sách cũng tốt như chỉ bao gồm tất cả các yếu tố không chẵn (nghĩa là các yếu tố lẻ). & NBSP;

Python3

list[start:end]
0
list[start:end]
1
list[start:end]
2
list[start:end]
3
list[start:end]
4
list[start:end]
5
list[start:end]
4
list[start:end]
7
list[start:end]
4
list[start:end]
9
list[start:end]
4
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
1
list[start:end]
4
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
3
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
4

x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
5
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
6
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
7
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
8

Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10]

Input: [11, 5, 17, 18, 23, 50]
Output: Remove = [1:5], New_list = [11, 50]
0
Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10]

Input: [11, 5, 17, 18, 23, 50]
Output: Remove = [1:5], New_list = [11, 50]
1
11 50
6
11 50
7

Output:  

11 5 17 23

x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
9
x[:-2]

[1, 2, 3]
0
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
6
x[:-2]

[1, 2, 3]
2
x[:-2]

[1, 2, 3]
3
list[start:end]
1
list[start:end]
1
x[:-2]

[1, 2, 3]
6
x[:-2]

[1, 2, 3]
7
Remove adjacent elements using list slicing

New list after removing all even numbers:  [11, 5, 17, 23]

Python3

list[start:end]
0
list[start:end]
1
list[start:end]
2
list[start:end]
3
list[start:end]
4
list[start:end]
5
list[start:end]
4
list[start:end]
7
list[start:end]
4
list[start:end]
9
list[start:end]
4
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
1
list[start:end]
4
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
3
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
4

x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
5
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
6
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
7
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
8

x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
9
x[:-2]

[1, 2, 3]
0
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
6
x[:-2]

[1, 2, 3]
2
x[:-2]

[1, 2, 3]
3
list[start:end]
1
list[start:end]
1
x[:-2]

[1, 2, 3]
6
x[:-2]

[1, 2, 3]
7

Output:  

11 50

New list after removing all even numbers:  [11, 5, 17, 23]
Using list comprehension

Ví dụ #2: Sử dụng danh sách hiểu

Python3

list[start:end]
0
list[start:end]
1
list[start:end]
2
list[start:end]
3
list[start:end]
4
list[start:end]
5
list[start:end]
4
list[start:end]
7
list[start:end]
4
list[start:end]
9
list[start:end]
4
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
1
list[start:end]
4
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
3
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
4

x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
5
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
6
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
7
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
8

x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
9
x[:-2]

[1, 2, 3]
0
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
6
x[:-2]

[1, 2, 3]
2
x[:-2]

[1, 2, 3]
3
list[start:end]
1
list[start:end]
1
x[:-2]

[1, 2, 3]
6
x[:-2]

[1, 2, 3]
7

Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10]

Input: [11, 5, 17, 18, 23, 50]
Output: Remove = [1:5], New_list = [11, 50]
0
Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10]

Input: [11, 5, 17, 18, 23, 50]
Output: Remove = [1:5], New_list = [11, 50]
1
>>> import numpy
>>> myBigList = numpy.array(range(1000))
>>> myBigList[(87, 342, 217, 998, 500)]
Traceback (most recent call last):
  File "", line 1, in 
IndexError: invalid index
>>> myBigList[[87, 342, 217, 998, 500]]
array([ 87, 342, 217, 998, 500])
>>> myBigList[numpy.array([87, 342, 217, 998, 500])]
array([ 87, 342, 217, 998, 500])
49
Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10]

Input: [11, 5, 17, 18, 23, 50]
Output: Remove = [1:5], New_list = [11, 50]
3

Output:  

New list after removing unwanted numbers:  [17, 18, 23, 50]

New list after removing all even numbers:  [11, 5, 17, 23]
When index of elements is known.
Though indexes of elements in known, deleting the elements randomly will change the values of indexes. Hence, it is always recommended to delete the largest indices first. Using this strategy, index of smaller values will not be changed. We can sort the list in reverse order and delete the elements of list in descending order.

Python3

list[start:end]
0
list[start:end]
1
list[start:end]
2
list[start:end]
3
list[start:end]
4
list[start:end]
5
list[start:end]
4
list[start:end]
7
list[start:end]
4
list[start:end]
9
list[start:end]
4
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
1
list[start:end]
4
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
3
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
4

x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
5
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
6
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
7
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
8

x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
9
x[:-2]

[1, 2, 3]
0
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
6
x[:-2]

[1, 2, 3]
2
x[:-2]

[1, 2, 3]
3
list[start:end]
1
list[start:end]
1
x[:-2]

[1, 2, 3]
6
x[:-2]

[1, 2, 3]
7

New list after removing all even numbers:  [11, 5, 17, 23]

Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10]

Input: [11, 5, 17, 18, 23, 50]
Output: Remove = [1:5], New_list = [11, 50]
0
Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10]

Input: [11, 5, 17, 18, 23, 50]
Output: Remove = [1:5], New_list = [11, 50]
1
11 50
6
11 50
7

Output:  

>>> import numpy
>>> myBigList = numpy.array(range(1000))
>>> myBigList[(87, 342, 217, 998, 500)]
Traceback (most recent call last):
  File "", line 1, in 
IndexError: invalid index
>>> myBigList[[87, 342, 217, 998, 500]]
array([ 87, 342, 217, 998, 500])
>>> myBigList[numpy.array([87, 342, 217, 998, 500])]
array([ 87, 342, 217, 998, 500])
0

Phương pháp: Sử dụng chức năng liệt kê

Python3

list[start:end]
0
list[start:end]
1
list[start:end]
2
list[start:end]
3
list[start:end]
4
list[start:end]
5
list[start:end]
4
list[start:end]
7
list[start:end]
4
list[start:end]
9
list[start:end]
4
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
1
list[start:end]
4
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
3
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
4

list[start:end]
0
list[start:end]
1
11 5 17 23
1
x = [1, 2, 3, 4, 5]
x[1:]

[2, 3, 4, 5]
5
list[start:end]
09______37

Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10]

Input: [11, 5, 17, 18, 23, 50]
Output: Remove = [1:5], New_list = [11, 50]
0
list[start:end]
22

Độ phức tạp về thời gian: O (nlogn)O(nlogn)

Không gian phụ trợ: O (1) O(1)


Làm cách nào để chọn nhiều mục từ một danh sách trong Python?

Sử dụng Random.sample () để chọn ngẫu nhiên nhiều mục từ danh sách. Gọi ngẫu nhiên. Mẫu (dân số, k) để trả lại một danh sách các mặt hàng được chọn ngẫu nhiên từ dân số danh sách. sample() to randomly select multiple items from a list. Call random. sample(population, k) to return a list of k randomly selected items from a list population .

Làm thế nào để bạn chọn dữ liệu từ một danh sách trong Python?

Để chọn các phần tử từ danh sách Python, chúng tôi sẽ sử dụng Dist.Append ().Chúng tôi sẽ tạo một danh sách các chỉ số được truy cập và vòng lặp được sử dụng để lặp lại thông qua danh sách chỉ mục này để truy cập vào phần tử được chỉ định.Và sau đó chúng tôi thêm các yếu tố này vào danh sách mới bằng cách sử dụng một chỉ mục.list. append(). We will create a list of indices to be accessed and the loop is used to iterate through this index list to access the specified element. And then we add these elements to the new list using an index.

Làm thế nào để bạn cắt một danh sách trong Python?

Làm thế nào để cắt một danh sách trong Python..
Xác định danh sách bạn muốn cắt lát.Lấy danh sách bạn đang cắt vào những người con hoặc các ô mở rộng.....
Nhập các chỉ mục "Bắt đầu" và "Dừng".....
Thêm một giá trị "bước" tích cực.....
Thêm giá trị "bước" âm ..

Làm cách nào để chọn ngẫu nhiên một mục từ danh sách trong Python?

Chọn các phần tử n ngẫu nhiên từ danh sách bằng cách sử dụng phương thức lựa chọn () được sử dụng để trả về một số ngẫu nhiên từ chuỗi đã cho.Trình tự có thể là một danh sách hoặc một tuple.using choice() The choice() method is used to return a random number from given sequence. The sequence can be a list or a tuple.