【问题标题】:How to assign values to a list如何为列表赋值
【发布时间】:2016-01-01 05:05:53
【问题描述】:

十进制数字系统由十位数字组成,我们将其表示为“0123456789”。编写一个程序,将这些值归因于给定列表的元素

L = ['f','c','e','a','d','p','z','y','k','m'].

例如

Input is fff
Output is000

注意:代码不应该有'for'! 输入是'ccc' 输出为 111 输入是'eeeee' 输出为 22222

我想在python notebook(python 2.7)上为这个程序写一段代码!但我没有这样做。请帮帮我!

【问题讨论】:

  • 您能展示一下您尝试过的代码吗?另外,请明确指出您的代码在哪里给您带来问题。
  • 这台笔记本电脑里没有密码!

标签: python string list python-2.7 loops


【解决方案1】:

使用index()函数:

>>> x = 'fff'
>>> print ''.join(str(L.index(el)) for el in x)
000

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-28
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-06
    • 2018-06-01
    相关资源
    最近更新 更多