Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中附加整数_Python_Python 3.x - Fatal编程技术网

如何在python中附加整数

如何在python中附加整数,python,python-3.x,Python,Python 3.x,我有这个阵列: listPatients = [[ "Johnson", "Fred", "N", "2763 Filibuster Drive", "Lakeland", "FL", "37643", "Q", "05/27/1935", "164-55-0726", "N"]] \ + [[ "Williams", "Betty", "L", "701 Collage Avenue", "Orlando", "FL", "31234",

我有这个阵列:

listPatients =   [[  "Johnson",   "Fred", "N", "2763 Filibuster Drive",  "Lakeland", "FL", "37643", "Q", "05/27/1935", "164-55-0726", "N"]] \
+                [[ "Williams",  "Betty", "L",    "701 Collage Avenue",   "Orlando", "FL", "31234", "F", "11/27/1971", "948-44-1038", "Y"]] \
+                [[     "Ling", "Hector", "X",     "1500 Raceway Lane",     "Tampa", "FL", "32785", "M", "10/17/2003", "193-74-0274", "Y"]] \
+                [[    "Albin",   "Ross", "L",      "207 Daisy Avenue",  "Lakeland", "FL", "32643", "M", "12/08/1990", "458-57-2867", "N"]] \
+                [[ "Anderson",  "Jason", "O",       "1527 Lewis Road",     "Tampa", "FL", "32785", "M", "11/25/1991", "093-50-1093", "Y"]] \
+                [[     "Baca",  "Edwin", "L",       "25 Hunters Lane",  "Lakeland", "FL", "32643", "M", "10/30/1992", "159-56-9731", "Y"]] \
+                [[   "Birner", "Dalton", "M",     "851 Applebe Court",   "Orlando", "FL", "31234", "M", "09/22/1993", "695-21-2340", "Y"]] \
+                [["Dominguez", "Javier", "B",   "1410 Waterford Blvd",   "Orlando", "FL", "31234", "M", "08/04/1994", "753-66-6482", "N"]] \
+                [[   "Aimino", "Nicolo", "S",      "2379 Runners Way",  "Lakeland", "FL", "32643", "M", "07/11/1995", "852-73-4196", "Y"]] \
+                [["Armstrong","Addison", "T",    "46 Hawthorne Drive",  "Lakeland", "FL", "32643", "M", "06/18/1996", "648-81-1456", "Y"]] \
+                [[    "Beard",    "Ian", "J",  "1814 Constitution Ct",   "Orlando", "FL", "31234", "M", "05/28/1997", "879-61-1829", "N"]] \
+                [[ "Calderon",  "Yamil", "C",         "345 Cigar Row",     "Tampa", "FL", "32785", "M", "04/07/1998", "123-87-6431", "Y"]] \
+                [[   "Carter", "Thomas", "P",       "896 Pine Avenue",        "Tampa", "FL", "32785", "M", "03/12/1999", "248-65-3197", "Y"]] \
+                [[  "Chaname",  "Bryan", "D",    "24 Blue Belt Drive",           "Lakeland", "FL", "32643", "M", "02/23/2000", "741-85-9632", "Y"]] \
+                [[   "Chaney", "Chaney", "Z",    "2589 College Court",     
"Orlando", "FL", "31234", "M", "01/15/2001", "963-25-7418", "Y"]]
我想加上年龄;这是取出生年份(指数(数组(元素[7]))并从2015年减去后的计算:

for patients in range(len(listPatients)):
    YOB=listPatients[patients][8][6]+listPatients[patients][8][7]
    +listPatients[patients][8][8]+listPatients[patients][8][9]
age= currentYear-int(YOB)
story4=patients.append(age)

输出显示我不能附加整数。我能做什么?

在您的代码片段中,
患者
是一个
int
,它没有
附加
方法

>>> range(len(['x','y','z']))
[0, 1, 2]

范围迭代器中的每个项目都将是一个整数。

在您的代码中,
患者
是列表列表中的索引,而不是子列表本身,即使用
患者。追加(年龄)
您正试图追加到一个数字!相反,请尝试迭代
列表患者
本身。此外,您可以使用切片
[6:://code>而不是将年份的所有单个字符串联起来,或者使用
患者[8].split('/')[2]
来获取年份

for patients in listPatients:
    YOB = patients[8][6:] # or patients[8].split('/')[2]
    age = currentYear - int(YOB)
    patients.append(age)
另外,
append
不会返回任何内容,因此无论
story4
应该是什么,它都将始终是
None

最后,您可以这样编写多行列表:

listPatients =   [[  "Johnson",  ... stuff ..., "164-55-0726", "N"],
                  ... many more lines ...
                  [   "Chaney",  ... stuff ..., "963-25-7418", "Y"]]

这里没有理由使用
范围
进行迭代

for patient in listPatients:
    YOB=patient[8][-4:]
    age= currentYear-int(YOB)
    patient.append(age)
    story4=age

您希望显示listPatients元素,而不是其范围:

for patients in range(len(listPatients)):
    YOB=listPatients[patients][8][6]+listPatients[patients][8][7]+listPatients[patients][8][8]+listPatients[patients][8][9]
    age= currentYear-int(YOB)
    story4=listPatients[patients].append(age)

你能添加完整的回溯吗?我想它说,
'int'对象没有属性“append”
在这种情况下,患者不是一个列表。我希望这不是真实的患者数据。@user5402其中一些地址不存在,所以可能不存在。我应该字符串(患者)吗@C.B.@JoshuaPilkin
story4
应该是什么?story4的输出将是我患者的年龄@为什么要投否决票?我自己在OPs数据上运行了这段代码,它运行得非常好。不管谁投了反对票,想解释一下吗?我真的很想让投反对票的人解释一下。这是您可以对代码进行的最简单的更改,以使其正常工作。以最小的努力获得最大的结果。