简体   繁体   English

如何遍历每个列表项的列表评估第一个字符

[英]How to loop through list evalute first char of each item of list

Problem: How do I loop through a list, look at the first char in each list item. 问题:如何遍历列表,查看每个列表项中的第一个字符。 If the first char = "0" then remove the "0" then look at next item in list for same evaluation. 如果第一个char = "0"则删除“0”,然后查看列表中的下一个项目以进行相同的评估。 else: leave it as is. 否则:保持原样。

Code thus far: 代码到目前为止:

gList = ["094G.016", "094G.019", "094G.005", "194G.015"]

for x in gList:
    lGrid[i].lstrip("0")
    print gList
else:
    pass

Desired Output: 期望的输出:

gList = ["94G.016", "94G.019", "94G.005", "194G.015"]

Research: 研究:

I can use gList.lstrip("0") to remove the zero from the first item in the list but don't know how to get it to move to the next entry and repeat the process. 我可以使用gList.lstrip("0")从列表中的第一个项目中删除零,但不知道如何让它移动到下一个条目并重复该过程。

Alternatively: I know I can use gList[0][0] to select the first list/char item but again need way for it to loop through the list. 或者:我知道我可以使用gList[0][0]来选择第一个list / char项目,但是再次需要它来遍历列表。

EDIT: Using a generator: 编辑:使用发电机:

Code thus far: 代码到目前为止:

def rem0(data):
    (x.lstrip('0') for x in lGrid)
    yield x

for i in rem0(lGrid):
    print i

Desired Output: 期望的输出:

gList = ["94G.016", "94G.019", "94G.005", "194G.015"]
>>> gList = ["094G.016", "094G.019", "094G.005", "194G.015"]
>>> [x.lstrip('0') for x in gList]
['94G.016', '94G.019', '94G.005', '194G.015']

improving your approach: with every iteration x points to the current item, so apply lstrip() on x not on gList 改进你的方法:每次迭代x都指向当前项,所以在x上应用lstrip()而不是gList

using item: 使用项目:

for x in gList:  
    x.lstrip("0") 

using index : 使用索引

for index in xrange(len(gList)):   #xrange() or(range() in python 3.x) returns a iterable which contains values from 0 to len(gList)-1 
    gList[index].lstrip("0")

Check out the beauty of list comprehensions on python docs. 查看python docs上列表推导的美妙之处。

A solution would be 一个解决方案是

Removing the item: 删除项目:

[item for item in gList if item[0] != '0']

or removing the leading '0' 或删除前导'0'

[item.lstrip('0') for item in gList]

Updated answer, since I cannot post comments. 更新的答案,因为我无法发表评论。

To fix your code: 要修复您的代码:

gList = ["094G.016", "094G.019", "094G.005", "194G.015"]

outL=[]
for x in gList:
    outL.append(x.lstrip("0"))

print outL
# prints ['94G.016', '94G.019', '94G.005', '194G.015']

As others have said, this is easier with a list comprehension: 正如其他人所说,通过列表理解,这更容易:

>>> [x.lstrip('0') for x in gList]
['94G.016', '94G.019', '94G.005', '194G.015']

Or, better still, a generator: 或者,更好的是,发电机:

>>> (x.lstrip('0') for x in gList)
<generator object <genexpr> at 0x10c27c140>
>>> list(_)
['94G.016', '94G.019', '94G.005', '194G.015']

Edit 编辑

You can do a generator two ways easily. 你可以轻松地用两种方式做一个发电机。

First is to write a function: 首先是写一个函数:

def rem0(data):
    for x in data:
       yield x.lstrip('0')

for i in rem0(gList):
   print i

The other is a gen expression right where you need it: 另一个是你需要它的gen表达式:

for i in (x.lstrip('0') for x in gList): 
   print i

Or even: 甚至:

g=(x.lstrip('0') for x in gList)
for i in g:
    ...

Both ways print your desired output. 两种方式都打印您想要的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM