Hướng dẫn how do i print the second number in a list python? - làm cách nào để in số thứ hai trong một danh sách python?

friends = ["John", "Mark", "James"]
for friend in friends:
    print[friend]

Làm thế nào tôi có thể in phần tử thứ hai trong danh sách này bằng cách sử dụng vòng lặp cho? Tôi biết làm thế nào để có được phần tử thứ hai trong danh sách nhưng tôi không biết làm thế nào để có được nó bằng cách sử dụng vòng lặp for.

Hỏi ngày 28 tháng 12 năm 2020 lúc 3:36Dec 28, 2020 at 3:36

2

Bạn chỉ có thể làm:

print[friends[1]]

Điều đó sẽ cung cấp cho phần tử thứ hai, nó sẽ đầu ra:

Mark

Nếu bạn muốn sử dụng một vòng lặp, hãy thử:

for i, v in enumerate[friends]:
    if i == 1:
        print[v]

Output:

Mark

Lập chỉ mục Python bắt đầu từ 0, do đó chỉ mục của phần tử thứ hai sẽ là 1.

Vòng lặp cho tôi đã lặp lại thông qua danh sách, nhưng trình lặp

print[friends[1]]
0 là chỉ mục của mọi yếu tố và
print[friends[1]]
1 là giá trị, vì vậy nó kiểm tra xem ____10 [chỉ mục] là
print[friends[1]]
3, nếu vậy, nó in
print[friends[1]]
1.

Đã trả lời ngày 28 tháng 12 năm 2020 lúc 3:39Dec 28, 2020 at 3:39

U12-ForwardU12-ForwardU12-Forward

66.6K13 Huy hiệu vàng78 Huy hiệu bạc97 Huy hiệu đồng13 gold badges78 silver badges97 bronze badges

Bạn cũng có thể sử dụng một quầy cho những vấn đề như vậy. Tôi đã đặt nó thành

print[friends[1]]
5 và cho mỗi lần lặp, nó được tăng lên bởi
print[friends[1]]
3. Vì vậy, để truy cập phần tử thứ 2
print[friends[1]]
7 sẽ là
print[friends[1]]
8. Bạn có thể thêm câu lệnh
print[friends[1]]
9 và chỉ in
Mark
0

friends = ["John", "Mark", "James"]
cnt=0
for friend in friends:
    cnt+=1
    if cnt==2:
         print[friend]

Đã trả lời ngày 28 tháng 12 năm 2020 lúc 3:57Dec 28, 2020 at 3:57

a121a121a121

8054 Huy hiệu vàng8 Huy hiệu bạc18 Huy hiệu đồng4 gold badges8 silver badges18 bronze badges

0

# sắp xếp danh sách ..

#display phần tử cuối cùng thứ hai của danh sách ..

In ["Phần tử lớn thứ hai của danh sách là:", list_val [-2]].

  • Làm cách nào để in số nhỏ thứ hai trong Python?
  • Phương pháp 1:.
  • # sắp xếp danh sách ..

    #display phần tử cuối cùng thứ hai của danh sách ..

    In ["Phần tử lớn thứ hai của danh sách là:", list_val [-2]].

    Làm cách nào để in số nhỏ thứ hai trong Python?

    Phương pháp 1:. 

    Lấy một biến nói trước và đặt nó thành giá trị tối đa số nguyên .. list1 = [10, 20, 4]
    Output: 10

    Chạy một vòng cho phạm vi [0, LEN [ARR]]. list2 = [70, 11, 20, 4, 100]
    Output: 70

    Kiểm tra xem đầu tiên> mảng [i], đặt trước = mảng [i]. Sorting is an easier but less optimal method. Given below is an O[n] algorithm to do the same. 

    Python3

    Mark
    
    1
    Mark
    
    2
    Mark
    
    3
    Mark
    
    4
    Mark
    
    5
    Mark
    
    6
    Mark
    
    5
    Mark
    
    8
    Mark
    
    5
    for i, v in enumerate[friends]:
        if i == 1:
            print[v]
    
    0__25
    for i, v in enumerate[friends]:
        if i == 1:
            print[v]
    
    2

    for i, v in enumerate[friends]:
        if i == 1:
            print[v]
    
    4
    Mark
    
    2
    for i, v in enumerate[friends]:
        if i == 1:
            print[v]
    
    6
    for i, v in enumerate[friends]:
        if i == 1:
            print[v]
    
    7
    print[friends[1]]
    
    5
    for i, v in enumerate[friends]:
        if i == 1:
            print[v]
    
    9
    print[friends[1]]
    
    3
    Mark
    
    1

    Mark
    
    2
    Mark
    
    2
    Mark
    
    4
    for i, v in enumerate[friends]:
        if i == 1:
            print[v]
    
    7
    print[friends[1]]
    
    5
    for i, v in enumerate[friends]:
        if i == 1:
            print[v]
    
    9
    print[friends[1]]
    
    3
    Mark
    
    1

    friends = ["John", "Mark", "James"]
    cnt=0
    for friend in friends:
        cnt+=1
        if cnt==2:
             print[friend]
    
    0____22
    friends = ["John", "Mark", "James"]
    cnt=0
    for friend in friends:
        cnt+=1
        if cnt==2:
             print[friend]
    
    22____53

    for

    friends = ["John", "Mark", "James"]
    cnt=0
    for friend in friends:
        cnt+=1
        if cnt==2:
             print[friend]
    
    5
    friends = ["John", "Mark", "James"]
    cnt=0
    for friend in friends:
        cnt+=1
        if cnt==2:
             print[friend]
    
    6
    friends = ["John", "Mark", "James"]
    cnt=0
    for friend in friends:
        cnt+=1
        if cnt==2:
             print[friend]
    
    7
    friends = ["John", "Mark", "James"]
    cnt=0
    for friend in friends:
        cnt+=1
        if cnt==2:
             print[friend]
    
    8
    print[friends[1]]
    
    8
    Second highest number is :  45
    0

    Second highest number is :  45
    1
    print[friends[1]]
    
    9
    Second highest number is :  45
    3

    Second highest number is :  45
    4
    Mark
    
    2
    Mark
    
    2
    Second highest number is :  45
    7

    Second highest number is :  45
    4
    for i, v in enumerate[friends]:
        if i == 1:
            print[v]
    
    4
    Mark
    
    2
    Second largest element is: 45
    1

    Second highest number is :  45
    1
    Second largest element is: 45
    3
    Second largest element is: 45
    4
    Second largest element is: 45
    5
    Second largest element is: 45
    6

    Second highest number is :  45
    4
    Second largest element is: 45
    8
    Mark
    
    2
    Second largest element is: 45
    0

    Second highest number is :  45
    4
    Mark
    
    2
    Mark
    
    2
    Second largest element is: 45
    1

    Second highest number is :  45
    1
    Second largest element is: 45
    3
    for i, v in enumerate[friends]:
        if i == 1:
            print[v]
    
    4
    Mark
    
    222222

    Second highest number is :  45
    4for4
    Mark
    
    2
    Second largest element is: 45
    0

    for7

    Mark
    
    2
    Mark
    
    2
    Second largest element is: 45
    1

    print[friends[1]]
    
    01
    friends = ["John", "Mark", "James"]
    cnt=0
    for friend in friends:
        cnt+=1
        if cnt==2:
             print[friend]
    
    8
    print[friends[1]]
    
    03
    print[friends[1]]
    
    04

    print[friends[1]]
    
    05
    print[friends[1]]
    
    06
    print[friends[1]]
    
    07

    Đầu ra

    Second highest number is :  45

    Phương pháp 2: Sắp xếp danh sách theo thứ tự tăng dần và in phần tử cuối cùng thứ hai trong danh sách. Sort the list in ascending order and print the second last element in the list.

    Python3

    Các

    print[friends[1]]
    
    29
    Mark
    
    2
    print[friends[1]]
    
    31
    friends = ["John", "Mark", "James"]
    cnt=0
    for friend in friends:
        cnt+=1
        if cnt==2:
             print[friend]
    
    8
    print[friends[1]]
    
    33
    print[friends[1]]
    
    34

    print[friends[1]]
    
    35

    print[friends[1]]
    
    01
    friends = ["John", "Mark", "James"]
    cnt=0
    for friend in friends:
        cnt+=1
        if cnt==2:
             print[friend]
    
    8
    print[friends[1]]
    
    38
    print[friends[1]]
    
    39
    print[friends[1]]
    
    40
    print[friends[1]]
    
    8
    Mark
    
    1

    Đầu ra

    Second largest element is: 45

    Phương pháp 2: Sắp xếp danh sách theo thứ tự tăng dần và in phần tử cuối cùng thứ hai trong danh sách. By removing the max element from the list 

    Python3

    Các

    print[friends[1]]
    
    29
    Mark
    
    2
    print[friends[1]]
    
    31
    friends = ["John", "Mark", "James"]
    cnt=0
    for friend in friends:
        cnt+=1
        if cnt==2:
             print[friend]
    
    8
    print[friends[1]]
    
    33
    print[friends[1]]
    
    34

    print[friends[1]]
    
    60
    for i, v in enumerate[friends]:
        if i == 1:
            print[v]
    
    6
    print[friends[1]]
    
    62

    print[friends[1]]
    
    01
    friends = ["John", "Mark", "James"]
    cnt=0
    for friend in friends:
        cnt+=1
        if cnt==2:
             print[friend]
    
    8
    for i, v in enumerate[friends]:
        if i == 1:
            print[v]
    
    6
    print[friends[1]]
    
    62

    Phương pháp 3: Bằng cách xóa phần tử tối đa khỏi danh sách & nbsp; Find the max list element on inputs provided by the user 

    Python3

    Các

    print[friends[1]]
    
    01
    friends = ["John", "Mark", "James"]
    cnt=0
    for friend in friends:
        cnt+=1
        if cnt==2:
             print[friend]
    
    8
    print[friends[1]]
    
    38
    Mark
    
    5
    print[friends[1]]
    
    84
    print[friends[1]]
    
    85
    print[friends[1]]
    
    40
    print[friends[1]]
    
    8
    Mark
    
    1

    Đầu ra

    Second largest element is: 45

    Phương pháp 2: Sắp xếp danh sách theo thứ tự tăng dần và in phần tử cuối cùng thứ hai trong danh sách. Traverse once to find the largest and then once again to find the second largest. 

    Python3

    Các

    print[friends[1]]
    
    29
    Mark
    
    2
    print[friends[1]]
    
    31
    friends = ["John", "Mark", "James"]
    cnt=0
    for friend in friends:
        cnt+=1
        if cnt==2:
             print[friend]
    
    8
    print[friends[1]]
    
    33
    print[friends[1]]
    
    34

    Phương pháp 3: Bằng cách xóa phần tử tối đa khỏi danh sách & nbsp;

    Mark
    
    1
    Mark
    
    2
    Mark
    
    3
    Mark
    
    4
    Mark
    
    5
    Mark
    
    6
    Mark
    
    5
    Mark
    
    8
    Mark
    
    5
    for i, v in enumerate[friends]:
        if i == 1:
            print[v]
    
    0__25
    for i, v in enumerate[friends]:
        if i == 1:
            print[v]
    
    2

    print[friends[1]]
    
    56
    Mark
    
    2
    print[friends[1]]
    
    33
    friends = ["John", "Mark", "James"]
    cnt=0
    for friend in friends:
        cnt+=1
        if cnt==2:
             print[friend]
    
    3

    Phương pháp 4: Tìm phần tử danh sách tối đa trên các đầu vào được cung cấp bởi người dùng & NBSP;

    Phương pháp 5: Traverse một lần để tìm cái lớn nhất và sau đó một lần nữa để tìm thấy lớn thứ hai. & NBSP;

    Second highest number is :  45
    4
    Mark
    
    20
    Mark
    
    21

    print[friends[1]]
    
    89
    print[friends[1]]
    
    90

    Second highest number is :  45
    1
    print[friends[1]]
    
    92
    Mark
    
    2
    print[friends[1]]
    
    5

    print[friends[1]]
    
    01
    Mark
    
    31
    Mark
    
    4
    Mark
    
    5
    Mark
    
    6
    Mark
    
    5
    Mark
    
    8
    Mark
    
    5
    for i, v in enumerate[friends]:
        if i == 1:
            print[v]
    
    0
    Mark
    
    5
    for i, v in enumerate[friends]:
        if i == 1:
            print[v]
    
    2
    Mark
    
    41

    Second highest number is :  45
    1
    print[friends[1]]
    
    96
    Mark
    
    2
    Mark
    
    4
    print[friends[1]]
    
    99
    : Using list comprehension

    Python3

    Second highest number is :  45
    1for
    friends = ["John", "Mark", "James"]
    cnt=0
    for friend in friends:
        cnt+=1
        if cnt==2:
             print[friend]
    
    5
    friends = ["John", "Mark", "James"]
    cnt=0
    for friend in friends:
        cnt+=1
        if cnt==2:
             print[friend]
    
    6
    friends = ["John", "Mark", "James"]
    cnt=0
    for friend in friends:
        cnt+=1
        if cnt==2:
             print[friend]
    
    7
    friends = ["John", "Mark", "James"]
    cnt=0
    for friend in friends:
        cnt+=1
        if cnt==2:
             print[friend]
    
    8
    friends = ["John", "Mark", "James"]
    cnt=0
    for friend in friends:
        cnt+=1
        if cnt==2:
             print[friend]
    
    22

    Second highest number is :  45
    4
    print[friends[1]]
    
    9
    Mark
    
    10

    Mark
    
    11
    print[friends[1]]
    
    92
    Mark
    
    2
    Mark
    
    14

    Mark
    
    11
    print[friends[1]]
    
    96
    Mark
    
    2
    Mark
    
    18

    Mark
    
    11
    print[friends[1]]
    
    92
    Mark
    
    2
    for i, v in enumerate[friends]:
        if i == 1:
            print[v]
    
    6
    Mark
    
    26

    Mark
    
    44
    print[friends[1]]
    
    01
    Mark
    
    82

    Second highest number is :  45
    1
    Mark
    
    28
    Mark
    
    29
    Using lambda function

    Python3

    Phương pháp 6: Sử dụng danh sách hiểu biết

    print[friends[1]]
    
    89
    Mark
    
    43

    Mark
    
    44
    Mark
    
    45
    Mark
    
    2
    Mark
    
    47______

    Mark
    
    44
    Mark
    
    28
    for i, v in enumerate[friends]:
        if i == 1:
            print[v]
    
    6
    Mark
    
    59

    print[friends[1]]
    
    9
    Mark
    
    61
    Mark
    
    222222
    Mark
    
    64
    Mark
    
    21

    Python3

    Phương pháp 6: Sử dụng danh sách hiểu biết

    for i, v in enumerate[friends]:
        if i == 1:
            print[v]
    
    33
    Mark
    
    2
    for i, v in enumerate[friends]:
        if i == 1:
            print[v]
    
    6
    Mark
    
    99

    print[friends[1]]
    
    89
    Mark
    
    43

    print[friends[1]]
    
    01
    friends = ["John", "Mark", "James"]
    cnt=0
    for friend in friends:
        cnt+=1
        if cnt==2:
             print[friend]
    
    8
    for i, v in enumerate[friends]:
        if i == 1:
            print[v]
    
    6
    for i, v in enumerate[friends]:
        if i == 1:
            print[v]
    
    50


    Bây giờ, khai báo một biến nói thứ hai và đặt nó thành giá trị tối đa số nguyên ..

    Kiểm tra nếu [mảng [i]! = ....use -2 as index.

    Bây giờ, khai báo một biến nói thứ hai và đặt nó thành giá trị tối đa số nguyên ..

    Kiểm tra nếu [mảng [i]! = ...., e.g. print[my_list[1:3]] . The print[] function will print the slice of the list starting at the specified index and going up to, but not including the stop index.

    Làm thế nào để bạn in giá trị cao thứ hai trong danh sách trong Python?

    Thí dụ -..
    #program để tìm số lượng lớn thứ hai của danh sách ..
    # Tuyên bố danh sách ..
    list_val = [20, 30, 40, 25, 10].
    # sắp xếp danh sách ..
    list_val.sort[].
    #display phần tử cuối cùng thứ hai của danh sách ..
    In ["Phần tử lớn thứ hai của danh sách là:", list_val [-2]].

    Làm cách nào để in số nhỏ thứ hai trong Python?

    Phương pháp 1:..
    Lấy một biến nói trước và đặt nó thành giá trị tối đa số nguyên ..
    Chạy một vòng cho phạm vi [0, LEN [ARR]].
    Kiểm tra xem đầu tiên> mảng [i], đặt trước = mảng [i].
    Bây giờ, khai báo một biến nói thứ hai và đặt nó thành giá trị tối đa số nguyên ..
    Chạy một vòng cho phạm vi [0, LEN [ARR]].
    Kiểm tra nếu [mảng [i]! = ....
    Print[second].

    Bài Viết Liên Quan

    Chủ Đề