If you see here "Tuples and Sequences" you can see that your data structure is a tuple.
You can see here "More on Lists" that sort() is a function only for arrays.
You can use sorted() to sort arrays or tuples
new_list=("a","g","x","e","s","s")
a=sorted(new_list) # ['a', 'e', 'g', 's', 's', 'x']
Answer from Hearner on Stack OverflowIf you see here "Tuples and Sequences" you can see that your data structure is a tuple.
You can see here "More on Lists" that sort() is a function only for arrays.
You can use sorted() to sort arrays or tuples
new_list=("a","g","x","e","s","s")
a=sorted(new_list) # ['a', 'e', 'g', 's', 's', 'x']
new_list is defined as a tuple. Make it as a list by enclosing it in square
brackets
new_list=["a","g","x","e","s","s"]
new_list.sort()
print (new_list)
# ['a', 'e', 'g', 's', 's', 'x']
Sphinx 3.5 causes `AttributeError: 'tuple' object has no attribute 'sort'` on `ctx['css_files']`
python - AttributeError: 'tuple' object has no attribute 'sort' - Stack Overflow
Bug Report for combination-target-sum-ii: Solution that should run on initial test cases gives unexpected error
AttributeError: 'tuple' object has no attribute '' Error Help
Videos
I am trying to make a discord bot that would turn a message into lowercase. I am encountering an error, as the title suggests, "AttributeError: 'tuple' object has no attribute 'lower'. "
Here is my code if anyone can help.
https://hastebin.com/ibareyilax.py
If this is the wrong subreddit I don't mind taking down my post and posting it elsewhere.
You return four variables s1,s2,s3,s4 and receive them using a single variable obj. This is what is called a tuple, obj is associated with 4 values, the values of s1,s2,s3,s4. So, use index as you use in a list to get the value you want, in order.
Copyobj=list_benefits()
print obj[0] + " is a benefit of functions!"
print obj[1] + " is a benefit of functions!"
print obj[2] + " is a benefit of functions!"
print obj[3] + " is a benefit of functions!"
You're returning a tuple. Index it.
Copyobj=list_benefits()
print obj[0] + " is a benefit of functions!"
print obj[1] + " is a benefit of functions!"
print obj[2] + " is a benefit of functions!"
zip returns a list of tuples; you can sort them by accessing the elements with their index:
result_list.sort(key=lambda x: x[1], reverse=True)
if you like to be more explicit, you can use collections.namedtuple, and access the element via dot notation on the attribute name:
from collections import namedtuple
Poll = namedtuple('Poll', ['candidate', 'numvotes'])
uniquecandidate = ['a', 'b', 'c', 'd']
votes = [3, 4, 7, 1]
poll_results = list(map(Poll, uniquecandidate, votes))
poll_results.sort(key=lambda x: x.numvotes, reverse=True)
print(poll_results)
or with typing.NamedTuple:
from typing import NamedTuple
class Poll(NamedTuple):
candidate: str
numvotes: int
uniquecandidate = ['a', 'b', 'c', 'd']
votes = [3, 4, 7, 1]
poll_results = list(map(Poll, uniquecandidate, votes))
poll_results.sort(key=lambda x: x.numvotes, reverse=True)
print(poll_results)
out:
[Poll(candidate='c', numvotes=7), Poll(candidate='b', numvotes=4), Poll(candidate='a', numvotes=3), Poll(candidate='d', numvotes=1)]
Because your candidates and votes are stored in a tuple objects, the get() method won't work on them like it would on a dictionary. I recommend switching from
x.get('votes')
to this:
x[1]
This will get index 1 of each tuple (which in your case is the vote count).
from kubernetes import client, config
config.load_kube_config()
v1 = client.CoreV1Api()
print("Listing services with their IPs:")
ret = v1.list_service_for_all_namespaces_with_http_info(watch=False)
for i in ret.items:
print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name)) This throws this error: File "filename.py", line 8, in <module> for i in ret.items: AttributeError: 'tuple' object has no attribute 'items'
But when I simply print(ret), it certainly LOOKS like a tuple, which means I should be able to iterate through with tuple.items, no?