Python 如何迭代字符串列表中的每个字符串并对其进行操作';s元素
我是python新手,我需要一些帮助 任务:给定一个列表-->Python 如何迭代字符串列表中的每个字符串并对其进行操作';s元素,python,string,list,Python,String,List,我是python新手,我需要一些帮助 任务:给定一个列表-->words=['aba','xyz','xgx','dssd','sdjh'] 我需要比较列表中每个字符串的第一个和最后一个元素, 如果字符串中的第一个和最后一个元素相同,则递增计数 给出的列表是: words = ['aba', 'xyz', 'xgx', 'dssd', 'sdjh'] 如果我手动尝试,我可以迭代列表中字符串的每个元素 words = ['aba', 'xyz', 'xgx', 'dssd', 'sdjh'] w
words=['aba','xyz','xgx','dssd','sdjh']
我需要比较列表中每个字符串的第一个和最后一个元素,
如果字符串中的第一个和最后一个元素相同,则递增计数
给出的列表是:
words = ['aba', 'xyz', 'xgx', 'dssd', 'sdjh']
如果我手动尝试,我可以迭代列表中字符串的每个元素
words = ['aba', 'xyz', 'xgx', 'dssd', 'sdjh']
w1 = words[0]
print w1
aba
for i in w1:
print i
a
b
a
if w1[0] == w1[len(w1) - 1]:
c += 1
print c
1
但是,当我尝试使用FOR循环迭代列表中所有字符串的所有元素时
我犯了一个错误
words = ['aba', 'xyz', 'xgx', 'dssd', 'sdjh']
c = 0
for i in words:
w1 = words[i]
if w1[0] == w1[len(w1) - 1]:
c += 1
print c
错误:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: list indices must be integers, not str
回溯(最近一次呼叫最后一次):
文件“”,第2行,在
TypeError:列表索引必须是整数,而不是str
请让我知道,如何比较列表中编号字符串的第一个和最后一个元素
words = ['aba', 'xyz', 'xgx', 'dssd', 'sdjh']
w1 = words[0]
print w1
aba
for i in w1:
print i
a
b
a
if w1[0] == w1[len(w1) - 1]:
c += 1
print c
1
提前感谢。试试:
for word in words:
if word[0] == word[-1]:
c += 1
print c
for words in words
返回words
的项目,而不是索引。如果您需要索引,请尝试使用enumerate
:
for idx, word in enumerate(words):
print idx, word
将输出
0, 'aba'
1, 'xyz'
etc.
上面word[-1]
中的-1
是Python表示“最后一个元素”的方式word[-2]
将为您提供最后一个元素,依此类推
您也可以使用生成器来实现这一点
c = sum(1 for word in words if word[0] == word[-1])
改为使用range(),如下所示:
for i in range(len(words)):
...
原因是在第二个示例中,
i
是单词本身,而不是单词的索引。所以
for w1 in words:
if w1[0] == w1[len(w1) - 1]:
c += 1
print c
使用
range(len())
等同于使用enumerate()
的建议是不正确的。它们返回相同的结果,但它们不相同
使用enumerate()
实际上会提供键/值对。使用range(len())
不起作用
让我们先检查范围(len())
(从原始海报的示例开始):
这给了我们一个简单的列表:
[0, 1, 2, 3, 4]
。。。这个列表中的元素在我们的结果中充当“索引”
因此,让我们对我们的enumerate()
版本执行同样的操作:
words = ['aba', 'xyz', 'xgx', 'dssd', 'sdjh']
print enumerate(words)
这当然没有给我们一个列表:
<enumerate object at 0x7f6be7f32c30>
它给了我们:
[(0, 'aba'), (1, 'xyz'), (2, 'xgx'), (3, 'dssd'), (4, 'sdjh')]
这些是实际的键/值对
所以这个
words = ['aba', 'xyz', 'xgx', 'dssd', 'sdjh']
for i in range(len(words)):
print "words[{}] = ".format(i), words[i]
。。。实际上,获取第一个列表(单词),并创建第二个简单的列表,范围由第一个列表的长度指示
所以我们有两个简单的列表,我们只是从每个列表中打印一个元素,以获得所谓的“键/值”对
但它们不是真正的键/值对;它们只是来自不同列表的两个单一元素同时打印
而枚举()
代码:
for i, word in enumerate(words):
print "words[{}] = {}".format(i, word)
。。。还将创建第二个列表。但这个列表实际上是一个键/值对列表,我们从一个源中获取每个键和值,而不是从两个列表中获取(就像我们上面所做的那样)
因此,我们打印相同的结果,但来源完全不同——处理方式也完全不同。下面的代码输出第一个字母和最后一个字母相等的单词数。使用以下工具进行测试和验证: 输出:
$python main.py
3
您正在用Word迭代项,但必须迭代项的长度:
words = ['aba', 'xyz', 'xgx', 'dssd', 'sdjh']
c = 0
for i in range(len(words)):
w1 = words[i]
if w1[0] == w1[len(w1) - 1]:
c += 1
print (c)
在您的情况下,i[0]是“aba”,因为i是根据文字中的项目计算的:
words = ['aba', 'xyz', 'xgx', 'dssd', 'sdjh']
c = 0
for i in words:
print(i)
输出为:
阿巴
对列表进行迭代会返回其元素而不是索引,因此会出现错误。您的Python one liner可以执行您想要的操作(但不能解决或解释错误),它看起来像:
c=sum([1如果w[0]==w[-1],则0表示w的单词)。
。但是,在你开始掌握列表理解的诀窍之前,这并不能真正帮助你(更简短的是:c=sum([int(w[0]==w[-1]),对于文字中的w])
。@无论如何,你可以放弃int
调用和[]
。-)@AshwiniChaudhary嗯,我不喜欢这样求布尔数的和,但我想隐式转换在这里是有效的。我听之任之,否则你的评论就没有意义了;-)。你能解释更多关于你的代码的细节吗?嗨,宁静,基本上,如果第一个单词和最后一个单词是相同的,那么代码就是递增的。这并不能回答这个问题。一旦你有足够的钱,你将能够;相反-
c=0
words = ['challa','reddy','challa']
for idx, word in enumerate(words):
if idx==0:
firstword=word
print(firstword)
elif idx == len(words)-1:
lastword=word
print(lastword)
if firstword==lastword:
c=c+1
print(c)
for i,j in enumerate(words): # i---index of word----j
#now you got index of your words (present in i)
print(i)
words = ['aba', 'xyz', 'xgx', 'dssd', 'sdjh']
c = 0
for i in range(len(words)):
w1 = words[i]
if w1[0] == w1[len(w1) - 1]:
c += 1
print (c)
words = ['aba', 'xyz', 'xgx', 'dssd', 'sdjh']
c = 0
for i in words:
print(i)