所以我有一个导入.txt文件的代码,并使用line.split()
将其列组织到一个列表中基本上是在表格中 list1 =(1,2,3,4,5,6,7,8)
list1中的最后一个值是我用
创建的新列表total = list1[1]+list1[2]+list1[3]+list1[4]+list1[5]
Info = []
if total > 650:
info.append(1.0)
if total < 650:
info.append(2.0)
然后我将其添加到list1的末尾
所以打印list1给了我
(&#39; 031&#39;,&#39; 97&#39;,&#39; 108&#39;,&#39; 113&#39;,&#39; 48&#39;,& #39; 217&#39;,&#39; 14&#39;,[2.0])
(&#39; 032&#39;,&#39; 97&#39;,&#39; 124&#39;,&#39; 147&#39;,&#39; 45&#39;,& #39; 355&#39;,&#39; 15&#39;,[1.0])
(&#39; 033&#39;,&#39; 140&#39;,&#39; 145&#39;,&#39; 175&#39;,&#39; 50&#39;,& #39; 446&#39;,&#39; 14&#39;,[1.0])
(&#39; 034&#39;,&#39; 133&#39;,&#39; 123&#39;,&#39; 115&#39;,&#39; 46&#39;,& #39; 430&#39;,&#39; 15&#39;,[1.0])
(&#39; 035&#39;,&#39; 107&#39;,&#39; 92&#39;,&#39; 136&#39;,&#39; 45&#39;,& #39; 278&#39;,&#39; 13&#39;,[1.0])
(&#39; 036&#39;,&#39; 98&#39;,&#39; 115&#39;,&#39; 130&#39;,&#39; 37&#39;,& #39; 387&#39;,&#39; 15&#39;,[1.0])
(&#39; 037&#39;,&#39; 117&#39;,&#39; 69&#39;,&#39; 131&#39;,&#39; 34&#39;,& #39; 238&#39;,&#39; 12&#39;,[2.0])
(&#39; 038&#39;,&#39; 134&#39;,&#39; 125&#39;,&#39; 132&#39;,&#39; 50&#39;,& #39; 434&#39;,&#39; 15&#39;,[1.0])
(&#39; 039&#39;,&#39; 125&#39;,&#39; 116&#39;,&#39; 178&#39;,&#39; 50&#39;,& #39; 433&#39;,&#39; 15&#39;,[1.0])
(&#39; 040&#39;,&#39; 125&#39;,&#39; 142&#39;,&#39; 156&#39;,&#39; 50&#39;,& #39; 363&#39;,&#39; 15&#39;,[1.0])
(&#39; 041&#39;,&#39; 77&#39;,&#39; 51&#39;,&#39; 68&#39;,&#39; 45&#39;,& #39; 219&#39;,&#39; 15&#39;,[2.0])
(&#39; 042&#39;,&#39; 122&#39;,&#39; 142&#39;,&#39; 182&#39;,&#39; 50&#39;,& #39; 447&#39;,&#39; 15&#39;,[1.0])
(&#39; 043&#39;,&#39; 103&#39;,&#39; 123&#39;,&#39; 102&#39;,&#39; 46&#39;,& #39; 320&#39;,&#39; 15&#39;,[1.0])
(&#39; 044&#39;,&#39; 106&#39;,&#39; 100&#39;,&#39; 127&#39;,&#39; 50&#39;,& #39; 362&#39;,&#39; 15&#39;,[1.0])
(&#39; 045&#39;,&#39; 125&#39;,&#39; 110&#39;,&#39; 140&#39;,&#39; 50&#39;,& #39; 396&#39;,&#39; 15&#39;,[1.0])
(&#39; 046&#39;,&#39; 120&#39;,&#39; 98&#39;,&#39; 129&#39;,&#39; 48&#39;,& #39; 325&#39;,&#39; 13&#39;,[1.0])
(&#39; 047&#39;,&#39; 89&#39;,&#39; 70&#39;,&#39; 80&#39;,&#39; 46&#39;,& #39; 302&#39;,&#39; 14&#39;,[2.0])
(&#39; 048&#39;,&#39; 99&#39;,&#39; 130&#39;,&#39; 103&#39;,&#39; 50&#39;,& #39; 436&#39;,&#39; 15&#39;,[1.0])
(&#39; 049&#39;,&#39; 100&#39;,&#39; 87&#39;,&#39; 148&#39;,&#39; 17&#39;,& #39; 408&#39;,&#39; 13&#39;,[1.0])
(&#39; 050&#39;,&#39; 104&#39;,&#39; 47&#39;,&#39; 91&#39;,&#39; 37&#39;,& #39; 50&#39;,&#39; 9&#39;,[2.0])
我想知道的是如何编写一行代码,当line1 [6] = 13时,它会从line1 [7]减去.5,当它的12减去1时减去1.5,而当它的11或更低时减去1.5
任何帮助将不胜感激!
答案 0 :(得分:0)
line1[7]
是一个列表,因此我假设您要从列表中的数字中减去。这样的事情应该可以胜任:
x = line1[6]
if x <= 11:
line1[7][0] -= 1.5
elif x == 12:
line1[7][0] -= 1
elif x == 13:
line1[7][0] -= 0.5
答案 1 :(得分:0)
line1[7][0] -= 1.5 if line1[6]<=11 else 0.5*(14-line1[6]) if line1[6]<=13 else 0
答案 2 :(得分:0)
def convert_row(row):
row[7][0] -= sorted([0.5,1.5,7-int(row[6])/2])[1]
list1 = [('031', '97', '108', '113', '48', '217', '14', [2.0]),
('032', '97', '124', '147', '45', '355', '15', [1.0])]
for x in list1:
convert_row(x)
看到你想要它在“一行代码”上,我保持简短而没有其他人。基本上我计算7-int(row [6])/ 2匹配你的情况(11 12和13),然后取这个数字的中间,0.5和1.5来匹配其他情况。