如何從 python 腳本中的 bash 終端的用戶輸入中獲取兩個大小相等的串列的元組?假設我們希望程式注冊以下元組
([0.001, 0.01, 0.1, 1], [1000, 100, 10, 1])
抓住它們的唯一條件是成對的兩個:0.001、1000,然后是 0.01、100 等等。
解釋
代碼首先取 0.001 和 1000
然后需要 0.01 和 100 秒
然后需要 0.1 和 10 的三分之一
最后需要1和1。
一旦它采取了所有這些,它將把它們安排在所述元組中。
uj5u.com熱心網友回復:
您可以使用回圈來獲取輸入。
lst1 = list()
lst2 = list()
for i in range(4):
lst1.append(input("input 1 :"))
lst2.append(input("input 2 :"))
tup = (lst1, lst2)
uj5u.com熱心網友回復:
N = 4
# user input in this way:
# 1 2
x = [tuple(map(lambda x: int(x), input().split())) for _ in range(N)]
# user input in this way:
# 1
# 2
# x = [(int(input()), int(input())) for _ in range(N)]
如果您只想提取輸入:
x, y = ([0.001, 0.01, 0.1, 1], [1000, 100, 10, 1])
print(tuple(zip(x,y)))
# ((0.001, 1000), (0.01, 100), (0.1, 10), (1, 1))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/473599.html
標籤:python-3.x 列表 输入 元组 用户输入
