我有这个元组列表:
dCF3=[((0.0, 0.0), (0.100000001490116, 0.0), (0.200000002980232, 0.0), (0.300000011920929, 0.0), (0.400000005960464, 0.0), (0.5, 0.0), (0.600000023841858, 0.0), (0.699999988079071, 0.0), (0.800000011920929, 0.0), (0.899999976158142, 0.0), (1.0, 0.0)), ((0.0, 0.0), (0.00249999994412065, -268.749877929688), (0.00499999988824129, -534.530700683594), (0.0087500000372529, -932.520874023438), (0.0143750002607703, -1527.93103027344), (0.0228125005960464, -2414.58032226563), (0.0328124985098839, -3408.89599609375), (0.0428125001490116, -4313.58447265625), (0.0528125017881393, -5153.6572265625), (0.0628124997019768, -6001.00244140625), (0.0728124976158142, -6861.203125), (0.0828125029802322, -7718.9912109375), (0.0928125008940697, -8568.873046875), (0.102812498807907, -9406.283203125), (0.112812496721745, -10222.2841796875), (0.122812502086163, -11016.26953125), (0.1328125, -11787.7470703125), (0.142812505364418, -12536.3466796875), (0.152812495827675, -13261.8193359375), (0.162812501192093, -13964.04296875), (0.172812506556511, -14643.01953125), (0.182812497019768, -15298.8681640625), (0.192812502384186, -15931.8173828125), (0.202812492847443, -16542.1953125), (0.212812498211861, -17130.41796875), (0.222812503576279, -17696.978515625), (0.232812494039536, -18242.431640625), (0.242812499403954, -18767.3828125), (0.252812504768372, -19272.4765625), (0.262812495231628, -19758.388671875), (0.272812485694885, -20225.806640625), (0.282812505960464, -20675.43359375), (0.292812496423721, -21107.970703125), (0.302812486886978, -21523.888671875), (0.312812507152557, -21923.1015625), (0.322812497615814, -22307.275390625), (0.332812488079071, -22677.072265625), (0.34281250834465, -23033.1328125), (0.352812498807907, -23376.078125), (0.362812489271164, -23706.50390625), (0.372812509536743, -24024.984375), (0.3828125, -24332.06640625), (0.392812490463257, -24628.27734375), (0.402812510728836, -24914.11328125), (0.412812501192093, -25190.052734375), (0.42281249165535, -25456.55078125), (0.432812511920929, -25714.037109375), (0.442812502384186, -25962.919921875), (0.452812492847443, -26203.58984375), (0.462812513113022, -26436.4140625), (0.472812503576279, -26661.74609375), (0.482812494039536, -26879.9140625), (0.492812514305115, -27091.232421875), (0.502812504768372, -27296.00390625), (0.512812495231628, -27494.9765625), (0.522812485694885, -27688.0859375), (0.532812476158142, -27875.443359375), (0.542812526226044, -28057.2890625), (0.552812516689301, -28233.853515625), (0.562812507152557, -28405.35546875), (0.572812497615814, -28571.99609375), (0.582812488079071, -28733.9765625), (0.592812478542328, -28891.48046875), (0.602812528610229, -29044.685546875), (0.612812519073486, -29193.7578125), (0.622812509536743, -29338.859375), (0.6328125, -29480.142578125), (0.642812490463257, -29617.75), (0.652812480926514, -29751.8203125), (0.662812471389771, -29882.486328125), (0.672812521457672, -30009.87109375), (0.682812511920929, -30134.09375), (0.692812502384186, -30255.271484375), (0.702812492847443, -30373.5078125), (0.712812483310699, -30488.91015625), (0.722812473773956, -30601.576171875), (0.732812523841858, -30711.599609375), (0.742812514305115, -30819.0703125), (0.752812504768372, -30924.076171875), (0.762812495231628, -31026.69921875), (0.772812485694885, -31127.01953125), (0.782812476158142, -31225.109375), (0.792812526226044, -31321.044921875), (0.802812516689301, -31414.892578125), (0.812812507152557, -31506.720703125), (0.822812497615814, -31596.591796875), (0.832812488079071, -31684.568359375), (0.842812478542328, -31770.70703125), (0.852812528610229, -31855.06640625), (0.862812519073486, -31937.69921875), (0.872812509536743, -32018.658203125), (0.8828125, -32097.9921875), (0.892812490463257, -32175.75), (0.902812480926514, -32251.9765625), (0.912812471389771, -32326.716796875), (0.922812521457672, -32400.013671875), (0.932812511920929, -32471.91015625), (0.942812502384186, -32542.44140625), (0.952812492847443, -32611.6484375), (0.962812483310699, -32679.568359375), (0.972812473773956, -32746.234375), (0.982812523841858, -32811.6796875), (0.992812514305115, -32875.9453125), (1.00281250476837, -32939.05078125), (1.01281249523163, -33001.03515625), (1.02281248569489, -33061.92578125), (1.03281247615814, -33121.75390625), (1.0428124666214, -33180.5390625), (1.05281245708466, -33238.31640625), (1.06281244754791, -33295.10546875), (1.07281255722046, -33350.9375), (1.08281254768372, -33405.83203125), (1.09281253814697, -33459.8125), (1.10281252861023, -33512.90234375), (1.11281251907349, -33565.12109375), (1.12281250953674, -33616.49609375), (1.1328125, -33667.0390625), (1.14281249046326, -33716.77734375), (1.15281248092651, -33765.7265625), (1.16281247138977, -33813.90625), (1.17281246185303, -33861.33203125), (1.18281245231628, -33908.0234375), (1.19281244277954, -33953.99609375), (1.20281255245209, -33999.26953125), (1.21281254291534, -34043.85546875), (1.2228125333786, -34087.76953125), (1.23281252384186, -34131.03125), (1.24281251430511, -34173.65234375), (1.25281250476837, -34215.64453125), (1.26281249523163, -34257.0234375), (1.27281248569489, -34297.8046875), (1.28281247615814, -34338.0), (1.2928124666214, -34377.6171875), (1.30281245708466, -34416.67578125), (1.31281244754791, -34455.18359375), (1.32281255722046, -34493.1484375), (1.33281254768372, -34530.58984375), (1.34281253814697, -34567.515625), (1.35281252861023, -34603.9296875), (1.36281251907349, -34639.8515625), (1.37281250953674, -34675.2890625), (1.3828125, -34710.25), (1.39281249046326, -34744.7421875), (1.40281248092651, -34778.78125), (1.41281247138977, -34812.3671875), (1.42281246185303, -34845.515625), (1.43281245231628, -34878.234375), (1.44281244277954, -34910.53125), (1.45281255245209, -34942.41015625), (1.46281254291534, -34973.88671875), (1.4728125333786, -35004.9609375), (1.48281252384186, -35035.64453125), (1.49281251430511, -35065.9453125), (1.50281250476837, -35095.8671875), (1.51281249523163, -35125.421875), (1.52281248569489, -35154.61328125), (1.53281247615814, -35183.4453125), (1.5428124666214, -35211.9296875), (1.55281245708466, -35240.0703125), (1.56281244754791, -35267.87109375), (1.57281255722046, -35295.34375), (1.58281254768372, -35322.4921875), (1.59281253814697, -35349.31640625), (1.60281252861023, -35375.828125), (1.61281251907349, -35402.03125), (1.62281250953674, -35427.9296875), (1.6328125, -35453.53515625), (1.64281249046326, -35478.84375), (1.65281248092651, -35503.86328125), (1.66281247138977, -35528.6015625), (1.67281246185303, -35553.05859375), (1.68281245231628, -35577.24609375), (1.69281244277954, -35601.16015625), (1.70281255245209, -35624.8125), (1.71281254291534, -35648.203125), (1.7228125333786, -35671.33984375), (1.73281252384186, -35694.22265625), (1.74281251430511, -35716.859375), (1.75281250476837, -35739.25), (1.76281249523163, -35761.40234375), (1.77281248569489, -35783.31640625), (1.78281247615814, -35805.0), (1.7928124666214, -35826.45703125), (1.80281245708466, -35847.6875), (1.81281244754791, -35868.6953125), (1.82281255722046, -35889.484375), (1.83281254768372, -35910.0625), (1.84281253814697, -35930.42578125), (1.85281252861023, -35950.58203125), (1.86281251907349, -35970.53125), (1.87281250953674, -35990.28125), (1.8828125, -36009.83203125), (1.89281249046326, -36029.1875), (1.90281248092651, -36048.34765625), (1.91281247138977, -36067.3203125), (1.92281246185303, -36086.10546875), (1.93281245231628, -36104.703125), (1.94281244277954, -36123.12109375), (1.95281255245209, -36141.359375), (1.96281254291534, -36159.421875), (1.9728125333786, -36177.3125), (1.98281252384186, -36195.02734375), (1.99281251430511, -36212.578125), (2.00281238555908, -36229.95703125), (2.01281261444092, -36247.17578125), (2.02281260490417, -36264.23046875), (2.03281259536743, -36281.125), (2.04281258583069, -36297.86328125), (2.05281257629395, -36314.4453125), (2.0628125667572, -36344.6875), (2.07281255722046, -36381.9609375), (2.08281254768372, -36418.8984375), (2.09281253814697, -36455.49609375), (2.10281252861023, -36491.76953125), (2.11281251907349, -36531.01953125), (2.12281250953674, -36590.515625), (2.1328125, -36649.4765625), (2.14281249046326, -36707.91796875), (2.15281248092651, -36765.83984375), (2.16281247138977, -36823.25), (2.17281246185303, -36880.15625), (2.18281245231628, -36936.56640625), (2.19281244277954, -36992.48828125), (2.2028124332428, -37047.921875), (2.21281242370605, -37102.87890625), (2.22281241416931, -37157.36328125), (2.23281240463257, -37211.3828125), (2.24281239509583, -37264.94140625), (2.25281238555908, -37318.04296875), (2.26281261444092, -37370.69921875), (2.27281260490417, -37422.9140625), (2.28281259536743, -37474.6875), (2.29281258583069, -37526.03125), (2.30281257629395, -37576.9453125), (2.3128125667572, -37627.44140625), (2.32281255722046, -37677.51953125), (2.33281254768372, -37727.1875), (2.34281253814697, -37776.44921875), (2.35281252861023, -37825.3125), (2.36281251907349, -37873.7734375), (2.37281250953674, -37921.84765625), (2.3828125, -37969.53515625), (2.39281249046326, -38016.83984375), (2.40281248092651, -38063.765625), (2.41281247138977, -38110.31640625), (2.42281246185303, -38156.50390625), (2.43281245231628, -38202.32421875), (2.44281244277954, -38247.78125), (2.4528124332428, -38292.88671875), (2.46281242370605, -38337.640625), (2.47281241416931, -38382.046875))]
我在这个列表中有两个元组。首先,我想得到第一个元组的第一列的最大值(例如:(Xi,Yi),max X),并将该值添加到第二个元组的第一列。此外,在此操作之后,我想将两个元组合并为一个。
有什么想法吗?
编辑:
问题已经解决了。但是,如果不是第一个元组,我想找到m
元组的第一列的最大值,然后将其添加到下一个n
元组的第一列,直到结束列表,然后在最后加入所有元组中的一个,我怎么能这样做?
我的代码有效,但可以优化:
indexLSNR=len(dCF3)-2
t1max=max(dCF3[indexLSNR])[0]
for i in range(0, len(dCF3)):
if i<=indexLSNR:
if i==0:
new_line= dCF3[i]
else:
new_line= dCF3[i] + new_line
if i>indexLSNR:
new_tup2 = tuple((a + t1max, b) for a, b in dCF3[i])
new_line= new_line + new_tup2
答案 0 :(得分:3)
“首先,我想获得第一个元组第一列的最大值”
m = max(dCF3[0])[0]
或者,如果您希望更明确地考虑仅考虑第一列
from operator import itemgetter
m = max(dCF3[0], key=itemgetter(0))[0]
“...并将该值添加到第二个元组的第一列。”
元组是不可变的,因此您无法更新其中的值。但是,您可以使用更新的值创建一个新元组:
以下内容基于dCF3[1]
创建了新元组,并在所有条目的第一列中添加了m
:
new_tup2 = tuple((a + m, b) for a, b in dCF3[1])
“此外,在此操作之后,我想将两个元组合并为一个。”
如果你的意思是将两个元组连接成一个长元组:
joined = dCF3[0] + new_tup2
<强>更新强>
为了解决您更新的问题,我从您想要的示例代码中收集:
t1max
N-1
元组(在您的示例中为N=2
),使用更新的值,使第一列增加t1max
以下是我将如何处理它,以期通过最小化我们对元组的副本数来保持效率:
n = 2 # your parameter
p = n - 1 # shorthand, just to make the following code cleaner
t1max = max(dCF3[0])[0] # get max of first col of first tuple
# store a reference to all tuples except the last N-1
# Note that we're NOT making copies of the actual tuples. Only refs.
tup_list = list(dCF3[:-p])
# Append generators that will return the last N-1 tuples with updated values
# Again, we're not making any copies of the tuple. Only generators that will
# iterate through the tuples when consumed
for tup in dCF3[-p:]:
tup_list.append(((a + t1max, b) for a, b in tup))
# Now we're ready to create a copy of the tuples as a single large tuple
from itertools import chain
new_tup = tuple(chain.from_iterable(tup_list))
P.S。如果您需要支持itertools.chain.from_iterable不可用的python&lt; 2.6,则可以改为使用chain(*tup_list)
。
答案 1 :(得分:2)
找到最大值:
max_col1 = map(max, zip(*dCF3[0]))[0] #[0]Gives the max of the first values in the tuple
在第二个元组中的每个第1列添加1:
new_tup = tuple((x + max_col1, y) for x, y in dCF3[1])
并合并
tup = dCF3[0] + new_tup
答案 2 :(得分:1)
快速使用itertools
:
>>> import itertools
>>> list(itertools.chain(dCF3[0], max(dCF3[0]) + dCF3[1][0], dCF3[1][1:]))
这加入了:
答案 3 :(得分:1)
这将处理任意数量的元组:
lis = [((0.0, 0.0), (0.100000001490116, 0.0), (0.200000002980232, 0.0), (0.300000011920929, 0.0), (0.400000005960464, 0.0), (0.5, 0.0), (0.600000023841858, 0.0), (0.699999988079071, 0.0), (0.800000011920929, 0.0), (0.899999976158142, 0.0), (1.0, 0.0)), ((0.0, 0.0), (0.00249999994412065, -268.749877929688), (0.00499999988824129, -534.530700683594), (0.0087500000372529, -932.520874023438), (0.0143750002607703, -1527.93103027344), (0.0228125005960464, -2414.58032226563), (0.0328124985098839, -3408.89599609375), (0.0428125001490116, -4313.58447265625), (0.0528125017881393, -5153.6572265625), (0.0628124997019768, -6001.00244140625), (0.0728124976158142, -6861.203125), (0.0828125029802322, -7718.9912109375), (0.0928125008940697, -8568.873046875), (0.102812498807907, -9406.283203125), (0.112812496721745, -10222.2841796875), (0.122812502086163, -11016.26953125), (0.1328125, -11787.7470703125), (0.142812505364418, -12536.3466796875), (0.152812495827675, -13261.8193359375), (0.162812501192093, -13964.04296875), (0.172812506556511, -14643.01953125), (0.182812497019768, -15298.8681640625), (0.192812502384186, -15931.8173828125), (0.202812492847443, -16542.1953125), (0.212812498211861, -17130.41796875), (0.222812503576279, -17696.978515625), (0.232812494039536, -18242.431640625), (0.242812499403954, -18767.3828125), (0.252812504768372, -19272.4765625), (0.262812495231628, -19758.388671875), (0.272812485694885, -20225.806640625), (0.282812505960464, -20675.43359375), (0.292812496423721, -21107.970703125), (0.302812486886978, -21523.888671875), (0.312812507152557, -21923.1015625), (0.322812497615814, -22307.275390625), (0.332812488079071, -22677.072265625), (0.34281250834465, -23033.1328125), (0.352812498807907, -23376.078125), (0.362812489271164, -23706.50390625), (0.372812509536743, -24024.984375), (0.3828125, -24332.06640625), (0.392812490463257, -24628.27734375), (0.402812510728836, -24914.11328125), (0.412812501192093, -25190.052734375), (0.42281249165535, -25456.55078125), (0.432812511920929, -25714.037109375), (0.442812502384186, -25962.919921875), (0.452812492847443, -26203.58984375), (0.462812513113022, -26436.4140625), (0.472812503576279, -26661.74609375), (0.482812494039536, -26879.9140625), (0.492812514305115, -27091.232421875), (0.502812504768372, -27296.00390625), (0.512812495231628, -27494.9765625), (0.522812485694885, -27688.0859375), (0.532812476158142, -27875.443359375), (0.542812526226044, -28057.2890625), (0.552812516689301, -28233.853515625), (0.562812507152557, -28405.35546875), (0.572812497615814, -28571.99609375), (0.582812488079071, -28733.9765625), (0.592812478542328, -28891.48046875), (0.602812528610229, -29044.685546875), (0.612812519073486, -29193.7578125), (0.622812509536743, -29338.859375), (0.6328125, -29480.142578125), (0.642812490463257, -29617.75), (0.652812480926514, -29751.8203125), (0.662812471389771, -29882.486328125), (0.672812521457672, -30009.87109375), (0.682812511920929, -30134.09375), (0.692812502384186, -30255.271484375), (0.702812492847443, -30373.5078125), (0.712812483310699, -30488.91015625), (0.722812473773956, -30601.576171875), (0.732812523841858, -30711.599609375), (0.742812514305115, -30819.0703125), (0.752812504768372, -30924.076171875), (0.762812495231628, -31026.69921875), (0.772812485694885, -31127.01953125), (0.782812476158142, -31225.109375), (0.792812526226044, -31321.044921875), (0.802812516689301, -31414.892578125), (0.812812507152557, -31506.720703125), (0.822812497615814, -31596.591796875), (0.832812488079071, -31684.568359375), (0.842812478542328, -31770.70703125), (0.852812528610229, -31855.06640625), (0.862812519073486, -31937.69921875), (0.872812509536743, -32018.658203125), (0.8828125, -32097.9921875), (0.892812490463257, -32175.75), (0.902812480926514, -32251.9765625), (0.912812471389771, -32326.716796875), (0.922812521457672, -32400.013671875), (0.932812511920929, -32471.91015625), (0.942812502384186, -32542.44140625), (0.952812492847443, -32611.6484375), (0.962812483310699, -32679.568359375), (0.972812473773956, -32746.234375), (0.982812523841858, -32811.6796875), (0.992812514305115, -32875.9453125), (1.00281250476837, -32939.05078125), (1.01281249523163, -33001.03515625), (1.02281248569489, -33061.92578125), (1.03281247615814, -33121.75390625), (1.0428124666214, -33180.5390625), (1.05281245708466, -33238.31640625), (1.06281244754791, -33295.10546875), (1.07281255722046, -33350.9375), (1.08281254768372, -33405.83203125), (1.09281253814697, -33459.8125), (1.10281252861023, -33512.90234375), (1.11281251907349, -33565.12109375), (1.12281250953674, -33616.49609375), (1.1328125, -33667.0390625), (1.14281249046326, -33716.77734375), (1.15281248092651, -33765.7265625), (1.16281247138977, -33813.90625), (1.17281246185303, -33861.33203125), (1.18281245231628, -33908.0234375), (1.19281244277954, -33953.99609375), (1.20281255245209, -33999.26953125), (1.21281254291534, -34043.85546875), (1.2228125333786, -34087.76953125), (1.23281252384186, -34131.03125), (1.24281251430511, -34173.65234375), (1.25281250476837, -34215.64453125), (1.26281249523163, -34257.0234375), (1.27281248569489, -34297.8046875), (1.28281247615814, -34338.0), (1.2928124666214, -34377.6171875), (1.30281245708466, -34416.67578125), (1.31281244754791, -34455.18359375), (1.32281255722046, -34493.1484375), (1.33281254768372, -34530.58984375), (1.34281253814697, -34567.515625), (1.35281252861023, -34603.9296875), (1.36281251907349, -34639.8515625), (1.37281250953674, -34675.2890625), (1.3828125, -34710.25), (1.39281249046326, -34744.7421875), (1.40281248092651, -34778.78125), (1.41281247138977, -34812.3671875), (1.42281246185303, -34845.515625), (1.43281245231628, -34878.234375), (1.44281244277954, -34910.53125), (1.45281255245209, -34942.41015625), (1.46281254291534, -34973.88671875), (1.4728125333786, -35004.9609375), (1.48281252384186, -35035.64453125), (1.49281251430511, -35065.9453125), (1.50281250476837, -35095.8671875), (1.51281249523163, -35125.421875), (1.52281248569489, -35154.61328125), (1.53281247615814, -35183.4453125), (1.5428124666214, -35211.9296875), (1.55281245708466, -35240.0703125), (1.56281244754791, -35267.87109375), (1.57281255722046, -35295.34375), (1.58281254768372, -35322.4921875), (1.59281253814697, -35349.31640625), (1.60281252861023, -35375.828125), (1.61281251907349, -35402.03125), (1.62281250953674, -35427.9296875), (1.6328125, -35453.53515625), (1.64281249046326, -35478.84375), (1.65281248092651, -35503.86328125), (1.66281247138977, -35528.6015625), (1.67281246185303, -35553.05859375), (1.68281245231628, -35577.24609375), (1.69281244277954, -35601.16015625), (1.70281255245209, -35624.8125), (1.71281254291534, -35648.203125), (1.7228125333786, -35671.33984375), (1.73281252384186, -35694.22265625), (1.74281251430511, -35716.859375), (1.75281250476837, -35739.25), (1.76281249523163, -35761.40234375), (1.77281248569489, -35783.31640625), (1.78281247615814, -35805.0), (1.7928124666214, -35826.45703125), (1.80281245708466, -35847.6875), (1.81281244754791, -35868.6953125), (1.82281255722046, -35889.484375), (1.83281254768372, -35910.0625), (1.84281253814697, -35930.42578125), (1.85281252861023, -35950.58203125), (1.86281251907349, -35970.53125), (1.87281250953674, -35990.28125), (1.8828125, -36009.83203125), (1.89281249046326, -36029.1875), (1.90281248092651, -36048.34765625), (1.91281247138977, -36067.3203125), (1.92281246185303, -36086.10546875), (1.93281245231628, -36104.703125), (1.94281244277954, -36123.12109375), (1.95281255245209, -36141.359375), (1.96281254291534, -36159.421875), (1.9728125333786, -36177.3125), (1.98281252384186, -36195.02734375), (1.99281251430511, -36212.578125), (2.00281238555908, -36229.95703125), (2.01281261444092, -36247.17578125), (2.02281260490417, -36264.23046875), (2.03281259536743, -36281.125), (2.04281258583069, -36297.86328125), (2.05281257629395, -36314.4453125), (2.0628125667572, -36344.6875), (2.07281255722046, -36381.9609375), (2.08281254768372, -36418.8984375), (2.09281253814697, -36455.49609375), (2.10281252861023, -36491.76953125), (2.11281251907349, -36531.01953125), (2.12281250953674, -36590.515625), (2.1328125, -36649.4765625), (2.14281249046326, -36707.91796875), (2.15281248092651, -36765.83984375), (2.16281247138977, -36823.25), (2.17281246185303, -36880.15625), (2.18281245231628, -36936.56640625), (2.19281244277954, -36992.48828125), (2.2028124332428, -37047.921875), (2.21281242370605, -37102.87890625), (2.22281241416931, -37157.36328125), (2.23281240463257, -37211.3828125), (2.24281239509583, -37264.94140625), (2.25281238555908, -37318.04296875), (2.26281261444092, -37370.69921875), (2.27281260490417, -37422.9140625), (2.28281259536743, -37474.6875), (2.29281258583069, -37526.03125), (2.30281257629395, -37576.9453125), (2.3128125667572, -37627.44140625), (2.32281255722046, -37677.51953125), (2.33281254768372, -37727.1875), (2.34281253814697, -37776.44921875), (2.35281252861023, -37825.3125), (2.36281251907349, -37873.7734375), (2.37281250953674, -37921.84765625), (2.3828125, -37969.53515625), (2.39281249046326, -38016.83984375), (2.40281248092651, -38063.765625), (2.41281247138977, -38110.31640625), (2.42281246185303, -38156.50390625), (2.43281245231628, -38202.32421875), (2.44281244277954, -38247.78125), (2.4528124332428, -38292.88671875), (2.46281242370605, -38337.640625), (2.47281241416931, -38382.046875))]
lis = [map(list,x) for x in lis] #create list of lists as you can't modify a tuple
maxx = max(y[0] for x in lis[:m] for y in x) #find the max in first m tuples
for i in xrange(m,m+n+1): #update n tuples after m
for j in xrange(len(lis[i])):
lis[i][j][0] += maxx
new_lis = lis[0] + lis[1]