Hướng dẫn check if next element exists python - kiểm tra xem phần tử tiếp theo có tồn tại không python

Tôi đang thực hiện một số thử thách với Python.

Tôi có mã sau:

def SimpleSymbols[str]:
    letters = []
    results = []

    for i in range[len[str]]:
        if str[i] != '=' and str[i] != '+':
            letters.append[str[i]]

            if str[i-1] == '+' and str[i+1] == '+':
                results.append[str[i]]

    return len[letters] == len[results]

//repl.it/BH1s/1

Nếu tôi kiểm tra print[SimpleSymbols['++d+===+c++==']] thì mọi thứ đều ổn nhưng nếu tôi kiểm tra print[SimpleSymbols['++d+===+c++==+a']]

Tôi nhận được lỗi này:

Traceback [cuộc gọi gần đây nhất cuối cùng]: Tệp "Python", dòng 15, trong tệp "Python", dòng 9, trong SimplesyMbols IndexError: String Index ngoài phạm vi

Ai đó có thể giải thích cho tôi xin vui lòng tại sao?

Hỏi ngày 12 tháng 9 năm 2015 lúc 7:11Sep 12, 2015 at 7:11

2

Bạn đang cố gắng truy cập chỉ mục str[i+1], trên lần lặp cuối cùng, ngoài giới hạn ...

Trong trường hợp bạn phải sử dụng nó, hãy hạn chế vòng lặp để chỉ chạy đến len[str]-1. Để xử lý trường hợp: '++d+===+c++==+a', bạn có thể thêm séc sau vòng lặp để xác minh rằng phần tử một trước không phải là

def SimpleSymbols[str]:
    letters = []
    results = []

    for i in range[len[str]]:
        if str[i] != '=' and str[i] != '+':
            letters.append[str[i]]
        try:
            if str[i-1] == '+' and str[i+1] == '+':
                results.append[str[i]]
        except IndexError:
            do what needs to be done in this case.

    return len[letters] == len[results]
0 [hoặc để thêm nó vào điều kiện
def SimpleSymbols[str]:
    letters = []
    results = []

    for i in range[len[str]]:
        if str[i] != '=' and str[i] != '+':
            letters.append[str[i]]
        try:
            if str[i-1] == '+' and str[i+1] == '+':
                results.append[str[i]]
        except IndexError:
            do what needs to be done in this case.

    return len[letters] == len[results]
1 trong vòng lặp].

Hơn nữa, đó là một thực tế tồi tệ khi đặt tên cho biến của bạn

def SimpleSymbols[str]:
    letters = []
    results = []

    for i in range[len[str]]:
        if str[i] != '=' and str[i] != '+':
            letters.append[str[i]]
        try:
            if str[i-1] == '+' and str[i+1] == '+':
                results.append[str[i]]
        except IndexError:
            do what needs to be done in this case.

    return len[letters] == len[results]
2 là chức năng tích hợp Python!

Đã trả lời ngày 12 tháng 9 năm 2015 lúc 7:14Sep 12, 2015 at 7:14

NIR Alfasinir AlfasiNir Alfasi

52.5K11 Huy hiệu vàng82 Huy hiệu bạc124 Huy hiệu đồng11 gold badges82 silver badges124 bronze badges

2

Bởi vì khi bạn đến nhân vật cuối cùng của Str và đó là một lá thư, thì str[i+1] không tồn tại.

Bạn cần đặt điều kiện trong một khối

def SimpleSymbols[str]:
    letters = []
    results = []

    for i in range[len[str]]:
        if str[i] != '=' and str[i] != '+':
            letters.append[str[i]]
        try:
            if str[i-1] == '+' and str[i+1] == '+':
                results.append[str[i]]
        except IndexError:
            do what needs to be done in this case.

    return len[letters] == len[results]
4 để tránh điều này xảy ra. [Bạn sẽ phải quyết định phải làm gì trong
def SimpleSymbols[str]:
    letters = []
    results = []

    for i in range[len[str]]:
        if str[i] != '=' and str[i] != '+':
            letters.append[str[i]]
        try:
            if str[i-1] == '+' and str[i+1] == '+':
                results.append[str[i]]
        except IndexError:
            do what needs to be done in this case.

    return len[letters] == len[results]
5 khi một
def SimpleSymbols[str]:
    letters = []
    results = []

    for i in range[len[str]]:
        if str[i] != '=' and str[i] != '+':
            letters.append[str[i]]
        try:
            if str[i-1] == '+' and str[i+1] == '+':
                results.append[str[i]]
        except IndexError:
            do what needs to be done in this case.

    return len[letters] == len[results]
6 được nâng lên]

def SimpleSymbols[str]:
    letters = []
    results = []

    for i in range[len[str]]:
        if str[i] != '=' and str[i] != '+':
            letters.append[str[i]]
        try:
            if str[i-1] == '+' and str[i+1] == '+':
                results.append[str[i]]
        except IndexError:
            do what needs to be done in this case.

    return len[letters] == len[results]

Đã trả lời ngày 12 tháng 9 năm 2015 lúc 7:15Sep 12, 2015 at 7:15

Reblochon Masquereblochon MasqueReblochon Masque

34.3k10 Huy hiệu vàng51 Huy hiệu bạc73 Huy hiệu đồng10 gold badges51 silver badges73 bronze badges

3

def SimpleSymbols[str]:
    letters = []
    results = []

    for i in range[len[str]]:
        if str[i] != '=' and str[i] != '+':
            letters.append[str[i]]

            if i < len[str]-1 and str[i-1] == '+' and str[i+1] == '+':
                results.append[str[i]]

    return len[letters] == len[results]

print SimpleSymbols["=+b+a"]

Viết mã của bạn theo cách này, mọi thứ sẽ ổn.

Đã trả lời ngày 12 tháng 9 năm 2015 lúc 7:41Sep 12, 2015 at 7:41

Bạn dường như đang tìm kiếm các biểu tượng không phải là

def SimpleSymbols[str]:
    letters = []
    results = []

    for i in range[len[str]]:
        if str[i] != '=' and str[i] != '+':
            letters.append[str[i]]
        try:
            if str[i-1] == '+' and str[i+1] == '+':
                results.append[str[i]]
        except IndexError:
            do what needs to be done in this case.

    return len[letters] == len[results]
0 cũng như
def SimpleSymbols[str]:
    letters = []
    results = []

    for i in range[len[str]]:
        if str[i] != '=' and str[i] != '+':
            letters.append[str[i]]
        try:
            if str[i-1] == '+' and str[i+1] == '+':
                results.append[str[i]]
        except IndexError:
            do what needs to be done in this case.

    return len[letters] == len[results]
8 và kiểm tra xem tất cả các biểu tượng như vậy đều được theo dõi và đi trước
def SimpleSymbols[str]:
    letters = []
    results = []

    for i in range[len[str]]:
        if str[i] != '=' and str[i] != '+':
            letters.append[str[i]]
        try:
            if str[i-1] == '+' and str[i+1] == '+':
                results.append[str[i]]
        except IndexError:
            do what needs to be done in this case.

    return len[letters] == len[results]
0.

Biểu tượng đầu tiên và cuối cùng là các trường hợp đặc biệt ở chỗ chúng không đi trước cũng như không theo bất cứ điều gì và do đó, nếu chúng không phải là ____10 cũng như

def SimpleSymbols[str]:
    letters = []
    results = []

    for i in range[len[str]]:
        if str[i] != '=' and str[i] != '+':
            letters.append[str[i]]
        try:
            if str[i-1] == '+' and str[i+1] == '+':
                results.append[str[i]]
        except IndexError:
            do what needs to be done in this case.

    return len[letters] == len[results]
8, chức năng của bạn sẽ trả về
def SimpleSymbols[str]:
    letters = []
    results = []

    for i in range[len[str]]:
        if str[i] != '=' and str[i] != '+':
            letters.append[str[i]]

            if i < len[str]-1 and str[i-1] == '+' and str[i+1] == '+':
                results.append[str[i]]

    return len[letters] == len[results]

print SimpleSymbols["=+b+a"]
2.

Vì, trong Python,

def SimpleSymbols[str]:
    letters = []
    results = []

    for i in range[len[str]]:
        if str[i] != '=' and str[i] != '+':
            letters.append[str[i]]

            if i < len[str]-1 and str[i-1] == '+' and str[i+1] == '+':
                results.append[str[i]]

    return len[letters] == len[results]

print SimpleSymbols["=+b+a"]
3 trả về ký tự cuối cùng của chuỗi, vòng lặp của bạn không bị hỏng nếu ký tự đầu tiên không phải là
def SimpleSymbols[str]:
    letters = []
    results = []

    for i in range[len[str]]:
        if str[i] != '=' and str[i] != '+':
            letters.append[str[i]]
        try:
            if str[i-1] == '+' and str[i+1] == '+':
                results.append[str[i]]
        except IndexError:
            do what needs to be done in this case.

    return len[letters] == len[results]
0 cũng không phải là
def SimpleSymbols[str]:
    letters = []
    results = []

    for i in range[len[str]]:
        if str[i] != '=' and str[i] != '+':
            letters.append[str[i]]
        try:
            if str[i-1] == '+' and str[i+1] == '+':
                results.append[str[i]]
        except IndexError:
            do what needs to be done in this case.

    return len[letters] == len[results]
8. Thậm chí trả lại
def SimpleSymbols[str]:
    letters = []
    results = []

    for i in range[len[str]]:
        if str[i] != '=' and str[i] != '+':
            letters.append[str[i]]

            if i < len[str]-1 and str[i-1] == '+' and str[i+1] == '+':
                results.append[str[i]]

    return len[letters] == len[results]

print SimpleSymbols["=+b+a"]
6 cho đầu vào như
def SimpleSymbols[str]:
    letters = []
    results = []

    for i in range[len[str]]:
        if str[i] != '=' and str[i] != '+':
            letters.append[str[i]]

            if i < len[str]-1 and str[i-1] == '+' and str[i+1] == '+':
                results.append[str[i]]

    return len[letters] == len[results]

print SimpleSymbols["=+b+a"]
7. Tuy nhiên, đối với nhân vật cuối cùng, mã của bạn tăng một
def SimpleSymbols[str]:
    letters = []
    results = []

    for i in range[len[str]]:
        if str[i] != '=' and str[i] != '+':
            letters.append[str[i]]
        try:
            if str[i-1] == '+' and str[i+1] == '+':
                results.append[str[i]]
        except IndexError:
            do what needs to be done in this case.

    return len[letters] == len[results]
6 được mong đợi.

Bạn có thể một trong hai

  • Kiểm tra xem biểu tượng đầu tiên hoặc cuối cùng là trong

    def SimpleSymbols[str]:
        letters = []
        results = []
    
        for i in range[len[str]]:
            if str[i] != '=' and str[i] != '+':
                letters.append[str[i]]
    
                if i < len[str]-1 and str[i-1] == '+' and str[i+1] == '+':
                    results.append[str[i]]
    
        return len[letters] == len[results]
    
    print SimpleSymbols["=+b+a"]
    
    9 và trả về
    def SimpleSymbols[str]:
        letters = []
        results = []
    
        for i in range[len[str]]:
            if str[i] != '=' and str[i] != '+':
                letters.append[str[i]]
    
                if i < len[str]-1 and str[i-1] == '+' and str[i+1] == '+':
                    results.append[str[i]]
    
        return len[letters] == len[results]
    
    print SimpleSymbols["=+b+a"]
    
    2 nếu chúng làm vì chúng không thể đi trước hoặc theo sau bởi
    def SimpleSymbols[str]:
        letters = []
        results = []
    
        for i in range[len[str]]:
            if str[i] != '=' and str[i] != '+':
                letters.append[str[i]]
            try:
                if str[i-1] == '+' and str[i+1] == '+':
                    results.append[str[i]]
            except IndexError:
                do what needs to be done in this case.
    
        return len[letters] == len[results]
    
    0. Sau đó, bạn có thể chạy vòng lặp của mình trên chuỗi bên trong:

    def SimpleSymbols[symbols]:
        control = ['+', '=']
    
        if symbols[0] not in control or symbols[-1] not in control:
            return False
    
        for last_index, symbol in enumerate[symbols[1:-1]]:
            next_index = last_index + 2
            if letter not in control:
                if symbols[last_index] != '+' or symbols[next_index] != '+':
                    # Found a letter that is not enclosed in two '+'
                    return False
        return True
    
  • Sử dụng các biểu thức chính quy để đếm sự xuất hiện của cả hai biểu tượng không có trong

    def SimpleSymbols[str]:
        letters = []
        results = []
    
        for i in range[len[str]]:
            if str[i] != '=' and str[i] != '+':
                letters.append[str[i]]
    
                if i < len[str]-1 and str[i-1] == '+' and str[i+1] == '+':
                    results.append[str[i]]
    
        return len[letters] == len[results]
    
    print SimpleSymbols["=+b+a"]
    
    9 và các biểu tượng trong cùng một nhóm được đặt trong vòng hai
    def SimpleSymbols[str]:
        letters = []
        results = []
    
        for i in range[len[str]]:
            if str[i] != '=' and str[i] != '+':
                letters.append[str[i]]
            try:
                if str[i-1] == '+' and str[i+1] == '+':
                    results.append[str[i]]
            except IndexError:
                do what needs to be done in this case.
    
        return len[letters] == len[results]
    
    0:

    import re
    
    def SimpleSymbols[symbols]:
        simples = len[re.findall[r'[^+=]', symbols]]
        enclosed = len[re.findall[r'\+[^+=]\+', symbols]]
    
        return simples == enclosed
    

Đã trả lời ngày 12 tháng 9 năm 2015 lúc 8:28Sep 12, 2015 at 8:28

Bài Viết Liên Quan

Chủ Đề