在 Python 中連接數組的主要問題是數組將在末尾被截斷。
arrays whitespace join I have an array of strings that I want to join together, but preserve the white space between each string. For example: ['a', 'b', 'c'] -> 'a b c' I tried using .join(array), but it removes all the white space. How can I preserve the white space? I have an array of strings that I want to join together, but preserve the white space between each string. For example:['a', 'b', 'c'] -> 'a b c'I tried using .join(array), but it removes all the white space. How can I preserve the white space?
數組
數組是存儲在連續內存位置的項的集合。 可以使用 list() 函數創建數組,並可以使用 index() 函數訪問它們。
使用數組的技巧
在 Python 中使用數組時,需要牢記一些提示。 首先,訪問數組中的元素時始終使用數組索引表示法。 例如,而不是像這樣寫出數組中的整個元素列表:
列表 = [1, 2, 3]
你可以簡單地寫:
列表[0] = 4
這會將值 4 分配給列表中的第一個元素,即索引 0。其次,重要的是要記住數組是從零開始的。 這意味著數組中的第一個元素位於索引 0,第二個元素位於索引 1。第三,記住數組是可變的也很重要。 這意味著您可以使用適當的方法(例如 list[0] = 4)修改數組的內容。 最後,重要的是要注意數組是序列數據類型。 這意味著它們支持順序操作(如 list[0]、list[1]、...)以及索引操作(如 list[0]、list[1]、...)。