小编Rel*_*der的帖子

有人可以解释这个词典行为吗?

>>> data = "0:1:2"
>>> h2 = data[0]
>>> a = {0: "... ", 1: "..- ", 2: ".-."}
>>> print (0 in a)
True
>>> print (h2)
0
>>> print (h2 in a)
False
>>> print (a.keys())
dict_keys([0, 1, 2])
Run Code Online (Sandbox Code Playgroud)

别名出了什么问题?

python dictionary python-3.x

2
推荐指数
1
解决办法
55
查看次数

我应该如何使用list comprehension重写这个map()示例?

>>> a = [1,2,3,4]
>>> b = [17,12,11,10]
>>> c = [-1,-4,5,9]

>>> list(map(lambda x,y,z:x+y+z, a,b,c))
[17, 10, 19, 23]
Run Code Online (Sandbox Code Playgroud)

尝试:

>>>[(x+y+z) for x in a for y in b for z in c]
Run Code Online (Sandbox Code Playgroud)

但是添加了这些列表中每个可能的元素组合(不仅仅是先加第一,第二和第二等):

[17, 14, 23, 27, 12, 9, 18, 22, 11, 8, 17, 21, 10, 7, 16, 20, 18, 15, 24, 28, 13, 10, 19, 23, 12, 9, 18, 22, 11, 8, 17, 21, 19, 16, 25, 29, 14, 11, 20, 24, 13, 10, 19, 23, 12, …
Run Code Online (Sandbox Code Playgroud)

python list-comprehension map python-3.x

2
推荐指数
1
解决办法
64
查看次数

在Python中解析多行

我正在学习Python,我需要编写一个程序来确定一首诗中出现次数最多的单词.困扰我的问题是将一首诗的一行解析成包含该诗的单词的单一列表.当我解决它时,我将毫不费力地确定出现次数最多的单词.

我可以通过重复调用input()来访问诗的行,最后一行包含三个字符###.

所以,我写道:

while True:
   y = input()
   if y == "###":
     break
   y = y.lower()
   y = y.split()
Run Code Online (Sandbox Code Playgroud)

并输入:

Here is a line like sparkling wine
Line up now behind the cow
###
Run Code Online (Sandbox Code Playgroud)

得到的结果:

['here', 'is', 'a', 'line', 'like', 'sparkling', 'wine']
['line', 'up', 'now', 'behind', 'the', 'cow']
Run Code Online (Sandbox Code Playgroud)

如果我试着打电话给y [0],我得到:

here
line
Run Code Online (Sandbox Code Playgroud)

如何在同一个变量中连接两个列表,或者如何将每一行分配给不同的变量?

任何提示都表示赞赏.谢谢.

python parsing python-3.x

1
推荐指数
1
解决办法
1335
查看次数