简体   繁体   English

仅返回变量中的最后一个列表元素

[英]Only last list element returned in a variable

So I have a list of strings with some metric units inside.所以我有一个字符串列表,里面有一些公制单位。 For example:例如:

list=['i','am','160','cm','but','i','would','like','to','be','1.8','m']

I want to convert it into a new list, with the values converted through a dictionary.我想将其转换为一个新列表,并通过字典转换值。 I'm making a variable of the metric units on the list, so that I can use it later for the conversion.我正在制作列表中公制单位的变量,以便以后可以将其用于转换。 But every time I call this variable only the last element is returned.但是每次我调用这个变量时,只返回最后一个元素。 (eg only the m in the list above). (例如,只有上面列表中的m )。

Do you know how I can fix this?你知道我该如何解决这个问题吗?

SI_1 = {'mm':0.001, 'cm':0.01, 'm':1.0, 'km':1000}
for i in new_list:
    if i in SI_1:
        METRIC_IN_TEXT=i
        print(METRIC_IN_TEXT)
        METRIC_IN_TEXT_VALUE=SI_1.get(i)
        print(METRIC_IN_TEXT_VALUE)

I'm not sure what are you asking for, but we can convert all values from list using mapping this way:我不确定您要的是什么,但我们可以使用这种映射方式转换列表中的所有值:

  1. don't call variable list because it is build-in keyword.不要调用变量list ,因为它是内置关键字。

Code example:代码示例:

my_list=['i','am','160','cm','but','i','would','like','to','be','1.8','m']
mapper = {'mm':0.001, 'cm':0.01, 'm':1.0, 'km':1000}

res = [mapper[el] if el in mapper.keys() else el for el in my_list ]
res

output: output:

['i', 'am', '160', 0.01, 'but', 'i', 'would', 'like', 'to', 'be', '1.8', 1.0] ['i', 'am', '160', 0.01, 'but', 'i', 'would', 'like', 'to', 'be', '1.8', 1.0]

Sorry i wasn't clear at all on my post, because it is a uni assignment.抱歉,我的帖子根本不清楚,因为这是一个大学作业。 SO i want to make a function that converts units from a text file, and then it writes the same text to a different file with the units converted.所以我想做一个 function 从文本文件转换单位,然后将相同的文本写入不同的文件并转换单位。 The target unit is determined by the DICT.目标单位由 DICT 确定。 SO my problem is that the list comprehension 'CONVERTED_LIST_1' generates different list for every conversion istead of one list with all the conversion.所以我的问题是列表理解“CONVERTED_LIST_1”为每次转换生成不同的列表,而不是一个包含所有转换的列表。 Hope i became more clear now希望我现在变得更清楚

SI_1 = {'mm':0.001, 'cm':0.01, 'm':1.0, 'km':1000}
METRIC_IN_DICT=D.get('SIZE')
METRIC_IN_DICT_VALUE=SI_1.get(METRIC_IN_DICT)


with open('TEXT1.txt','r') as x:
        LIST=x.read().split()
        new_list=[item.replace('.','') if '.' in item else item for item in LIST]  
        for i in new_list:
            if i in SI_1:
            METRIC_IN_TEXT=i
            print(METRIC_IN_TEXT)
            METRIC_IN_TEXT_VALUE=SI_1.get(i)
            print(METRIC_IN_TEXT_VALUE)
            POS_OF_VALUE=new_list[new_list.index(METRIC_IN_TEXT)-1]
            print(POS_OF_LENGTH)
            RESULT=int(POS_OF_VALUE)*METRIC_IN_TEXT_VALUE/METRIC_IN_DICT_VALUE
            print(RESULT)   
            CONVERTED_LIST_1=[RESULT if i==POS_OF_VALUE and POS_OF_VALUE.isdigit() else METRIC_IN_DICT if i==METRIC_IN_TEXT else i for i in new_list] 
            print(CONVERTED_LIST_1)```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM