Hướng dẫn join alternative python - tham gia python thay thế

4

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.

Giả sử tôi có một chuỗi string = 'abcdefghi' và tôi muốn đầu ra là 'a-b-c-d-e-f-g-h-i' tôi có thể dễ dàng sử dụng ____10 và nhận đầu ra cần thiết. Nhưng điều gì sẽ xảy ra nếu tôi muốn làm điều tương tự bằng cách sử dụng Regex? Làm thế nào tôi sẽ làm điều tương tự bằng cách sử dụng regex?

Tôi đang hỏi vì tôi đang học cách sử dụng Regex và muốn biết cách suy nghĩ trong đó.

Bhargav Rao

47.5K27 Huy hiệu vàng122 Huy hiệu bạc137 Huy hiệu đồng27 gold badges122 silver badges137 bronze badges

Hỏi ngày 13 tháng 12 năm 2014 lúc 10:45Dec 13, 2014 at 10:45

0

Một giải pháp sử dụng nhìn xung quanh sẽ

>>> import re
>>> str="abcdefghi"
>>> re.sub[r'[? I am implementing binary search tree in Python 3, but I wasn't able to meet my goal.
Here is code:

class Node[object]:

    def __init__[self, key=None, data=None]:
        self.key = key
        self.data = data

class BinarySearchTree[object]:
    keyfunc = None  # Will it be worse when using lambda x: x as default?
    
    def __init__[self, node=None]:
        self.root = node
        self.left = None
        self.right = None
        # I don't want default to be NoneType, but don't know how for now.

    def add[self, key, data=None]:
        node = Node[key, data]
        if self.root is None:
            self.root = node
            return
        parent = self.root.key
        if self.keyfunc is None:
            if key < parent:
                if self.left is None:
                    self.left = __class__[node]
                else:
                    self.left.add[key, data]
                        
            elif key > parent:
                if self.right is None:
                    self.right = __class__[node]
                else:
                    self.right.add[key, data]
        else:
            if keyfunc[key] < keyfunc[parent]:
                if self.left is None:
                    self.left = __class__[node]
                else:
                    self.left.add[key, data]
            elif keyfunc[key] > keyfunc[parent]:
                if self.right is None:
                    self.right = __class__[node]
                else:
                    self.right.add[key, data]
    def inorder[self]:
        if self.root:
            if self.left:
                self.left.inorder[]
            print[self.root.key, end=' ']
            if self.right:
                self.right.inorder[]

if __name__ == '__main__':
    bst1 = BinarySearchTree[]
    arr = [2,6,4,3,2,7,8,1,9,5]
    for key in arr:
        bst1.add[key]
    bst1.inorder[]

Dù sao nó cũng hoạt động, nhưng điều tôi muốn là:

  • Ngay cả khi nút gốc không có con, tôi muốn loại ____ 1 [hoặc
    >>> string = 'abcdefghi'
    >>> import re
    >>> re.sub[r'\B', '-', string]
    'a-b-c-d-e-f-g-h-i'
    
    8] luôn luôn là nhị phân. Bằng cách đó, tôi có thể cho phép cây null được xử lý phù hợp với các cây khác, và tôi cũng có thể loại bỏ lặp lại
    >>> string = 'abcdefghi'
    >>> import re
    >>> re.sub[r'\B', '-', string]
    'a-b-c-d-e-f-g-h-i'
    
    9 trong add []. That way, I can allow null tree treated in consistency with other trees, and also can I remove repeating
    >>> string = 'abcdefghi'
    >>> import re
    >>> re.sub[r'\B', '-', string]
    'a-b-c-d-e-f-g-h-i'
    
    9 in add[].
    That way, I can allow null tree treated in consistency with other trees, and also can I remove repeating
    >>> string = 'abcdefghi'
    >>> import re
    >>> re.sub[r'\B', '-', string]
    'a-b-c-d-e-f-g-h-i'
    
    9 in add[].
  • Tôi không muốn làm thủ công
    >>> re.sub[r'[?

Bài Viết Liên Quan

Chủ Đề