So I wrote this but it doesn't accomplish what I want to do. Basically, I want to replace the number in the second index with whatever the word is at that index in the content_list list.
content_list= ['abstract', 'bow', 'button', 'chiffon', 'collar', 'cotton', 'crepe', 'crochet', 'crop', 'embroidered', 'floral', 'floralprint', 'knit', 'lace', 'longsleeve', 'peasant', 'pink', 'pintuck', 'plaid', 'pleated', 'polkadot', 'printed', 'red', 'ruffle', 'sheer', 'shirt', 'sleeve', 'sleeveless', 'split', 'striped', 'summer', 'trim', 'tunic', 'v-neck', 'woven', '']
max=[['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', '24'],['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', '19,15,24']]
for l in max:
e=l[1]
f=e.split(",")
for s in f:
intt=int(s)
rep=content_list[intt]
#print(rep)
e.replace(s,rep)
#print(z)
print(max)
This is the output that i get:
[['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', '24'], ['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', '19,15,24']]
But this is what i want:
[['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', 'sheer'], ['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', 'pleated,peasant,sheer']]
First of all, max is a built-in function I would highly recommend you to check how to name variables for the future, It may cause some big problems for you :).
You can brute-force your way out here also something like this:
arr = [
['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', '24'],
['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', '19,15,24'],
]
for inner in arr:
indexes=inner[1]
inner[1] = ""
for number in indexes.split(","):
inner[1] += content_list[int(number)]
print(inner)
I would do something like this, I think must be better options but it works... so it's better than nothing
content_list = ['abstract', 'bow', 'button', 'chiffon',
'collar', 'cotton', 'crepe', 'crochet',
'crop', 'embroidered', 'floral', 'floralprint',
'knit', 'lace', 'longsleeve', 'peasant', 'pink',
'pintuck', 'plaid', 'pleated', 'polkadot', 'printed',
'red', 'ruffle', 'sheer', 'shirt', 'sleeve', 'sleeveless',
'split', 'striped', 'summer', 'trim', 'tunic',
'v-neck', 'woven', '']
target_array = [['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', '24'],
['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', '19,15,24']]
for id_target, el in enumerate(target_array):
for num in el[1].split(','):
target_array[id_target][1] = target_array[id_target][1].replace(num,
content_list[int(num)])
Here is the solution:
import numpy as np
c= ['abstract', 'bow', 'button', 'chiffon', 'collar', 'cotton', 'crepe', 'crochet', 'crop', 'embroidered', 'floral', 'floralprint', 'knit', 'lace', 'longsleeve', 'peasant', 'pink', 'pintuck', 'plaid', 'pleated', 'polkadot', 'printed', 'red', 'ruffle', 'sheer', 'shirt', 'sleeve', 'sleeveless', 'split', 'striped', 'summer', 'trim', 'tunic', 'v-neck', 'woven', '']
m=[['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', '24'],['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg', '19,15,24']]
n = np.copy(m)
for i in range(np.size(m,1)):
for j in range(np.size(m[i][1].split(','))):
idx = m[i][1].split(',')[j]
if (j==0):
n[i][1] = c[int(idx)]
else:
n[i][1] += ',' + c[int(idx)]
print(n)
The output:
[['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg' 'sheer']
['Img/img/Sheer_Pleated-Front_Blouse/img_00000001.jpg'
'pleated,peasant,sheer']]
Related
I have a nested list of tuples:
[[('complacency', 0.0001833514688884038),
('complexity', 0.00020885722118234196),
('system', 0.00030831926569582427),
('accidents', 0.0003527060442832197),
('major', 0.0003577792651629483),
('accident', 0.000556904280447189),
('Safety', 0.0005632664174249453),
('issue', 0.000604949484895331),
('risk', 0.000655457410972149),
('complex', 0.0007215989124478362)],
[('situation', 0.00029301996954456724),
('awareness', 0.0003444184039291439),
('people', 0.00201798567882153),
('loss', 0.002527094648295153),
('constructs', 0.002921195578537488),
('complacency', 0.003951273394687846),
('human', 0.004937009924663634),
('world', 0.004937009924663634),
('Dekker', 0.004963844372504768),
('representational', 0.006297840866917582)]]
I want to drop the y element from every tuple (the numbers), but I need to be able to preserve the nested structure of the list.
You can use list comprehension:
lst = [[('complacency', 0.0001833514688884038), ('complexity', 0.00020885722118234196), ('system', 0.00030831926569582427), ('accidents', 0.0003527060442832197), ('major', 0.0003577792651629483), ('accident', 0.000556904280447189), ('Safety', 0.0005632664174249453), ('issue', 0.000604949484895331), ('risk', 0.000655457410972149), ('complex', 0.0007215989124478362)], [('situation', 0.00029301996954456724), ('awareness', 0.0003444184039291439), ('people', 0.00201798567882153), ('loss', 0.002527094648295153), ('constructs', 0.002921195578537488), ('complacency', 0.003951273394687846), ('human', 0.004937009924663634), ('world', 0.004937009924663634), ('Dekker', 0.004963844372504768), ('representational', 0.006297840866917582)]]
output = [[x for x, _ in sublst] for sublst in lst]
print(output)
# [['complacency', 'complexity', 'system', 'accidents', 'major', 'accident', 'Safety', 'issue', 'risk', 'complex'],
# ['situation', 'awareness', 'p eple', 'loss', 'constructs', 'complacency', 'human', 'world', 'Dekker', 'representational']]
Assuming lst the input, if you have a fixed depth:
out = [[t[0] for t in l] for l in lst]
output:
[['complacency', 'complexity', 'system', 'accidents', 'major', 'accident', 'Safety', 'issue', 'risk', 'complex'],
['situation', 'awareness', 'people', 'loss', 'constructs', 'complacency', 'human', 'world', 'Dekker', 'representational']]
If you have an arbitrary depth, use recursion:
def subset(x):
if isinstance(x, tuple):
return x[0]
else:
return [subset(e) for e in x]
out = subset(lst)
I have a programme that reads a list of lists and produces a key from the first element in each list and a value from the other elements in the list. If the key already exists then the programme appends the list data to the existing value in the dictionary pair.
My code looks like:
# IMPORTS
from csv import reader
# VARIABLES
test_list = [['\ufeffCategory', 'materials', 'details', 'cost'], ['Clothes', 'Socks', 'Wool', '5'], ['Clothes', 'Shorts', 'Cotton', '5'], ['Clothes', 'Trousers', 'Cotton', '15'], ['Education', 'Books', 'Rymans', '10'], ['Education', 'Pens', 'Rymans', '12'], ['Education', 'Pencils', 'Rymans', '4'], ['Education', 'Paper', 'WH Smths', '3.5'], ['Education', 'computer', 'PC World', '800'], ['Entertainment', 'Kindle Unlimited', 'DD', '7.99'], ['Entertainment', 'Audible', 'On hold', '7'], ['Entertainment', 'Spotify', 'DD', '11.99'], ['Entertainment', 'Netflix', 'DD', '9']]
def make_dict_from_csv(test_list):
""" read list make a key:value pair making the key the first element in list and the rest of the elements as the value"""
budget_dict = {}
i = 0
starting_key = ""
temp_list = []
for item in test_list:
key = test_list[i][0]
if key != starting_key:
value = [n for n in test_list[i][1:]]
budget_dict[key] = value
starting_key = key
value = []
else:
value = []
value = [n for n in test_list[i][1:]]
temp_list = []
for k, v in budget_dict.items():
if k == starting_key:
temp_list = [v, value]
budget_dict[key] = temp_list
temp_list = []
i += 1
del budget_dict["\ufeffCategory"] # delete header data
return budget_dict
budget_list = read_csv_make_list(file)
budget_dict = make_dict_from_csv(test_list)
print(budget_dict)
How can I 'flatten' the lists in the key:value pair so that I end up with e.g.
{'Clothes': [['Socks', 'Wool', '5'], ['Shorts', 'Cotton', '5'], ['Trousers', 'Cotton', '15']]}
rather than
{'Clothes': [[['Socks', 'Wool', '5'], ['Shorts', 'Cotton', '5']], ['Trousers', 'Cotton', '15']]}`
?
Try this:
def make_dict_from_csv(test_list):
budget_dict = dict()
for sublist in test_list:
key = sublist[0]
if key=="\ufeffCategory":
continue
if key in budget_dict:
budget_dict[key].append(sublist[1:])
else:
budget_dict[key] = [sublist[1:]]
return budget_dict
>>> make_dict_from_csv(test_list)
{'Clothes': [['Socks', 'Wool', '5'],
['Shorts', 'Cotton', '5'],
['Trousers', 'Cotton', '15']],
'Education': [['Books', 'Rymans', '10'],
['Pens', 'Rymans', '12'],
['Pencils', 'Rymans', '4'],
['Paper', 'WH Smths', '3.5'],
['computer', 'PC World', '800']],
'Entertainment': [['Kindle Unlimited', 'DD', '7.99'],
['Audible', 'On hold', '7'],
['Spotify', 'DD', '11.99'],
['Netflix', 'DD', '9']]
}
def make_dict_from_csv(test_list):
ret = {}
for i in range(1,len(test_list)):
key = test_list[i][0]
value = test_list[i][1:]
if key not in ret:
ret[key] = [value]
else:
ret[key].append(value)
return ret
You can condense this greatly.
from collections import defaultdict
def make_dict_from_csv(test_list):
itr = iter(test_list)
next(itr) # Skip the header
budget_dict = defaultdict(list)
for key, *value in itr:
budget_dict[key].append(value)
return budget_dict
I'm trying to find the length of words individually in my text file. I tried it by following code but this code is showing me up the count of words that how many times this word is used in file.
text = open(r"C:\Users\israr\Desktop\counter\Bigdata.txt")
d = dict()
for line in text:
line = line.strip()
line = line.lower()
words = line.split(" ")
for word in words:
if word in d:
d[word] = d[word] + 1
else:
# Add the word to dictionary with count 1
d[word] = 1
for key in list(d.keys()):
print(key, ":", d[key])
And the output is something like this
china : 14
emerged : 1
as : 16
one : 5
of : 44
the : 108
world's : 7
first : 2
civilizations, : 1
in : 26
fertile : 1
basin : 1
yellow : 1
river : 1
north : 1
plain. : 1
Basically i want a list of words having same length for example china , first , world :5 this 5 is the length of all this words and so on the words having different length in other list
İf you need all word's total lengths seperatly, you can find them using this formula:
len(word) * count(word) for all word in words
equalivent in python:
d[key] * len(key)
Change last 2 lines with below:
for key in list(d.keys()):
print(key, ":", d[key] * len(key))
----EDIT----
It ıs what you asked in comments, I guess. Below code gives you groups whose members are the same length.
for word in words:
if len(word) in d:
if word not in d[len(word)]:
d[len(word)].append(word)
else:
# Add the word to dictionary with count 1
d[len(word)] = [word]
for key in list(d.keys()):
print(key, ":", d[key])
Output of this code:
3 : ['the', 'bc,', '(c.', 'who', 'was', '100', 'bc)', 'and', 'xia', 'but', 'not', 'one', 'due', '8th', '221', 'qin', 'shi', 'for', 'his', 'han', '220', '206', 'has', 'war', 'all', 'far']
8 : ['earliest', 'describe', 'writings', 'indicate', 'commonly', 'however,', 'cultural', 'history,', 'regarded', 'external', 'internal', 'culture,', 'troubled', 'imperial', 'selected', 'replaced', 'republic', 'mainland', "people's", 'peoples,', 'multiple', 'kingdoms', 'xinjiang', 'present.', '(carried']
5 : ['known', 'china', 'early', 'shang', 'texts', 'grand', 'ruled', 'river', 'which', 'along', 'these', 'arose', 'years', 'their', 'rule.', 'began', 'first', 'those', 'huang', 'title', 'after', 'until', '1912,', 'tasks', 'elite', 'young', '1949.', 'unity', 'being', 'civil', 'parts', 'other', 'world', 'waves', 'basis']
7 : ['written', 'records', 'history', 'dynasty', 'ancient', 'century', 'mention', 'writing', 'period,', 'xia.[5]', 'valley,', 'chinese', 'various', 'centers', 'yangtze', "world's", 'cradles', 'concept', 'mandate', 'justify', 'central', 'country', 'smaller', 'period.', 'another', 'warring', 'created', 'himself', 'huangdi', 'marking', 'systems', 'enabled', 'emperor', 'control', 'routine', 'handled', 'special', 'through', "china's", 'between', 'periods', 'culture', 'western', 'foreign']
2 : ['of', 'as', 'wu', 'by', 'no', 'is', 'do', 'in', 'to', 'be', 'at', 'or', 'bc', '21', 'ad']
4 : ['date', 'from', '1250', 'bc),', 'king', 'such', 'book', '11th', '(296', 'held', 'both', 'with', 'zhou', 'into', 'much', 'qin,', 'fell', 'soon', '(206', 'ad).', 'that', 'vast', 'were', 'men,', 'last', 'qing', 'then', 'most', 'whom', 'eras', 'have', 'some', 'asia', 'form']
9 : ['1600–1046', 'mentioned', 'documents', 'chapters,', 'historian', '2070–1600', 'existence', 'neolithic', 'millennia', 'thousands', '(1046–256', 'pressures', 'following', 'developed', 'conquered', '"emperor"', 'beginning', 'dynasties', 'directly.', 'centuries', 'carefully', 'difficult', 'political', 'dominated', 'stretched', 'contact),']
6 : ['during', "ding's", '(early', 'bamboo', 'annals', 'before', 'shang,', 'yellow', 'cradle', 'river.', 'shang.', 'oldest', 'heaven', 'weaken', 'states', 'spring', 'autumn', 'became', 'warred', 'times.', 'china.', 'death,', 'peace,', 'failed', 'recent', 'steppe', 'china;', 'tibet,', 'modern']
12 : ['reign,[1][2]', 'twenty-first', 'longer-lived', 'bureaucratic', 'calligraphy,', '(1644–1912),', '(1927–1949).', 'occasionally', 'immigration,']
11 : ['same.[3][4]', 'independent', 'traditional', 'territories', 'well-versed', 'literature,', 'philosophy,', 'assimilated', 'population.', 'warlordism,']
10 : ['historical', 'originated', 'continuous', 'supplanted', 'introduced', 'government', 'eventually', 'splintered', 'literature', 'philosophy', 'oppressive', 'successive', 'alternated', 'influences', 'expansion,']
1 : ['a', '–']
13 : ['civilization.', 'civilizations', 'examinations.', 'statehood—the', 'assimilation,']
17 : ['civilizations,[6]']
16 : ['civilization.[7]']
0 : ['']
14 : ['administrative']
18 : ['scholar-officials.']
Below is full version of code.
text = open("bigdata.txt")
d = dict()
for line in text:
line = line.strip()
line = line.lower()
words = line.split(" ")
for word in words:
if len(word) in d:
if word not in d[len(word)]:
d[len(word)].append(word)
else:
d[len(word)] = [word]
for key in list(d.keys()):
print(key, ":", d[key])
When you look at the code for dealing with each word, you will see your problem..
for word in words:
if word in d:
d[word] = d[word] + 1
else:
# Add the word to dictionary with count 1
d[word] = 1
Here you are checking if a word is in the dictionary. If it is, add 1 to its key when we find it. If it is not, initialize it at 1. This is the core concept for counting repetitions.
If you want to count the length of the word, you could simply do.
for word in words:
if word not in d:
d[word] = len(word)
And to output your dict, you can do
for k, v in d.items():
print(k, ":", v)
You can create a list of word lengths and then process them through python's built-in Counter:
from collections import Counter
with open("mytext.txt", "r") as f:
words = f.read().split()
words_lengths = [len(word) for word in words]
counter = Counter(words_lengths)
The output would be smth like:
In[1]:counter
Out[1]:Counter({7: 146, 9: 73, 5: 73, 4: 146, 1: 73})
Where keys are words lengths, and values are the number of times they occurred.
You can work with that as with usual dictionary.
I'm attempting to create a new category in my list based on the price of an application. If the price equals 0 then the new category will list the application as 'free.' If the application is equal to or greater than 50, then the new category will list the application as 'very expensive.'
When I run the following code, my new list appears to have the desired section called 'price label' for the first row which would essentially be column headers. However, the subsequent rows / parts of the list do not contain the desired price categories: 'free', 'affordable', 'expensive', etc...
Can someone please tell me why I'm not seeing the desired categories of 'free', 'affordable', expensive', etc...
Thank you in advance.
opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
for app in apps_data[1:]:
price = float(app[4])
if price == 0:
app.append('free')
elif price > 0 and price < 20:
app.append('affordable')
elif price >= 20 and price < 50:
app.append('expensive')
elif price >= 50:
app.append('very expensive')
apps_data[0].append('price_label')
I don't see whats the error. The code works perfectly fine for me.
Also if you want to access vaiables in the original apps_data list you can use enumerate
for x,app in enumerate(apps_data[1:]):
I have attached the output below
from csv import reader
with open('data.csv','r') as f:
read_file = reader(f)
apps_data = list(read_file)
for app in apps_data[1:]:
price = float(app[4])
print(price)
if price == 0:
app.append('free')
elif price > 0 and price < 20:
app.append('affordable')
elif price >= 20 and price < 50:
app.append('expensive')
elif price >= 50:
app.append('very expensive')
apps_data[0].append('price_label')
And the output was
['a', 'b', 'c', 'd', 'e', 'price_label']
['0.158910465', '0.471050439', '0.57616592', '0.73310126', '22', 'expensive']
['0.551014295', '0.366737111', '0.199838786', '0.58817097', '54', 'very expensive']
['0.466420839', '0.691756584', '0.282783984', '0.585577806', '96', 'very expensive']
['0.17755358', '0.836570041', '0.950988799', '0.08555555', '77', 'very expensive']
['0.511195256', '0.02926122', '0.250221844', '0.811879858', '96', 'very expensive']
['0.905874282', '0.377772593', '0.461482911', '0.161167257', '80', 'very expensive']
['0.20806536', '0.983685005', '0.024045729', '0.36432202', '45', 'expensive']
['0.980234589', '0.417512776', '0.291544555', '0.65904768', '5', 'affordable']
['0.352375065', '0.978612252', '0.1695656', '0.033074721', '54', 'very expensive']
['0.128592107', '0.199429582', '0.431974287', '0.600086682', '94', 'very expensive']
['0.156103725', '0.060598535', '0.259385323', '0.372274231', '60', 'very expensive']
['0.97831926', '0.322897216', '0.665576639', '0.616234227', '49', 'expensive']
['0.59680653', '0.062487993', '0.03078493', '0.467880813', '21', 'expensive']
['0.10911428', '0.79362376', '0.417777681', '0.658541168', '85', 'very expensive']
['0.234281085', '0.768671392', '0.455784293', '0.780224135', '73', 'very expensive']
['0.329952824', '0.333698305', '0.737541893', '0.537251952', '85', 'very expensive']
['0.471958364', '0.722226788', '0.629428645', '0.304862801', '8', 'affordable']
['0.454529755', '0.124055118', '0.838332848', '0.569409642', '28', 'expensive']
['0.957025298', '0.222965542', '0.831737726', '0.075038234', '91', 'very expensive']
not sure what your list looks like but what you have here will not work. For one the iterating variable app is an element of your list apps_data. You can't append to it.
Lets say your apps_data looks like apps_data-[0,1,2,3,10] so app will take on the value 0,1,2,3,10 as you iterate. Line price=float(app[4]) will through an error as app is a scalar. Perhaps you wanted price=float(apps_data[4]) which would be the value 10 in the example. You will need to initialize an empty list then append to it.
I am new to python and I am trying to develop a phone troubleshoot program where I ask the user what is wrong with their device, and if my program detects the word 'wet' or 'water' it will reply with an outcome. Another example would be 'screen is cracked'. I am having a problem that if I input 'My screen is cracked'. My code does not detect it. Any help appreciated!
Snippet of my code:
print(60 * '-')
print('Could you describe what is wrong with your device?')
print(60 * '-')
time.sleep(1)
user_problem = input('')
if user_problem in ('water', 'waterdamage', 'rain', 'toilet', 'pool', 'sea', 'ocean', 'river',):
print('WATERDAMAGE VARAIBLE')
elif user_problem in ('screen', 'cracked', 'shattered', 'smashed',):
print('SCREEN VARIABLE')
userproblem is your whole user's input. You are checking if it belongs to a tuple with those keywords. So, if the input is "My phone is wet", this string does not belong to ('water', 'waterdamage', 'rain', 'toilet', 'pool', 'sea', 'ocean', 'river',) since it's not equal to any of these words. Same problem in the second if.
The correct solution is to ask if any of these words is contained in the input, which is quite the opposite. You would have something like:
userproblem_words = userproblem.split(' ')
water_related_words = ('water', 'waterdamage', 'rain', 'toilet', 'pool', 'sea', 'ocean', 'river')
if (any([(word in water_related_words) for word in userproblem_words])):
print('WATERDAMAGE VARIABLE')
break_related_words = ('screen', 'cracked', 'shattered', 'smashed')
elif (any([word in userproblem for word in break_related_words])):
print('SCREEN VARIABLE')
Or, if you don't like the list comprehension's readability in this case, you can use a plain for:
water_related_words = ('water', 'waterdamage', 'rain', 'toilet', 'pool', 'sea', 'ocean', 'river')
break_related_words = ('screen', 'cracked', 'shattered', 'smashed')
for word in userproblem.split(' '):
if word in water_related_words:
print('WATERDAMAGE VARIABLE')
break
elif word in break_related_words:
print('SCREEN VARIABLE')
break
You need to change your approach, here is an example how you can do it:
print (60 * '-')
print ('Could you describe what is wrong with your device?')
print (60 * '-')
time.sleep(1)
userproblem = input('')
water = ['water', 'waterdamage', 'rain', 'toilet', 'pool', 'sea', 'ocean', 'river']
screen = ['screen', 'cracked', 'shattered', 'smashed']
for item in water:
if item in userproblem.split(' '):
print('WATERDAMAGE VARIABLE')
break
for item in screen:
if item in userproblem.split(' '):
print('SCREEN VARIABLE')
break