我可以修改列表中指定的单个值吗?

无功功率01

我需要能够打印一个列表,其中我将列表中的所需值标记为“ XX ”,即从“XX”开始。我的问题是我使用了一个嵌套列表并且不知道我应该如何定位某个值。我也不知道我是否能够将整数更改为字符串,或者这是否会弄乱整个列表的打印方式。我的“标记”值的函数如下所示:

  i = 0
  while i <= len(chosenseat):
    findseat = field.index(chosenseat[i])
    field[findseat] = "*"+chosenseat[i]+"*"
    for idx, row in enumerate(field):
        if idx == floor(len(field) / 2):
            print("{:^{}}".format('↓ TYST AVD ↓', (len(row) * 4) - 2))
        if idx % 2 == 1:
            row = row[::-1]
        print(("{:<4}" * len(row)).format(*row))
    i += 1

(我在 StackOverflow 上从 Hampus Larsson 那里得到了上面的大部分代码!)

整个程序是一个火车车厢的订票系统,我希望能够在预订后用“**”标记所需的座位。打印列表时,它看起来像:

  1   2   3   4   
  8   7   6   5   
  9   10  11  12  
   ↓ TYST AVD ↓ 
  16  15  14  13 
  17  18  19  20  
  24  23  22  21 

然后,如果用户例如想要座位 19 和 20,则列表应打印为:

 1   2   3   4   
 8   7   6   5   
 9   10  11  12  
  ↓ TYST AVD ↓ 
 16  15  14  13  
 17  18 *19**20*  
 24  23  22  21

我遇到的另一个问题是,在解开门票时,应该删除“**”,但我目前甚至无法将星星固定到位。有人能在这方面指导我吗?

数字信号处理器

我试图了解您的初始列表是什么,假设我做对了,这可能是一个解决方案:

import copy

seatList = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20]]
chosenseat = [11, 12]

def find_in2Dlist(lst, item):
    for index, sublist in enumerate(lst):
        try:
            idx = sublist.index(item)
            return [index, idx]
        except: continue
    return None
            

def printList(mlist, chosenseat):
  i = 0
  temp = copy.deepcopy(mlist)
  while i < len(chosenseat):
      findseat = find_in2Dlist(mlist, chosenseat[i])
      if findseat == None:
          i += 1
      else:
          temp[findseat[0]][findseat[1]] = '*' + str(mlist[findseat[0]][findseat[1]]) + '*'
      i += 1
  
  for idx, row in enumerate(temp):
        if idx == len(temp) // 2:
            print("{:^{}}".format('↓ TYST AVD ↓', (len(row) * 4) - 2))
        if idx % 2 == 1:
            row = row[::-1]
        print(("{:<4}" * len(row)).format(*row))
   
def printInitialList(mlist):
    print(mList)

printList(seatList, chosenseat) #=>
# 1   2   3   4   
# 8   7   6   5   
# 9   10  11  12  
#  ↓ TYST AVD ↓ 
# 16  15  14  13  
# 17  18  *19**20*
# 24  23  22  21 

编辑:因为printList使用了初始列表的副本,所以这个保持不变并且可以重复使用:

print(seatList) #=>
# 1   2   3   4   
# 8   7   6   5   
# 9   10  11  12  
#  ↓ TYST AVD ↓ 
# 16  15  14  13  
# 17  18  19  20
# 24  23  22  21 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我可以在其中指定--no-colors等默认值的.mocha文件吗?

我可以在TypeScript中指定值类型,但仍然让TS推断键类型吗?

我可以在转换中指定transform属性吗?

我可以在node.js中指定RabbitMQ凭证吗?

我可以在方法签名中指定鸭子类型吗?

我可以在 Django 模型中指定连接策略吗?

我可以在源自某个值的 django 模型中指定一个上传文件夹吗?

我可以在gradle中指定我只想使用某个插件进行测试吗?

我可以根据我所在的分支(在 Dockerfile 中指定)更改数据库吗?

在 Python 中,我可以遍历单个值吗?

我可以直接在Swagger 2.0中指定响应吗?

我可以在lex代码中指定模式匹配优先级吗?

我们可以调用 SourceFunction#collectWithTimestamp 摄取时间中指定的时间戳吗

自动套用:我可以在架构中指定选项助手吗?

我可以在配置文件中指定ninja.port吗?

我可以在Gemfile中指定需要的Git存储库的子路径吗?

我可以在关键帧中指定百分比吗?

我可以在节点js文件中指定es6吗

我可以在matplotlib中指定文本框的右下角吗?

我可以在python-eve中指定位置并同时聚合吗?

我可以在cfscript中将文件写入文件中指定文件权限的Linux目录吗?

我可以在 backdraftjs 的复合组件中指定子组件的位置吗?

我可以在Jinja2模板中指定SQLAlchemy外部关系的顺序吗?

我可以安装频率低于处理器系统内存规范中指定频率的RAM吗?

我可以使用硒网格在node.json中指定webdriver.gecko.driver吗?

我可以在Python的Plotly Heatmap中指定单元格的宽度和高度吗?

我可以允许用户在SSRS tablix中指定静态列的顺序吗?

CustomerIO 我可以在每个 API 请求中指定 SMS 文本吗?

我可以查找指定页面的路线列表吗?