Hướng dẫn python find index of all spaces in string - python tìm chỉ mục của tất cả các khoảng trắng trong chuỗi

Ví dụ:

"""There are three whitespaces in variable text. The index of whitespaces are 5,6, & 7. My goal is to store these index to a list for which I can iterate later. I've tried using find[] and index[] but they only return the index for which the first occurence occured.""""

text = 'Hello World!'

for e in text:

def get_full_name[]:

    full_name = input["Enter your Full name: "]
    return  full_name
0

def get_full_name[]:

    full_name = input["Enter your Full name: "]
    return  full_name
1

def get_full_name[]:

    full_name = input["Enter your Full name: "]
    return  full_name
2

Đầu vào: 'Xin chào Thế giới!'

Đầu ra: [5,6,7]

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 đang viết một chức năng cho một bài tập trong lớp CS. Nhưng tôi không biết tại sao tôi nhận được lỗi này: cùng một mã hoạt động mà không có chức năng. Tôi cố gắng tìm kiếm cách tìm một chỉ mục "khoảng trắng" trong một chuỗi nhưng tôi không thể tìm thấy bất kỳ. Mục đích của bài tập là trả về tên đầy đủ được gõ bởi người dùng xóa một chữ cái ngẫu nhiên khỏi cả tên và họ. [Đây là "giả sử" tên đầy đủ được gõ bởi người dùng chứa hai phần] Cảm ơn

Enter your Full name: Emily Watson
Traceback [most recent call last]:
  File "cs_lec7_char_shuffle_ex.py", line 33, in 
    version1 = remove_two_letters[full_name]
  File "cs_lec7_char_shuffle_ex.py", line 16, in remove_two_letters
    gap_pos = full_name.find[" "]                    
AttributeError: 'NoneType' object has no attribute 'find'

***Repl Closed***

import random

def get_full_name[]:

    full_name = input["Enter your Full name: "]

def remove_two_letters[full_name]:

    gap_pos = full_name.find[" "]                    
    name_length = len[full_name]                    

    first_name = full_name[0:gap_pos]               
    sur_name = full_name[gap_pos+1:name_length]

    first_random_pos = random.randrange[1,gap_pos]  
    sur_random_pos = random.randrange[gap_pos+1, name_length]

    first_name = first_name.replace[full_name[first_random_pos],"",1]    
    sur_name = sur_name.replace[full_name[sur_random_pos],"",1]         

    removed_name = first_name + " " + sur_name

    return removed_name

full_name = get_full_name[]
version1 = remove_two_letters[full_name]

print[version1]

Hỏi ngày 16 tháng 10 năm 2015 lúc 8:18Oct 16, 2015 at 8:18

Newbie_AndroidNewbie_AndroidNewbie_Android

2252 Huy hiệu vàng3 Huy hiệu bạc12 Huy hiệu đồng2 gold badges3 silver badges12 bronze badges

Bạn đã xác định

def get_full_name[]:

    full_name = input["Enter your Full name: "]
    return  full_name
3 bên trong không gian tên địa phương của
def get_full_name[]:

    full_name = input["Enter your Full name: "]
    return  full_name
4, nếu bạn muốn sử dụng nó ở một nơi khác, bạn cần trả lại:

def get_full_name[]:

    full_name = input["Enter your Full name: "]
    return  full_name

Đó là những gì bạn đã có

def get_full_name[]:

    full_name = input["Enter your Full name: "]
    return  full_name
5 vì chức năng của bạn không trả về không và bạn đã không truyền cho
def get_full_name[]:

    full_name = input["Enter your Full name: "]
    return  full_name
6.

Đã trả lời ngày 16 tháng 10 năm 2015 lúc 8:20Oct 16, 2015 at 8:20

1

>>> string_1 = "That's it"
>>> string_1.index[" "]
6

Đã trả lời ngày 16 tháng 10 năm 2015 lúc 8:20Oct 16, 2015 at 8:20

LaszlowatylaszlowatyLaszlowaty

1.2671 Huy hiệu vàng11 Huy hiệu bạc19 Huy hiệu đồng1 gold badge11 silver badges19 bronze badges

1

Hãy kiểm tra lỗi mà bạn đang nhận được:

AttributeError: 'NoneType' object has no attribute 'find'

Điều đó có nghĩa là bạn đang cố gắng truy cập thuộc tính

def get_full_name[]:

    full_name = input["Enter your Full name: "]
    return  full_name
7 trên một đối tượng loại
def get_full_name[]:

    full_name = input["Enter your Full name: "]
    return  full_name
8. Chúng ta hãy xem dòng tạo ra lỗi, như được chỉ ra trong StackTrace:

gap_pos = full_name.find[" "]

Thuộc tính tìm thấy đang được gọi trên đối tượng

def get_full_name[]:

    full_name = input["Enter your Full name: "]
    return  full_name
3. Do đó, chúng ta có thể kết luận rằng đối tượng
def get_full_name[]:

    full_name = input["Enter your Full name: "]
    return  full_name
3 thuộc loại
def get_full_name[]:

    full_name = input["Enter your Full name: "]
    return  full_name
8.

Nếu chúng tôi theo dõi biến

def get_full_name[]:

    full_name = input["Enter your Full name: "]
    return  full_name
3 trở lại thông qua mã gọi, chúng tôi thấy rằng nó được cho là được đặt bởi hàm
def get_full_name[]:

    full_name = input["Enter your Full name: "]
    return  full_name
4, trên thực tế sẽ trả về
def get_full_name[]:

    full_name = input["Enter your Full name: "]
    return  full_name
8 do không có câu lệnh
>>> string_1 = "That's it"
>>> string_1.index[" "]
6
5 rõ ràng.

Đã trả lời ngày 16 tháng 10 năm 2015 lúc 8:30Oct 16, 2015 at 8:30

dotancohendotancohendotancohen

28.7K33 Huy hiệu vàng133 Huy hiệu bạc192 Huy hiệu Đồng33 gold badges133 silver badges192 bronze badges

1

Lần tiếp theo, nếu bạn muốn một manh mối để biết điều gì là sai, bạn có thể sử dụng loại [] để tìm ra rằng đây không phải là cùng loại giữa full_name đầu tiên của bạn và full_name thứ hai, vì vậy bạn chỉ cần trả lại biến của mình.

Tôi đã chạy bài kiểm tra cho bạn, và tôi đã có:

def get_full_name[]:

  full_name = input["Enter your Full name: "]
  print[type[full_name]]


full_name = get_full_name[]
print[type[full_name]]

Đầu ra:



Đã trả lời ngày 16 tháng 10 năm 2015 lúc 8:34Oct 16, 2015 at 8:34

Làm thế nào để bạn có được tất cả các chỉ mục của một phần tử trong một chuỗi trong Python?

Để tìm chỉ mục của một phần tử danh sách trong Python, hãy sử dụng phương thức Index [] tích hợp.Để tìm chỉ mục của một ký tự trong một chuỗi, hãy sử dụng phương thức index [] trên chuỗi.Đây là câu trả lời nhanh chóng.use the index[] method on the string. This is the quick answer.

Có phải không gian bao gồm trong Chỉ số Python?

Do đó, số chỉ số dương do đó, mỗi ký tự [bao gồm khoảng trắng] sẽ được biểu diễn bằng cái được gọi là chỉ mục.Một chỉ mục là vị trí của một mục cụ thể trong một tập dữ liệu.each character [whitespace included] will be represented by what is known as an index. An index is the location of a particular item within a data set.

Làm thế nào để bạn tìm thấy chỉ mục của một chuỗi cụ thể?

Phương thức indexof [] trả về vị trí của sự xuất hiện đầu tiên của [các] ký tự được chỉ định trong một chuỗi.Mẹo: Sử dụng phương thức LastIndexof để trả về vị trí của lần xuất hiện cuối cùng của [các] ký tự được chỉ định trong một chuỗi.. Tip: Use the lastIndexOf method to return the position of the last occurrence of specified character[s] in a string.

Làm thế nào để bạn trả về một chỉ mục chuỗi trong Python?

Python String index [] Phương thức index [] trả về chỉ mục của một chuỗi con bên trong chuỗi [nếu tìm thấy].Nếu không tìm thấy chất nền, nó sẽ tăng một ngoại lệ.The index[] method returns the index of a substring inside the string [if found]. If the substring is not found, it raises an exception.

Bài Viết Liên Quan

Chủ Đề