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
0friends = ["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]].
# 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: 10Chạ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
1Mark
2 Mark
3Mark
4Mark
5Mark
6Mark
5Mark
8Mark
5for i, v in enumerate[friends]:
if i == 1:
print[v]
0__25for i, v in enumerate[friends]:
if i == 1:
print[v]
2for i, v in enumerate[friends]:
if i == 1:
print[v]
4Mark
2 for i, v in enumerate[friends]:
if i == 1:
print[v]
6for i, v in enumerate[friends]:
if i == 1:
print[v]
7print[friends[1]]
5for i, v in enumerate[friends]:
if i == 1:
print[v]
9print[friends[1]]
3Mark
1Mark
2Mark
2 Mark
4for i, v in enumerate[friends]:
if i == 1:
print[v]
7print[friends[1]]
5for i, v in enumerate[friends]:
if i == 1:
print[v]
9print[friends[1]]
3Mark
1friends = ["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____53for
friends = ["John", "Mark", "James"]
cnt=0
for friend in friends:
cnt+=1
if cnt==2:
print[friend]
5friends = ["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]
7friends = ["John", "Mark", "James"]
cnt=0
for friend in friends:
cnt+=1
if cnt==2:
print[friend]
8print[friends[1]]
8Second highest number is : 450
Second highest number is : 451
print[friends[1]]
9 Second highest number is : 453
Second highest number is : 454
Mark
2Mark
2 Second highest number is : 457
Second highest number is : 454
for i, v in enumerate[friends]:
if i == 1:
print[v]
4Mark
2 Second largest element is: 451
Second highest number is : 451
Second largest element is: 453
Second largest element is: 454
Second largest element is: 455
Second largest element is: 456
Second highest number is : 454
Second largest element is: 458
Mark
2 Second largest element is: 450
Second highest number is : 454
Mark
2Mark
2 Second largest element is: 451
Second highest number is : 451
Second largest element is: 453
for i, v in enumerate[friends]:
if i == 1:
print[v]
4Mark
222222Second highest number is : 454
for
4Mark
2 Second largest element is: 450
for
7
Mark
2Mark
2 Second largest element is: 451
print[friends[1]]
01friends = ["John", "Mark", "James"]
cnt=0
for friend in friends:
cnt+=1
if cnt==2:
print[friend]
8print[friends[1]]
03print[friends[1]]
04print[friends[1]]
05print[friends[1]]
06print[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]]
29Mark
2 print[friends[1]]
31friends = ["John", "Mark", "James"]
cnt=0
for friend in friends:
cnt+=1
if cnt==2:
print[friend]
8print[friends[1]]
33print[friends[1]]
34print[friends[1]]
35print[friends[1]]
01friends = ["John", "Mark", "James"]
cnt=0
for friend in friends:
cnt+=1
if cnt==2:
print[friend]
8print[friends[1]]
38print[friends[1]]
39print[friends[1]]
40print[friends[1]]
8Mark
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]]
29Mark
2 print[friends[1]]
31friends = ["John", "Mark", "James"]
cnt=0
for friend in friends:
cnt+=1
if cnt==2:
print[friend]
8print[friends[1]]
33print[friends[1]]
34print[friends[1]]
60for i, v in enumerate[friends]:
if i == 1:
print[v]
6print[friends[1]]
62print[friends[1]]
01friends = ["John", "Mark", "James"]
cnt=0
for friend in friends:
cnt+=1
if cnt==2:
print[friend]
8for i, v in enumerate[friends]:
if i == 1:
print[v]
6print[friends[1]]
62Phươ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]]
01friends = ["John", "Mark", "James"]
cnt=0
for friend in friends:
cnt+=1
if cnt==2:
print[friend]
8print[friends[1]]
38Mark
5print[friends[1]]
84print[friends[1]]
85print[friends[1]]
40print[friends[1]]
8Mark
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]]
29Mark
2 print[friends[1]]
31friends = ["John", "Mark", "James"]
cnt=0
for friend in friends:
cnt+=1
if cnt==2:
print[friend]
8print[friends[1]]
33print[friends[1]]
34Phương pháp 3: Bằng cách xóa phần tử tối đa khỏi danh sách & nbsp;
Mark
1Mark
2 Mark
3Mark
4Mark
5Mark
6Mark
5Mark
8Mark
5for i, v in enumerate[friends]:
if i == 1:
print[v]
0__25for i, v in enumerate[friends]:
if i == 1:
print[v]
2print[friends[1]]
56Mark
2 print[friends[1]]
33friends = ["John", "Mark", "James"]
cnt=0
for friend in friends:
cnt+=1
if cnt==2:
print[friend]
3Phươ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 : 454
Mark
20Mark
21print[friends[1]]
89 print[friends[1]]
90Second highest number is : 451
print[friends[1]]
92Mark
2 print[friends[1]]
5print[friends[1]]
01Mark
31Mark
4Mark
5Mark
6Mark
5Mark
8Mark
5for i, v in enumerate[friends]:
if i == 1:
print[v]
0Mark
5for i, v in enumerate[friends]:
if i == 1:
print[v]
2Mark
41Second highest number is : 45
1
96print[friends[1]]
2 Mark
4Mark
99:
Using list comprehensionprint[friends[1]]
Python3
Second highest number is : 451
for
friends = ["John", "Mark", "James"]
cnt=0
for friend in friends:
cnt+=1
if cnt==2:
print[friend]
5friends = ["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]
7friends = ["John", "Mark", "James"]
cnt=0
for friend in friends:
cnt+=1
if cnt==2:
print[friend]
8friends = ["John", "Mark", "James"]
cnt=0
for friend in friends:
cnt+=1
if cnt==2:
print[friend]
22Second highest number is : 454
print[friends[1]]
9 Mark
10Mark
11print[friends[1]]
92Mark
2 Mark
14Mark
11print[friends[1]]
96Mark
2 Mark
18Mark
11print[friends[1]]
92Mark
2 for i, v in enumerate[friends]:
if i == 1:
print[v]
6Mark
26Mark
44print[friends[1]]
01Mark
82Second highest number is : 45
1
28 Mark
29
Using lambda functionMark
Python3
Phương pháp 6: Sử dụng danh sách hiểu biết
print[friends[1]]
89 Mark
43Mark
44Mark
45Mark
2 Mark
47______Mark
44Mark
28 for i, v in enumerate[friends]:
if i == 1:
print[v]
6Mark
59print[friends[1]]
9 Mark
61Mark
222222 Mark
64Mark
21
print[friends[1]]
Mark
Mark
Mark
Mark
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]
33Mark
2for i, v in enumerate[friends]:
if i == 1:
print[v]
6Mark
99print[friends[1]]
89 Mark
43print[friends[1]]
01friends = ["John", "Mark", "James"]
cnt=0
for friend in friends:
cnt+=1
if cnt==2:
print[friend]
8for i, v in enumerate[friends]:
if i == 1:
print[v]
6for i, v in enumerate[friends]:
if i == 1:
print[v]
50