说我有string = "Nobody will give me pancakes anymore"
我想计算字符串中的每个单词。因此,我string.split()
获取列表以获取['Nobody', 'will', 'give', 'me', 'pancakes', 'anymore']
。
但是当我想通过输入'Nobody'
知道len(string[0])
的长度时,它只给我1
,因为它认为第0个元素只是'N'
而不是'Nobody'
。
我需要做些什么来确保我能找到整个单词的长度,而不是单个元素?
答案 0 :(得分:4)
您取了string[0]
的第一个字母,忽略了string.split()
来电的结果。
存储拆分结果;这是一个包含单个词的列表:
words = string.split()
first_worth_length = len(words[0])
演示:
>>> string = "Nobody will give me pancakes anymore"
>>> words = string.split()
>>> words[0]
'Nobody'
>>> len(words[0])
6
答案 1 :(得分:1)
是的,string[0]
只是'N'
关于你的陈述......
我想计算字符串中的每个单词
>>> s = "Nobody will give me pancakes anymore"
>>> lens = map(lambda x: len(x), s.split())
>>> print lens
[6, 4, 4, 2, 8, 7]
那么,你可以做lens[0]
答案 2 :(得分:0)
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="smartinvestPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>SmartInvest</jta-data-source>
<properties />
</persistence-unit>
</persistence>