Questions on lists, the range function & concatenation

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1853 Views - Last Post: 25 January 2018 - 07:38 AM Rate Topic: -----

#1 fad3r  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 22-January 18

Questions on lists, the range function & concatenation

Posted 24 January 2018 - 08:32 AM

Hi everyone.

2nd time poster so getting use to everything.

Here is what I got going on:
I am trying to create a list that looks like this:['a', 'b', 'c', ..., 'z'} so that I can then create a dictonary out of it.

I found a way to create that and the numbers 1-26

mport string
mylist = string.ascii_lowercase
a = "'"
b = "'"
c = ", "
for i in mylist:
    letters1 = a + i + b + c
    #print (letters1)

#Create range of numbers for merging
numbers = range(1,27)



But I am running into issues with the rest of the code I have which looks like this

#Create range of numbers for merging
numbers = range(1,27)
d = ":"
for n in numbers:
    numbers1 = str(n) + d
    print (numbers1)



I get an error when running the code and I can't seem to figure out how to get the numbers assigned to a list. If I just print numbers it says numbers = range (1,27) almost like it is treating it like a string instead of a function.

If I loop through I get an error about mistmatched types. If I loop through and try to convert those to a string (what is above) it also doesnt work.

If possible please explain this like you are talking to a 4 year old.

Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Questions on lists, the range function & concatenation

#2 DK3250  Icon User is online

  • Pythonian
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,171
  • Joined: 27-December 13

Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 09:12 AM

I need some clarification here:
Are you trying to make string that looks like a dict, or do you want to actually make a dict ?
Was This Post Helpful? 1
  • +
  • -

#3 fad3r  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 22-January 18

Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 09:21 AM

Here is the full challenge. I am trying to break it into little bits and work on each one.

1) Start with this list:
#['a', 'b', 'c', ..., 'z'}
# And create a function that turns it into this dictionary:
#{1: 'a', 2: 'b', 3: 'c', ..., 26: 'z'}

# 2) Start with this string:
#'abc...z'
#And create a function that turns it into this dictionary:
#{1: 'a', 2: 'b', 3: 'c', ..., 26: 'z'}

# 3) Take one of the above functions and include only the odd numbers / letters:
# {1: 'a', 3: 'c', ..., 25: 'y'}

Not asking anyone to write the code because that prevents me from learning but more along of the lines of what am I doing wrong because not only am I new to Python I am new to programming. I was ok at Perl about 17 years ago but for all intensive purposes we could say I am starting from scratch.

I already figured out things that are wrong. I am not appending to a list in a code thus the list is constantly being overwritten. The print statements are in the wrong place so testing the wrong things.

So I have an idea of where to go or at least I am back out of the mud. The last part is a bit challenging and havent thought about how to figure that out yet.

This post has been edited by ndc85430: 24 January 2018 - 09:48 AM
Reason for edit:: Removed quote of previous post.

Was This Post Helpful? 0
  • +
  • -

#4 DK3250  Icon User is online

  • Pythonian
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,171
  • Joined: 27-December 13

Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 09:34 AM

ok, a few hints.
An easy way to make the list:
>>> letter_list = list("abcdefghijklmnopqrstuvwxyz")
>>> letter_list
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

From this list you want to extract both the index value and the letter - this is easily obtained by Pythons enumerate function.
'enumerate' is often not considered beginners stuff, but it is actually quite simple:
>>> for i, val in enumerate(letter_list):
	print(i, val)

	
0 a
1 b
2 c
3 d
4 e
5 f
6 g
7 h
8 i
9 j
10 k
11 l
12 m
13 n
14 o
15 p
16 q
17 r
18 s
19 t
20 u
21 v
22 w
23 x
24 y
25 z


I hope this will be of inspiration.
Was This Post Helpful? 1
  • +
  • -

#5 fad3r  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 22-January 18

Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 09:38 AM

Super helpful. Right now I am working on making the lists programmatically. Figured I might as well make the challenge harder so I can maximize my learnings. I will look into enumerate for the 2nd part.

Thanks!

This post has been edited by ndc85430: 24 January 2018 - 09:48 AM
Reason for edit:: Removed quote of previous post.

Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Beginner
  • member icon


Reputation: 10897
  • View blog
  • Posts: 18,590
  • Joined: 19-March 11

Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 09:39 AM

Quote

1) Start with this list:
#['a', 'b', 'c', ..., 'z'}
# And create a function that turns it into this dictionary:
#{1: 'a', 2: 'b', 3: 'c', ..., 26: 'z'}


So to start with, the question here is borked - the closing curly brace should be a square bracket, like so:
#['a', 'b', 'c', ..., 'z']



Now except for the elipsis, that's a list, in two senses: it's both the printed representation of a list, and it's also an expression that evaluates to a list. It's not actually the list, in some deep philosophical sense, but if you get rid of the three dots it's a complete specification of the list which is sufficient to instantiate the list in some python interpreter, and that's good enough for me.

So if we drop into the interpreter we can do something like

>>> l = ['a', 'b', 'c', ..., 'z']
  File "<stdin>", line 1
    l = ['a', 'b', 'c', ..., 'z']
                        ^
SyntaxError: invalid syntax
>>> l = ['a', 'b', 'c', 'z']
>>> l
['a', 'b', 'c', 'z']
>>> l[0]
'a'
>>> l[-1]
'z'
>>> 



See, it's a list.

Now, we want to turn that into a dictionary which maps the index of each entry in the list to that entry's value.

You're going to want to use the initial list, which you can either type in or get from the string library:

Quote

>>> import string
>>> dir(string)
['Formatter', 'Template', '_TemplateMetaclass', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_float', '_idmap', '_idmapL', '_int', '_long', '_multimap', '_re', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'atof', 'atof_error', 'atoi', 'atoi_error', 'atol', 'atol_error', 'capitalize', 'capwords', 'center', 'count', 'digits', 'expandtabs', 'find', 'hexdigits', 'index', 'index_error', 'join', 'joinfields', 'letters', 'ljust', 'lower', 'lowercase', 'lstrip', 'maketrans', 'octdigits', 'printable', 'punctuation', 'replace', 'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitfields', 'strip', 'swapcase', 'translate', 'upper', 'uppercase', 'whitespace', 'zfill']
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> list(_)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
>>> letters = list(string.ascii_lowercase)


Then, you want to write a function that iterates over the dictionary and maps each index to the value at that index. This is the naive way to do it, we can look at some slicker ways to manage it once you've done it the naive way.
Was This Post Helpful? 1
  • +
  • -

#7 fad3r  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 22-January 18

Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 09:45 AM

A friend sent it to me so that is just a typo (the bracket). I think the ... was to let me know all the letters. He could have wrote a-z but I think he gave me the first few letters to show the formatting.

Right now I am working on getting the letters into a list & trying to concatenate them with the 2nd list which is a list of numbers.

I think I need some kind of append command because I am constantly writing over the list. I am working on that now.

Playing with this:
import string
mylist = string.ascii_lowercase
a = "'"
b = "'"
c = ", "
letters1 = ""
for i in mylist:
    letters1.append ( a + i + b + c );
print (letters1)




Getting an error:

File "C:/Users/c0unt3rpl4y/PycharmProjects/House/Test 2.py", line 38, in <module>
letters1.append ( a + i + b + c );
AttributeError: 'str' object has no attribute 'append'

Process finished with exit code 1


But I think I have to figure out how to append the values for both strings & the numbers into a list before i move further.

This post has been edited by ndc85430: 24 January 2018 - 09:47 AM
Reason for edit:: Removed quote of previous post.

Was This Post Helpful? 0
  • +
  • -

#8 ndc85430  Icon User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 713
  • View blog
  • Posts: 2,896
  • Joined: 13-June 14

Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 09:49 AM

fad3r, there's no need to keep quoting the previous post. Please just use the "Reply" button.
Was This Post Helpful? 1
  • +
  • -

#9 fad3r  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 22-January 18

Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 09:50 AM

Sorry. Thanks for that.
Was This Post Helpful? 0
  • +
  • -

#10 ndc85430  Icon User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 713
  • View blog
  • Posts: 2,896
  • Joined: 13-June 14

Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 09:50 AM

The error is pretty clear: letters1 is a string and strings don't have an append method. You can use + to concatenate strings, but since strings are immutable be aware that that will produce a new string (so you'll have to reassign it to the original variable).
Was This Post Helpful? 1
  • +
  • -

#11 jon.kiparsky  Icon User is offline

  • Beginner
  • member icon


Reputation: 10897
  • View blog
  • Posts: 18,590
  • Joined: 19-March 11

Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 09:53 AM

View Postfad3r, on 24 January 2018 - 11:45 AM, said:

import string
mylist = string.ascii_lowercase
a = "'"
b = "'"
c = ", "
letters1 = ""
for i in mylist:
    letters1.append ( a + i + b + c );
print (letters1)




So here you're trying to put together a string that looks like a list. You want a list, not a string.

string.ascii_lowercase gives you a string with all of the lowercase letters of the Roman alphabet. Applying the list function to any string gives you the characters of that string as a list. So if you just want the list ['a','b','c',...'z'], then the example I gave you does exactly that.
letters = list(string.ascii_lowercase)


Was This Post Helpful? 1
  • +
  • -

#12 fad3r  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 22-January 18

Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 09:53 AM

Oh. I am still getting use to this. I wouldnt have figured that out in a like million years. Ok let me work on that and see what I come up with.

Thank you for all the help!
Was This Post Helpful? 0
  • +
  • -

#13 fad3r  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 22-January 18

Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 10:53 AM

Ok, with your guys help I have managed to solve creating the list for a-z and then creating the string for a-z.

I am now trying to create the range of numbers.

Here is what I have tired
#Create list of numbers
numbers = range(1,27)
list (numbers)
print (numbers)

#Create list of numbers
numbers = list((range(1,27))
print (numbers)




Both return range(1, 27) as if numbers were a string and range was functioning that way instead of as a function.

I also thought it might have to do with the range bringing back a string instead of an integer so I tried the int function also

#Create list of numbers
numbers = int((range(1,27))
print (numbers)




This just flat out produces an error. About print being wrong

I then tried this

#Create list of numbers
numbers = range(1,27)
int (numbers)
print (numbers)




And this gave an interesting error with: int (numbers)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'range'

Which leads me to believe the range function returns numbers already so not sure why when I try to print them or put them in a list I am getting errors.
Was This Post Helpful? 0
  • +
  • -

#14 ndc85430  Icon User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 713
  • View blog
  • Posts: 2,896
  • Joined: 13-June 14

Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 11:00 AM

No. The function returns and object in Python 3, that's basically something you can iterate over. int expects strings or something else that can be converted to a number, not a sequence of things to be converted.

You'll probably want to read up on how generators and iterators work.
Was This Post Helpful? 1
  • +
  • -

#15 jon.kiparsky  Icon User is offline

  • Beginner
  • member icon


Reputation: 10897
  • View blog
  • Posts: 18,590
  • Joined: 19-March 11

Re: Questions on lists, the range function & concatenation

Posted 24 January 2018 - 11:06 AM

Belay the part about what the range actually is, for now at least. It's an interesting question and you'll enjoy looking into it, but it's not the important thing for solving this problem. What you care about here is that a range is something that you can iterate over:
for i in range (1, 11):
  print(i)  # prints the numbers from 1 to 10 inclusive, each on a line




Knowing that, you should be able to create a dictionary, and then iterate over the numbers from 1 to 27, and add an entry to the dictionary for each of them

If creating the dictionary is hard, start there: for each integer from 1 to 10, map that number to itself. End result will be something like {1:1, 2:2, 3:3, ... 10:10}
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2