Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.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_Tuples - Fatal编程技术网

Python 如何用元组列表中的百分比替换数字?

Python 如何用元组列表中的百分比替换数字?,python,tuples,Python,Tuples,我有一个元组列表,需要通过应用简单的%公式来更改元组中的值(求和所有整数并显示替换原始整数的百分比),元组的其余部分保持不变。我不知道如何提取数字并在元组中执行此操作,到目前为止只是初始代码 def tupleCounts2Percents(inputList): lst = inputList lst[0] = (9) #example print lst inputList = [('CA',100),('NY',300),('AZ',200)] tupleCou

我有一个元组列表,需要通过应用简单的%公式来更改元组中的值(求和所有整数并显示替换原始整数的百分比),元组的其余部分保持不变。我不知道如何提取数字并在元组中执行此操作,到目前为止只是初始代码

def tupleCounts2Percents(inputList):
    lst = inputList
    lst[0] = (9) #example
    print lst 

inputList = [('CA',100),('NY',300),('AZ',200)]
tupleCounts2Percents(inputList)
我需要的输出是

[('CA',0.166),('NY',0.5),('AZ',0.333)]

如果您希望就地更改列表,使更改反映在调用函数的列表中,那么您可以简单地对列表进行
enumerate()
并创建新元组,例如-

>>> def tupleCounts2Percents(inputList):
...     lst = inputList
...     tot = sum(x[1] for x in lst)
...     for i,x in enumerate(lst):
...         lst[i] = (x[0],(1.*x[1])/tot)
...     print lst
...
>>> inputList = [('CA',100),('NY',300),('AZ',200)]
>>> tupleCounts2Percents(inputList)
[('CA', 0.16666666666666666), ('NY', 0.5), ('AZ', 0.3333333333333333)]
这是一种列表理解方法-

>>> def tupleCounts2Percents(inputList):
...     lst = inputList
...     tot = sum(x[1] for x in lst)
...     lst[:] = [(x[0],(1.*x[1])/tot) for x in lst]
...     print lst
...
>>> inputList = [('CA',100),('NY',300),('AZ',200)]
>>> tupleCounts2Percents(inputList)
[('CA', 0.16666666666666666), ('NY', 0.5), ('AZ', 0.3333333333333333)]

如果不希望列表就地更改,请使用
lst
代替
lst[:]
,例如-

>>> def tupleCounts2Percents(inputList):
...     lst = inputList
...     tot = sum(x[1] for x in lst)
...     lst = [(x[0],(1.*x[1])/tot) for x in lst]
...     print lst
...
>>> inputList = [('CA',100),('NY',300),('AZ',200)]
>>> tupleCounts2Percents(inputList)
[('CA', 0.16666666666666666), ('NY', 0.5), ('AZ', 0.3333333333333333)]
>>> inputList
[('CA', 100), ('NY', 300), ('AZ', 200)]

如果您希望就地更改列表,使更改反映在调用函数的列表中,那么您可以简单地对列表进行
enumerate()
并创建新元组,例如-

>>> def tupleCounts2Percents(inputList):
...     lst = inputList
...     tot = sum(x[1] for x in lst)
...     for i,x in enumerate(lst):
...         lst[i] = (x[0],(1.*x[1])/tot)
...     print lst
...
>>> inputList = [('CA',100),('NY',300),('AZ',200)]
>>> tupleCounts2Percents(inputList)
[('CA', 0.16666666666666666), ('NY', 0.5), ('AZ', 0.3333333333333333)]
这是一种列表理解方法-

>>> def tupleCounts2Percents(inputList):
...     lst = inputList
...     tot = sum(x[1] for x in lst)
...     lst[:] = [(x[0],(1.*x[1])/tot) for x in lst]
...     print lst
...
>>> inputList = [('CA',100),('NY',300),('AZ',200)]
>>> tupleCounts2Percents(inputList)
[('CA', 0.16666666666666666), ('NY', 0.5), ('AZ', 0.3333333333333333)]

如果不希望列表就地更改,请使用
lst
代替
lst[:]
,例如-

>>> def tupleCounts2Percents(inputList):
...     lst = inputList
...     tot = sum(x[1] for x in lst)
...     lst = [(x[0],(1.*x[1])/tot) for x in lst]
...     print lst
...
>>> inputList = [('CA',100),('NY',300),('AZ',200)]
>>> tupleCounts2Percents(inputList)
[('CA', 0.16666666666666666), ('NY', 0.5), ('AZ', 0.3333333333333333)]
>>> inputList
[('CA', 100), ('NY', 300), ('AZ', 200)]

如果您希望就地更改列表,使更改反映在调用函数的列表中,那么您可以简单地对列表进行
enumerate()
并创建新元组,例如-

>>> def tupleCounts2Percents(inputList):
...     lst = inputList
...     tot = sum(x[1] for x in lst)
...     for i,x in enumerate(lst):
...         lst[i] = (x[0],(1.*x[1])/tot)
...     print lst
...
>>> inputList = [('CA',100),('NY',300),('AZ',200)]
>>> tupleCounts2Percents(inputList)
[('CA', 0.16666666666666666), ('NY', 0.5), ('AZ', 0.3333333333333333)]
这是一种列表理解方法-

>>> def tupleCounts2Percents(inputList):
...     lst = inputList
...     tot = sum(x[1] for x in lst)
...     lst[:] = [(x[0],(1.*x[1])/tot) for x in lst]
...     print lst
...
>>> inputList = [('CA',100),('NY',300),('AZ',200)]
>>> tupleCounts2Percents(inputList)
[('CA', 0.16666666666666666), ('NY', 0.5), ('AZ', 0.3333333333333333)]

如果不希望列表就地更改,请使用
lst
代替
lst[:]
,例如-

>>> def tupleCounts2Percents(inputList):
...     lst = inputList
...     tot = sum(x[1] for x in lst)
...     lst = [(x[0],(1.*x[1])/tot) for x in lst]
...     print lst
...
>>> inputList = [('CA',100),('NY',300),('AZ',200)]
>>> tupleCounts2Percents(inputList)
[('CA', 0.16666666666666666), ('NY', 0.5), ('AZ', 0.3333333333333333)]
>>> inputList
[('CA', 100), ('NY', 300), ('AZ', 200)]

如果您希望就地更改列表,使更改反映在调用函数的列表中,那么您可以简单地对列表进行
enumerate()
并创建新元组,例如-

>>> def tupleCounts2Percents(inputList):
...     lst = inputList
...     tot = sum(x[1] for x in lst)
...     for i,x in enumerate(lst):
...         lst[i] = (x[0],(1.*x[1])/tot)
...     print lst
...
>>> inputList = [('CA',100),('NY',300),('AZ',200)]
>>> tupleCounts2Percents(inputList)
[('CA', 0.16666666666666666), ('NY', 0.5), ('AZ', 0.3333333333333333)]
这是一种列表理解方法-

>>> def tupleCounts2Percents(inputList):
...     lst = inputList
...     tot = sum(x[1] for x in lst)
...     lst[:] = [(x[0],(1.*x[1])/tot) for x in lst]
...     print lst
...
>>> inputList = [('CA',100),('NY',300),('AZ',200)]
>>> tupleCounts2Percents(inputList)
[('CA', 0.16666666666666666), ('NY', 0.5), ('AZ', 0.3333333333333333)]

如果不希望列表就地更改,请使用
lst
代替
lst[:]
,例如-

>>> def tupleCounts2Percents(inputList):
...     lst = inputList
...     tot = sum(x[1] for x in lst)
...     lst = [(x[0],(1.*x[1])/tot) for x in lst]
...     print lst
...
>>> inputList = [('CA',100),('NY',300),('AZ',200)]
>>> tupleCounts2Percents(inputList)
[('CA', 0.16666666666666666), ('NY', 0.5), ('AZ', 0.3333333333333333)]
>>> inputList
[('CA', 100), ('NY', 300), ('AZ', 200)]


如果使用Python2,您可能希望将total乘以1.0,以便始终得到浮点结果。是的,谢谢。我用的是Python3,所以我忘了。你能解释一下,当有一个字符串和一个整数的时候,如何在一个元组中求和吗?我不明白这是怎么回事works@madman总和位于每个元组的第二项上。我在
sum
的参数中使用了生成器表达式语法。@yangjie是的,但是x[1]不是指列表中的整型('NY',300')吗?我看到它对整数求和,但我想确保我理解正确。如果使用Python 2,可能需要将total乘以1.0,以便始终得到浮点结果。是的,谢谢。我用的是Python3,所以我忘了。你能解释一下,当有一个字符串和一个整数的时候,如何在一个元组中求和吗?我不明白这是怎么回事works@madman总和位于每个元组的第二项上。我在
sum
的参数中使用了生成器表达式语法。@yangjie是的,但是x[1]不是指列表中的整型('NY',300')吗?我看到它对整数求和,但我想确保我理解正确。如果使用Python 2,可能需要将total乘以1.0,以便始终得到浮点结果。是的,谢谢。我用的是Python3,所以我忘了。你能解释一下,当有一个字符串和一个整数的时候,如何在一个元组中求和吗?我不明白这是怎么回事works@madman总和位于每个元组的第二项上。我在
sum
的参数中使用了生成器表达式语法。@yangjie是的,但是x[1]不是指列表中的整型('NY',300')吗?我看到它对整数求和,但我想确保我理解正确。如果使用Python 2,可能需要将total乘以1.0,以便始终得到浮点结果。是的,谢谢。我用的是Python3,所以我忘了。你能解释一下,当有一个字符串和一个整数的时候,如何在一个元组中求和吗?我不明白这是怎么回事works@madman总和位于每个元组的第二项上。我在
sum
的参数中使用了生成器表达式语法。@yangjie是的,但是x[1]不是指列表中的整个('NY',300')吗?我看到它对整数求和,但希望确保我正确理解它