Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
My function takes a data frame as an argument that represent home adverts from the website. I am counting flats by the number of rooms and receive series which I would like to convert into a dictionary. My rooms counter seems to be a series still after applying to_dict(). Tried also with collections but it is the same.
def most_common_room_number(dane):
rooms = dane['Rooms']
rooms_counter = rooms.value_counts()
rooms_counter.to_dict()
# rooms_counter.to_dict(OrderedDict)
# dd = defaultdict(list)
# rooms_counter.to_dict(dd)
print(rooms_counter)
Assign rooms_counter.to_dict() to a variable and return that variable.
series = pd.Series([1, 2, 3])
d = series.to_dict()
print(type(d))
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
def gcd(m,n):
fm = []
for i in range(1, m+1):
if (m%i) == 0 :
fm.append(i)
fn = []
for j in range(1, n+1):
if (n%j) == 0 :
fm.append(j)
cf = []
for f in fm:
if f in fn:
cf.append(f)
return(cf[-1])
print(gcd(56,23))
Although, there are other ways to find gcd using while loop and math.gcd commands, but my mentor wants this solution working as its mentioned in his programming book. How can I solve IndexError: list index out of range in the above program.
You don't append anything to cf.
In your 2nd for loop, change fm.append(j) to fn.append(j)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I need to write a program that displays Celsius to Fahrenheit temperature conversion in a table.
Question - What is the correct way to print a specific index within a list. My attempt comes from the answer to a similar exercise outlined here. Thanks in advance.
This is my code
temp = range(0 , 101, 10)
tbl = []
tbl2 = []
for i in temp:
cel = i
tbl.append(cel)
fah = (i * 9/5) + 32
tbl2.append(fah)
print(cel, fah)
print('Celsius\t\tFahrenheit')
print('-------------------')
print(tbl(0) + ':\t\t', tbl2(0))
print('-------------------')
This is my output
Functions are called with parenthesis(). Indexes are accessed with with brackets[0]:
print(tbl[0] + ':\t\t', tbl2[0])
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I get a type error I cannot solve
This is to construct a counter for a dictionary:
counts = dict()
names = ['csev','cwen', 'csev', 'zqian', 'cwen']
#makes new tally for new names and updates existing names
for name in names :
if name not in counts:
counts[name] = 1
else:
counts[name] = counts[name + 1]
print(counts)
Should output:
{'csev':2, 'zqian':1, 'cwen':2}
change the line10 to
counts[name] = counts[name]+1
Even though the only problem you have is counts[name + 1] (should be counts[name] + 1 since you want to increment the count not the name), you should consider using a collections.Counter for this task:
from collections import Counter
names = ['csev', 'cwen', 'csev', 'zqian', 'cwen']
counts = Counter(names)
and while Counter is a dict-like object, if you want a dict object, use:
counts = dict(Counter(names))
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
def vector(sample_list):
for item in sample_list:
sums = 0
square = item**2
sums = square + sums
magnitude = sums**0.5
return magnitude
print(vector([2,3,-4]))
Why this code doesn't give the correct magnitude?
It gives the last value of the vector in function call.
change sums=0 position
def vector(sample_list):
sums = 0
for item in sample_list:
square = item**2
sums = square + sums
magnitude = sums**0.5
return magnitude
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
This is the code I am trying to run :
line = "123456789"
p = 2
print line[p,p+2]
And I get the error - TypeError: string indices must be integers, not tuple. How can I use line[ , ] with variables. Any help is appreciated.
You want to use colons for slicing.
line = "123456789"
p = 2
print line[p:p+2]
That works fine.
Output:
34
line = "123456789"
p = 2
print line[p,p+2] # this is incorrect slice notation
the correct form is:
print line[p:p+2] # with a colon
look here for info on strings and string slicing