1
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Tôi có một danh sách python như:
['', ''...]
Và tôi chỉ muốn trích xuất trực tiếp các chuỗi sau @ vào một danh sách khác, chẳng hạn như:
mylist = ['gmail.com', 'hotmail.com'...]
Có thể không? Split [] dường như không làm việc với các danh sách.
Đây là lần thử của tôi:
for x in range[len[mylist]]:
mylist[x].split["@",1][1]
Nhưng nó không cho tôi một danh sách đầu ra.
Đã hỏi ngày 8 tháng 6 năm 2017 lúc 22:17Jun 8, 2017 at 22:17
MadnomadnoMadno
8522 Huy hiệu vàng10 Huy hiệu bạc26 Huy hiệu đồng2 gold badges10 silver badges26 bronze badges
9
Bạn gần gũi, hãy thử những điều chỉnh nhỏ này:
Danh sách là Iterables, có nghĩa là nó dễ sử dụng cho vòng lặp hơn bạn nghĩ:
for x in mylist:
#do something
Bây giờ, điều bạn muốn làm là 1] Tách
mylist = ['gmail.com', 'hotmail.com'...]
0 tại mylist = ['gmail.com', 'hotmail.com'...]
1 và 2] Thêm kết quả vào danh sách khác.#In order to add to another list you need to make another list
newlist = []
for x in mylist:
split_results = x.split['@']
# Now you have a tuple of the results of your split
# add the second item to the new list
newlist.append[split_results[1]]
Một khi bạn hiểu rõ điều đó, bạn có thể nhận được sự ưa thích và sử dụng danh sách hiểu:
newlist = [x.split['@'][1] for x in mylist]
Đã trả lời ngày 8 tháng 6 năm 2017 lúc 22:32Jun 8, 2017 at 22:32
calico_calico_calico_
1.13112 Huy hiệu bạc23 Huy hiệu đồng12 silver badges23 bronze badges
Đó là giải pháp của tôi với nhau cho các vòng lặp:
myl = ['', ''...]
results = []
for element in myl:
for x in element:
if x == '@':
x = element.index['@']
results.append[element[x+1:]]
Đã trả lời ngày 8 tháng 6 năm 2017 lúc 22:29Jun 8, 2017 at 22:29
Dannyxndannyxndannyxn
4124 Huy hiệu bạc16 Huy hiệu đồng4 silver badges16 bronze badges
________số 8
Example:
Độ phức tạp về thời gian: O [N2]test_list1 = [“Geeksforgeeks”, “best”, “for”, “geeks”], test_list2 = [“Geeks”, “win”, “or”, “learn”]
Độ phức tạp không gian: O [n] [‘Geeks’, ‘or’]
Phương pháp số 3: Sử dụng phương thức Find [] “Geeks” occurs in “Geeksforgeeks string as substring.
3newlist = [x.split['@'][1] for x in mylist]
65mylist = ['gmail.com', 'hotmail.com'...]
5newlist = [x.split['@'][1] for x in mylist]
67 test_list1 = [“geeksforgeeks”, “best”, “4”, “geeks”], test_list2 = [“Geeks”, “win”, “or”, “learn”]mylist = ['gmail.com', 'hotmail.com'...]
0newlist = [x.split['@'][1] for x in mylist]
3newlist = [x.split['@'][1] for x in mylist]
70mylist = ['gmail.com', 'hotmail.com'...]
5newlist = [x.split['@'][1] for x in mylist]
72 []mylist = ['gmail.com', 'hotmail.com'...]
73mylist = ['gmail.com', 'hotmail.com'...]
1myl = ['', ''...] results = [] for element in myl: for x in element: if x == '@': x = element.index['@'] results.append[element[x+1:]]
75mylist = ['gmail.com', 'hotmail.com'...]
3mylist = ['gmail.com', 'hotmail.com'...]
777____178 No substrings found.mylist = ['gmail.com', 'hotmail.com'...]
Làm thế nào để tôi tìm thấy một chuỗi con trong một danh sách các chuỗi trong Python?
Chúng ta hãy thảo luận về một số cách để tìm các chuỗi có chuỗi con đã cho trong danh sách ..
Python3
mylist = ['gmail.com', 'hotmail.com'...]
2mylist = ['gmail.com', 'hotmail.com'...]
3 mylist = ['gmail.com', 'hotmail.com'...]
4mylist = ['gmail.com', 'hotmail.com'...]
5mylist = ['gmail.com', 'hotmail.com'...]
6mylist = ['gmail.com', 'hotmail.com'...]
7mylist = ['gmail.com', 'hotmail.com'...]
6mylist = ['gmail.com', 'hotmail.com'...]
9mylist = ['gmail.com', 'hotmail.com'...]
6for x in range[len[mylist]]:
mylist[x].split["@",1][1]
1for x in range[len[mylist]]:
mylist[x].split["@",1][1]
2for x in range[len[mylist]]:
mylist[x].split["@",1][1]
3mylist = ['gmail.com', 'hotmail.com'...]
3 mylist = ['gmail.com', 'hotmail.com'...]
4for x in range[len[mylist]]:
mylist[x].split["@",1][1]
6mylist = ['gmail.com', 'hotmail.com'...]
6for x in range[len[mylist]]:
mylist[x].split["@",1][1]
8mylist = ['gmail.com', 'hotmail.com'...]
6for x in mylist:
#do something
0__16for x in mylist:
#do something
222for x in mylist:
#do something
4for x in mylist:
#do something
5for x in mylist:
#do something
6 for x in mylist:
#do something
7 for x in mylist:
#do something
8for x in mylist:
#do something
9for x in mylist:
#do something
4for x in mylist:
#do something
5#In order to add to another list you need to make another list
newlist = []
for x in mylist:
split_results = x.split['@']
# Now you have a tuple of the results of your split
# add the second item to the new list
newlist.append[split_results[1]]
2 for x in mylist:
#do something
7 for x in mylist:
#do something
8#In order to add to another list you need to make another list
newlist = []
for x in mylist:
split_results = x.split['@']
# Now you have a tuple of the results of your split
# add the second item to the new list
newlist.append[split_results[1]]
5#In order to add to another list you need to make another list
newlist = []
for x in mylist:
split_results = x.split['@']
# Now you have a tuple of the results of your split
# add the second item to the new list
newlist.append[split_results[1]]
6mylist = ['gmail.com', 'hotmail.com'...]
3 mylist = ['gmail.com', 'hotmail.com'...]
07newlist = [x.split['@'][1] for x in mylist]
3 mylist = ['gmail.com', 'hotmail.com'...]
09______55________số 8
for x in mylist:
#do something
4for x in mylist:
#do something
5myl = ['', ''...]
results = []
for element in myl:
for x in element:
if x == '@':
x = element.index['@']
results.append[element[x+1:]]
7 for x in mylist:
#do something
7 for x in mylist:
#do something
8The original list 1 is : ['Geeksforgeeks', 'best', 'for', 'geeks'] The original list 2 is : ['Geeks', 'win', 'or', 'learn'] Substrings Intersections : ['or', 'Geeks']0
Đầu ra
The original list 1 is : ['Geeksforgeeks', 'best', 'for', 'geeks'] The original list 2 is : ['Geeks', 'win', 'or', 'learn'] Substrings Intersections : ['or', 'Geeks']
Phương pháp số 2: Sử dụng bất kỳ [] + biểu thức máy phát nào
Trong đó, bất kỳ [] nào được sử dụng để kiểm tra khớp nối trong bất kỳ chuỗi nào từ danh sách chuỗi để khớp với nhau.
Python3
mylist = ['gmail.com', 'hotmail.com'...]
2mylist = ['gmail.com', 'hotmail.com'...]
3 mylist = ['gmail.com', 'hotmail.com'...]
4mylist = ['gmail.com', 'hotmail.com'...]
5mylist = ['gmail.com', 'hotmail.com'...]
6mylist = ['gmail.com', 'hotmail.com'...]
7mylist = ['gmail.com', 'hotmail.com'...]
6mylist = ['gmail.com', 'hotmail.com'...]
9mylist = ['gmail.com', 'hotmail.com'...]
6for x in range[len[mylist]]:
mylist[x].split["@",1][1]
1for x in range[len[mylist]]:
mylist[x].split["@",1][1]
2for x in range[len[mylist]]:
mylist[x].split["@",1][1]
3mylist = ['gmail.com', 'hotmail.com'...]
3 mylist = ['gmail.com', 'hotmail.com'...]
4for x in range[len[mylist]]:
mylist[x].split["@",1][1]
6mylist = ['gmail.com', 'hotmail.com'...]
6for x in range[len[mylist]]:
mylist[x].split["@",1][1]
8mylist = ['gmail.com', 'hotmail.com'...]
6for x in mylist:
#do something
0__16for x in mylist:
#do something
222for x in mylist:
#do something
4for x in mylist:
#do something
5for x in mylist:
#do something
6 for x in mylist:
#do something
7 for x in mylist:
#do something
8for x in mylist:
#do something
9for x in mylist:
#do something
4for x in mylist:
#do something
5#In order to add to another list you need to make another list
newlist = []
for x in mylist:
split_results = x.split['@']
# Now you have a tuple of the results of your split
# add the second item to the new list
newlist.append[split_results[1]]
2 for x in mylist:
#do something
7 for x in mylist:
#do something
8#In order to add to another list you need to make another list
newlist = []
for x in mylist:
split_results = x.split['@']
# Now you have a tuple of the results of your split
# add the second item to the new list
newlist.append[split_results[1]]
5#In order to add to another list you need to make another list
newlist = []
for x in mylist:
split_results = x.split['@']
# Now you have a tuple of the results of your split
# add the second item to the new list
newlist.append[split_results[1]]
6mylist = ['gmail.com', 'hotmail.com'...]
3 mylist = ['gmail.com', 'hotmail.com'...]
07newlist = [x.split['@'][1] for x in mylist]
3 mylist = ['gmail.com', 'hotmail.com'...]
09______55for x in mylist:
#do something
4for x in mylist:
#do something
5myl = ['', ''...]
results = []
for element in myl:
for x in element:
if x == '@':
x = element.index['@']
results.append[element[x+1:]]
7 for x in mylist:
#do something
7 for x in mylist:
#do something
8The original list 1 is : ['Geeksforgeeks', 'best', 'for', 'geeks'] The original list 2 is : ['Geeks', 'win', 'or', 'learn'] Substrings Intersections : ['or', 'Geeks']0
Đầu ra
Phương pháp số 2: Sử dụng bất kỳ [] + biểu thức máy phát nàoTrong đó, bất kỳ [] nào được sử dụng để kiểm tra khớp nối trong bất kỳ chuỗi nào từ danh sách chuỗi để khớp với nhau.O[n2]
2mylist = ['gmail.com', 'hotmail.com'...]
3mylist = ['gmail.com', 'hotmail.com'...]
4mylist = ['gmail.com', 'hotmail.com'...]
5mylist = ['gmail.com', 'hotmail.com'...]
6mylist = ['gmail.com', 'hotmail.com'...]
7mylist = ['gmail.com', 'hotmail.com'...]
6mylist = ['gmail.com', 'hotmail.com'...]
9mylist = ['gmail.com', 'hotmail.com'...]
6mylist = ['gmail.com', 'hotmail.com'...]
1for x in range[len[mylist]]: mylist[x].split["@",1][1]
2O[n]for x in range[len[mylist]]: mylist[x].split["@",1][1]
3for x in range[len[mylist]]:
mylist[x].split["@",1][1]
3 mylist = ['gmail.com', 'hotmail.com'...]
4mylist = ['gmail.com', 'hotmail.com'...]
6for x in range[len[mylist]]:
mylist[x].split["@",1][1]
6mylist = ['gmail.com', 'hotmail.com'...]
8for x in range[len[mylist]]:
mylist[x].split["@",1][1]
6mylist = ['gmail.com', 'hotmail.com'...]
0__16for x in mylist:
#do something
222for x in mylist:
#do something
Python3
mylist = ['gmail.com', 'hotmail.com'...]
2mylist = ['gmail.com', 'hotmail.com'...]
3 mylist = ['gmail.com', 'hotmail.com'...]
4mylist = ['gmail.com', 'hotmail.com'...]
5mylist = ['gmail.com', 'hotmail.com'...]
6mylist = ['gmail.com', 'hotmail.com'...]
7mylist = ['gmail.com', 'hotmail.com'...]
6mylist = ['gmail.com', 'hotmail.com'...]
9mylist = ['gmail.com', 'hotmail.com'...]
6for x in range[len[mylist]]:
mylist[x].split["@",1][1]
1for x in range[len[mylist]]:
mylist[x].split["@",1][1]
2for x in range[len[mylist]]:
mylist[x].split["@",1][1]
3mylist = ['gmail.com', 'hotmail.com'...]
3 mylist = ['gmail.com', 'hotmail.com'...]
4for x in range[len[mylist]]:
mylist[x].split["@",1][1]
6mylist = ['gmail.com', 'hotmail.com'...]
6for x in range[len[mylist]]:
mylist[x].split["@",1][1]
8mylist = ['gmail.com', 'hotmail.com'...]
6for x in mylist:
#do something
0__16for x in mylist:
#do something
222for x in mylist:
#do something
4for x in mylist:
#do something
5for x in mylist:
#do something
6 for x in mylist:
#do something
7 for x in mylist:
#do something
8for x in mylist:
#do something
9
for x in mylist:
#do something
4for x in mylist:
#do something
5#In order to add to another list you need to make another list
newlist = []
for x in mylist:
split_results = x.split['@']
# Now you have a tuple of the results of your split
# add the second item to the new list
newlist.append[split_results[1]]
2 for x in mylist:
#do something
7 for x in mylist:
#do something
8#In order to add to another list you need to make another list
newlist = []
for x in mylist:
split_results = x.split['@']
# Now you have a tuple of the results of your split
# add the second item to the new list
newlist.append[split_results[1]]
5mylist = ['gmail.com', 'hotmail.com'...]
61mylist = ['gmail.com', 'hotmail.com'...]
3mylist = ['gmail.com', 'hotmail.com'...]
63newlist = [x.split['@'][1] for x in mylist]
3 mylist = ['gmail.com', 'hotmail.com'...]
65newlist = [x.split['@'][1] for x in mylist]
5 mylist = ['gmail.com', 'hotmail.com'...]
67newlist = [x.split['@'][1] for x in mylist]
0newlist = [x.split['@'][1] for x in mylist]
3 mylist = ['gmail.com', 'hotmail.com'...]
70newlist = [x.split['@'][1] for x in mylist]
5 mylist = ['gmail.com', 'hotmail.com'...]
72mylist = ['gmail.com', 'hotmail.com'...]
73myl = ['', ''...]
results = []
for element in myl:
for x in element:
if x == '@':
x = element.index['@']
results.append[element[x+1:]]
1mylist = ['gmail.com', 'hotmail.com'...]
75mylist = ['gmail.com', 'hotmail.com'...]
3mylist = ['gmail.com', 'hotmail.com'...]
777____178mylist = ['gmail.com', 'hotmail.com'...]
84mylist = ['gmail.com', 'hotmail.com'...]
85for x in mylist:
#do something
4for x in mylist:
#do something
5myl = ['', ''...]
results = []
for element in myl:
for x in element:
if x == '@':
x = element.index['@']
results.append[element[x+1:]]
7 for x in mylist:
#do something
7 for x in mylist:
#do something
8The original list 1 is : ['Geeksforgeeks', 'best', 'for', 'geeks'] The original list 2 is : ['Geeks', 'win', 'or', 'learn'] Substrings Intersections : ['or', 'Geeks']0
Đầu ra
The original list 1 is : ['Geeksforgeeks', 'best', 'for', 'geeks'] The original list 2 is : ['Geeks', 'win', 'or', 'learn'] Substrings Intersections : ['Geeks', 'or']