Hướng dẫn python extract substring from list of strings - python trích xuất chuỗi con từ danh sách các chuỗi

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.

newlist = [x.split['@'][1] for x in mylist]
3
mylist = ['gmail.com', 'hotmail.com'...]
65
newlist = [x.split['@'][1] for x in mylist]
5
mylist = ['gmail.com', 'hotmail.com'...]
67
test_list1 = [“geeksforgeeks”, “best”, “4”, “geeks”], test_list2 = [“Geeks”, “win”, “or”, “learn”]

newlist = [x.split['@'][1] for x in mylist]
0
newlist = [x.split['@'][1] for x in mylist]
3
mylist = ['gmail.com', 'hotmail.com'...]
70
newlist = [x.split['@'][1] for x in mylist]
5
mylist = ['gmail.com', 'hotmail.com'...]
72
[]

mylist = ['gmail.com', 'hotmail.com'...]
73
myl = ['', ''...]
results = []
for element in myl:
    for x in element:
        if x == '@':
            x =  element.index['@']
            results.append[element[x+1:]]
1
mylist = ['gmail.com', 'hotmail.com'...]
75
mylist = ['gmail.com', 'hotmail.com'...]
3
mylist = ['gmail.com', 'hotmail.com'...]
777____178
No substrings found.

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'...]
2
mylist = ['gmail.com', 'hotmail.com'...]
3
mylist = ['gmail.com', 'hotmail.com'...]
4
mylist = ['gmail.com', 'hotmail.com'...]
5
mylist = ['gmail.com', 'hotmail.com'...]
6
mylist = ['gmail.com', 'hotmail.com'...]
7
mylist = ['gmail.com', 'hotmail.com'...]
6
mylist = ['gmail.com', 'hotmail.com'...]
9
mylist = ['gmail.com', 'hotmail.com'...]
6
for x in range[len[mylist]]:
  mylist[x].split["@",1][1]
1
for x in range[len[mylist]]:
  mylist[x].split["@",1][1]
2

for x in range[len[mylist]]:
  mylist[x].split["@",1][1]
3
mylist = ['gmail.com', 'hotmail.com'...]
3
mylist = ['gmail.com', 'hotmail.com'...]
4
for x in range[len[mylist]]:
  mylist[x].split["@",1][1]
6
mylist = ['gmail.com', 'hotmail.com'...]
6
for x in range[len[mylist]]:
  mylist[x].split["@",1][1]
8
mylist = ['gmail.com', 'hotmail.com'...]
6
for x in mylist:
  #do something
0__16
for x in mylist:
  #do something
222

for x in mylist:
  #do something
4
for x in mylist:
  #do something
5
for x in mylist:
  #do something
6
for x in mylist:
  #do something
7
for x in mylist:
  #do something
8
for x in mylist:
  #do something
9

for x in mylist:
  #do something
4
for 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]]
6
mylist = ['gmail.com', 'hotmail.com'...]
3
mylist = ['gmail.com', 'hotmail.com'...]
07
newlist = [x.split['@'][1] for x in mylist]
3
mylist = ['gmail.com', 'hotmail.com'...]
09______55

________số 8

for x in mylist:
  #do something
4
for x in mylist:
  #do something
5
myl = ['', ''...]
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
8
The 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'...]
2
mylist = ['gmail.com', 'hotmail.com'...]
3
mylist = ['gmail.com', 'hotmail.com'...]
4
mylist = ['gmail.com', 'hotmail.com'...]
5
mylist = ['gmail.com', 'hotmail.com'...]
6
mylist = ['gmail.com', 'hotmail.com'...]
7
mylist = ['gmail.com', 'hotmail.com'...]
6
mylist = ['gmail.com', 'hotmail.com'...]
9
mylist = ['gmail.com', 'hotmail.com'...]
6
for x in range[len[mylist]]:
  mylist[x].split["@",1][1]
1
for x in range[len[mylist]]:
  mylist[x].split["@",1][1]
2

for x in range[len[mylist]]:
  mylist[x].split["@",1][1]
3
mylist = ['gmail.com', 'hotmail.com'...]
3
mylist = ['gmail.com', 'hotmail.com'...]
4
for x in range[len[mylist]]:
  mylist[x].split["@",1][1]
6
mylist = ['gmail.com', 'hotmail.com'...]
6
for x in range[len[mylist]]:
  mylist[x].split["@",1][1]
8
mylist = ['gmail.com', 'hotmail.com'...]
6
for x in mylist:
  #do something
0__16
for x in mylist:
  #do something
222

for x in mylist:
  #do something
4
for x in mylist:
  #do something
5
for x in mylist:
  #do something
6
for x in mylist:
  #do something
7
for x in mylist:
  #do something
8
for x in mylist:
  #do something
9

for x in mylist:
  #do something
4
for 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]]
6
mylist = ['gmail.com', 'hotmail.com'...]
3
mylist = ['gmail.com', 'hotmail.com'...]
07
newlist = [x.split['@'][1] for x in mylist]
3
mylist = ['gmail.com', 'hotmail.com'...]
09______55

for x in mylist:
  #do something
4
for x in mylist:
  #do something
5
myl = ['', ''...]
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
8
The 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à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.O[n2]

mylist = ['gmail.com', 'hotmail.com'...]
2
mylist = ['gmail.com', 'hotmail.com'...]
3
mylist = ['gmail.com', 'hotmail.com'...]
4
mylist = ['gmail.com', 'hotmail.com'...]
5
mylist = ['gmail.com', 'hotmail.com'...]
6
mylist = ['gmail.com', 'hotmail.com'...]
7
mylist = ['gmail.com', 'hotmail.com'...]
6
mylist = ['gmail.com', 'hotmail.com'...]
9
mylist = ['gmail.com', 'hotmail.com'...]
6
for x in range[len[mylist]]:
  mylist[x].split["@",1][1]
1
for x in range[len[mylist]]:
  mylist[x].split["@",1][1]
2
O[n]

for x in range[len[mylist]]:
  mylist[x].split["@",1][1]
3
mylist = ['gmail.com', 'hotmail.com'...]
3
mylist = ['gmail.com', 'hotmail.com'...]
4
for x in range[len[mylist]]:
  mylist[x].split["@",1][1]
6
mylist = ['gmail.com', 'hotmail.com'...]
6
for x in range[len[mylist]]:
  mylist[x].split["@",1][1]
8
mylist = ['gmail.com', 'hotmail.com'...]
6
for x in mylist:
  #do something
0__16
for x in mylist:
  #do something
222

Python3

mylist = ['gmail.com', 'hotmail.com'...]
2
mylist = ['gmail.com', 'hotmail.com'...]
3
mylist = ['gmail.com', 'hotmail.com'...]
4
mylist = ['gmail.com', 'hotmail.com'...]
5
mylist = ['gmail.com', 'hotmail.com'...]
6
mylist = ['gmail.com', 'hotmail.com'...]
7
mylist = ['gmail.com', 'hotmail.com'...]
6
mylist = ['gmail.com', 'hotmail.com'...]
9
mylist = ['gmail.com', 'hotmail.com'...]
6
for x in range[len[mylist]]:
  mylist[x].split["@",1][1]
1
for x in range[len[mylist]]:
  mylist[x].split["@",1][1]
2

for x in range[len[mylist]]:
  mylist[x].split["@",1][1]
3
mylist = ['gmail.com', 'hotmail.com'...]
3
mylist = ['gmail.com', 'hotmail.com'...]
4
for x in range[len[mylist]]:
  mylist[x].split["@",1][1]
6
mylist = ['gmail.com', 'hotmail.com'...]
6
for x in range[len[mylist]]:
  mylist[x].split["@",1][1]
8
mylist = ['gmail.com', 'hotmail.com'...]
6
for x in mylist:
  #do something
0__16
for x in mylist:
  #do something
222

for x in mylist:
  #do something
4
for x in mylist:
  #do something
5
for x in mylist:
  #do something
6
for x in mylist:
  #do something
7
for x in mylist:
  #do something
8
for x in mylist:
  #do something
9

for x in mylist:
  #do something
4
for 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

mylist = ['gmail.com', 'hotmail.com'...]
61
mylist = ['gmail.com', 'hotmail.com'...]
3
mylist = ['gmail.com', 'hotmail.com'...]
63

newlist = [x.split['@'][1] for x in mylist]
3
mylist = ['gmail.com', 'hotmail.com'...]
65
newlist = [x.split['@'][1] for x in mylist]
5
mylist = ['gmail.com', 'hotmail.com'...]
67

newlist = [x.split['@'][1] for x in mylist]
0
newlist = [x.split['@'][1] for x in mylist]
3
mylist = ['gmail.com', 'hotmail.com'...]
70
newlist = [x.split['@'][1] for x in mylist]
5
mylist = ['gmail.com', 'hotmail.com'...]
72

mylist = ['gmail.com', 'hotmail.com'...]
73
myl = ['', ''...]
results = []
for element in myl:
    for x in element:
        if x == '@':
            x =  element.index['@']
            results.append[element[x+1:]]
1
mylist = ['gmail.com', 'hotmail.com'...]
75
mylist = ['gmail.com', 'hotmail.com'...]
3
mylist = ['gmail.com', 'hotmail.com'...]
777____178

mylist = ['gmail.com', 'hotmail.com'...]
84
mylist = ['gmail.com', 'hotmail.com'...]
85

for x in mylist:
  #do something
4
for x in mylist:
  #do something
5
myl = ['', ''...]
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
8
The 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']


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 ...
Phương pháp số 1: Sử dụng danh sách hiểu.Danh sách hiểu là một cách thanh lịch để thực hiện bất kỳ nhiệm vụ cụ thể nào vì nó tăng khả năng đọc trong một thời gian dài.....
Phương pháp số 2: Sử dụng Filter [] + Lambda.....
Phương pháp số 3: Sử dụng re + search [].

Làm thế nào để bạn trích xuất một chuỗi từ một danh sách trong Python?

Để chuyển đổi danh sách thành một chuỗi, hãy sử dụng khả năng hiểu danh sách Python và hàm tham gia [].Sự hiểu biết danh sách sẽ đi qua từng phần tử một và phương thức tham gia [] sẽ kết hợp các phần tử của danh sách thành một chuỗi mới và trả về nó làm đầu ra.use Python List Comprehension and the join[] function. The list comprehension will traverse the elements one by one, and the join[] method will concatenate the list's elements into a new string and return it as output.

Bài Viết Liên Quan

Chủ Đề