ocost = [10,11,40,4,5] buy_list = ['a32','a11','a45','b30','c43']
def update_list(self, list):
f = open("list1.txt", "at")
for opencost in ocost:
for code in buy_list:
f.writelines("code;"+code+"cost;"+opencost+";\n")
Hello, I'm a beginner at Python Code. For example, the list of ocost and buy_list ocost = [10,11,40,4,5] buy_list = ['a32','a11','a45','b30','c43']
Code;a32;Cost;10 code;a11;cost,11 code;a45;cost;40 Code;b30;Cost;4 Code;c43;Cost;5 I'd like to make it into a text file. I'd like to know how to extract and insert each data without putting the list together. I've been thinking a lot about it, but I've been thinking about it since I've been thinking about it I can't think of anything.
python for
We match the x
th element of the ocost
array with the x
th element of the buy_list
array, right? If that's the case, I think that's how it works.
for x in range(0, len(buy_list)) :
f.writelines ("code;" + buy_list[x] + ";cost;" + str(ocost[x]) + ";\n")
There are also the following methods.
buy_list = ['a32','a11','a45','b30','c43']
ocost = [10,11,40,4,5]
Pairs = dict(zip(buy_list, ocost)) # Create a dict in the form of key=value
lines = map(lambda item:'code;{};Cost;{};\n'.format(item[0], item[1]), pairs.items()) #code:{};Cost:Create a string list to store as a template of type {}
with open('new_file.txt', 'wt') as f: f.Writelines(lines) # Create file and save string
© 2024 OneMinuteCode. All rights reserved.