Hướng dẫn find all positions of character in string python - tìm tất cả các vị trí của ký tự trong chuỗi python

Tôi đang cố gắng tìm tất cả những sự xuất hiện của "|" trong một chuỗi.

def findSectionOffsets(text): startingPos = 0 endPos = len(text) for position in text.find("|",startingPos, endPos): print position endPos = position

Nhưng tôi gặp lỗi:

for position in text.find("|",startingPos, endPos): TypeError: 'int' object is not iterable

Hỏi ngày 22 tháng 10 năm 2012 lúc 10:39Oct 22, 2012 at 10:39

2

Chức năng:

def findOccurrences(s, ch): return [i for i, letter in enumerate(s) if letter == ch] findOccurrences(yourString, '|')

sẽ trả về một danh sách các chỉ số của for position in text.find("|",startingPos, endPos): TypeError: 'int' object is not iterable 0 trong đó for position in text.find("|",startingPos, endPos): TypeError: 'int' object is not iterable 1 xảy ra.

Mooncrater

3.6504 huy hiệu vàng27 Huy hiệu bạc54 Huy hiệu đồng4 gold badges27 silver badges54 bronze badges

Đã trả lời ngày 22 tháng 10 năm 2012 lúc 10:50Oct 22, 2012 at 10:50

Marco L.Marco L.Marco L.

1.4894 Huy hiệu vàng17 Huy hiệu bạc25 Huy hiệu Đồng4 gold badges17 silver badges25 bronze badges

3

Nếu bạn muốn chỉ mục của tất cả các lần xuất hiện của ký tự for position in text.find("|",startingPos, endPos): TypeError: 'int' object is not iterable 1 trong một chuỗi, bạn có thể làm điều này

import re str = "aaaaaa|bbbbbb|ccccc|dddd" indexes = [x.start() for x in re.finditer('\|', str)] print(indexes) # <-- [6, 13, 19]

Ngoài ra bạn có thể làm

indexes = [x for x, v in enumerate(str) if v == '|'] print(indexes) # <-- [6, 13, 19]

Đã trả lời ngày 22 tháng 10 năm 2012 lúc 10:45Oct 22, 2012 at 10:45

Avasalavasalavasal

Huy hiệu vàng 14K4 30 Huy hiệu bạc47 Huy hiệu đồng4 gold badges30 silver badges47 bronze badges

Nó dễ dàng hơn để sử dụng các biểu thức thường xuyên ở đây;

import re def findSectionOffsets(text): for m in re.finditer('\|', text): print m.start(0)

Đã trả lời ngày 22 tháng 10 năm 2012 lúc 10:56Oct 22, 2012 at 10:56

Roland Smithroland SmithRoland Smith

41.2k3 Huy hiệu vàng61 Huy hiệu bạc89 Huy hiệu đồng3 gold badges61 silver badges89 bronze badges

import re def findSectionOffsets(text) for i,m in enumerate(re.finditer('\|',text)) : print i, m.start(), m.end()

DBR

162K65 Huy hiệu vàng274 Huy hiệu bạc340 Huy hiệu đồng65 gold badges274 silver badges340 bronze badges

Đã trả lời ngày 22 tháng 10 năm 2012 lúc 10:57Oct 22, 2012 at 10:57

ZuluzuluZulu

8.1969 Huy hiệu vàng46 Huy hiệu bạc55 Huy hiệu Đồng9 gold badges46 silver badges55 bronze badges

for position in text.find("|",startingPos, endPos): TypeError: 'int' object is not iterable 3 Trả về một số nguyên (chỉ mục mà tại đó chuỗi mong muốn được tìm thấy), do đó bạn có thể chạy cho vòng lặp qua nó.

Tôi đề nghị:

def findSectionOffsets(text): indexes = [] startposition = 0 while True: i = text.find("|", startposition) if i == -1: break indexes.append(i) startposition = i + 1 return indexes

Đã trả lời ngày 22 tháng 10 năm 2012 lúc 10:48Oct 22, 2012 at 10:48

Samfrancessamfrancessamfrances

3.0653 Huy hiệu vàng24 Huy hiệu bạc37 Huy hiệu đồng3 gold badges24 silver badges37 bronze badges

1

Nếu for position in text.find("|",startingPos, endPos): TypeError: 'int' object is not iterable 4 là chuỗi mà bạn muốn đếm số lượng for position in text.find("|",startingPos, endPos): TypeError: 'int' object is not iterable 5 chứa, dòng mã sau sẽ trả về số lượng:

len(text.split("|"))-1

Lưu ý: Điều này cũng sẽ làm việc để tìm kiếm các chuỗi phụ.

Đã trả lời ngày 30 tháng 8 năm 2018 lúc 7:26Aug 30, 2018 at 7:26

Pablo Reyespablo ReyesPablo Reyes

2.93920 Huy hiệu bạc29 Huy hiệu đồng20 silver badges29 bronze badges

text.find () chỉ trả về kết quả đầu tiên và sau đó bạn cần đặt vị trí bắt đầu mới dựa trên đó. Vì vậy, như thế này:

def findSectionOffsets(text): startingPos = 0 position = text.find("|", startingPos): while position > -1: print position startingPos = position + 1 position = text.find("|", startingPos)

Đã trả lời ngày 22 tháng 10 năm 2012 lúc 10:48Oct 22, 2012 at 10:48

SamfrancessamfrancesM Somerville

3.0653 Huy hiệu vàng24 Huy hiệu bạc37 Huy hiệu đồng28 silver badges37 bronze badges

2

Chủ đề