Python - 索引列表中的元素

[英]Python - indexing an element in a list


I am a new Python programmer and trying some stuff out. I want to create a list of strings and loop through my list to check the size of each element, if the size is greater than 5, then I will change that string into an uppercase string. I am stuck on how to access that element. Below is some of the code that I have written. Thank you in advance!

我是一名新的Python程序員並嘗試了一些東西。我想創建一個字符串列表並循環遍歷我的列表以檢查每個元素的大小,如果大小大於5,那么我將該字符串更改為大寫字符串。我被困在如何訪問該元素。以下是我編寫的一些代碼。先謝謝你!

lis = ['heeeeellllooo','world','low','higggghhh']
for elem in list:
  if len(lis[elem]) > 5:
    list[elem].upper()

3 个解决方案

#1


5  

use a list comprehension with ternary operator:

使用三元運算符列表理解:

In [16]: lis = ['heeeeellllooo','world','low','higggghhh']

In [17]: [x.upper() if len(x)>5 else x for x in lis] # this is a new list

Out[17]: ['HEEEEELLLLOOO', 'world', 'low', 'HIGGGGHHH']

You can also use enumerate(), enumerate returns a tuple whole first item is the index of the element and the second item is the element itself.

你也可以使用enumerate(),枚舉返回一個元組,整個第一項是元素的索引,第二項是元素本身。

This method modifies the original list.

此方法修改原始列表。

In [18]: for index,elem in enumerate(lis):
   ....:     if len(elem)>5:
   ....:         lis[index]=elem.upper()  #access element by index
   ....:         

In [19]: lis
Out[19]: ['HEEEEELLLLOOO', 'world', 'low', 'HIGGGGHHH']

How are lists implemented?:

如何實施清單?:

Python’s lists are really variable-length arrays, not Lisp-style linked lists. The implementation uses a contiguous array of references to other objects, and keeps a pointer to this array and the array’s length in a list head structure.

Python的列表實際上是可變長度數組,而不是Lisp風格的鏈表。該實現使用對其他對象的連續引用數組,並在列表頭結構中保留指向此數組和數組長度的指針。

This makes indexing a list a[i] an operation whose cost is independent of the size of the list or the value of the index.

這使得列表的索引成為[i]一種操作,其成本獨立於列表的大小或索引的值。

When items are appended or inserted, the array of references is resized. Some cleverness is applied to improve the performance of appending items repeatedly; when the array must be grown, some extra space is allocated so the next few times don’t require an actual resize.

附加或插入項目時,將調整引用數組的大小。一些聰明的東西被用來改善重復追加物品的表現;當必須增長數組時,會分配一些額外的空間,因此接下來的幾次不需要實際調整大小。

#2


3  

enumerate is very useful, if you'd like to change it in place. Try this:

枚舉是非常有用的,如果你想在適當的位置更改它。嘗試這個:

for idx, elem in enumerate(lis):
    if len(elem) > 5:
        list[idx] = elem.upper()

#3


0  

Here is the correct way to do what you were trying using a for loop. Your code used two different variable names for the list: lis and list. The variable elem is the item in the list, not the index. To get the index, you need to use enumerate(). The solution shown by Ashwini Chaudhary is called a list comprehension and is a much better way to do this.

以下是使用for循環執行操作的正確方法。您的代碼為列表使用了兩個不同的變量名稱:lis和list。變量elem是列表中的項目,而不是索引。要獲取索引,您需要使用enumerate()。 Ashwini Chaudhary所展示的解決方案被稱為列表理解,是一種更好的方法。

for i, elem in enumerate(lis):
  if len(elem) > 5:
    lis[i] = elem.upper()

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/01/10/73021c0667e9570c858d71224fe18bf5.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com